1

私は自分の問題の1つをグーグルで検索し、注釈を介して解決策を見つけました@component

しかし、私のアプリケーションでは xml 構成を使用しています。これは、注釈が厄介で構成可能ではないためです。コードをすべて再コンパイルして、smth を変更する必要があります。

だから、私の質問は、xml-confでこのソリューションをどのように使用するのですか? その中にコンポーネントを実装する方法は?

4

1 に答える 1

0

編集

あなたのコメントから、リスナーをAuthenticationEventに追加したいことがわかります

public class AuthenticationEventListener 
        implements ApplicationListener<AbstractAuthenticationEvent> {

    @Override
    public void onApplicationEvent(AbstractAuthenticationEvent event) {
        // process the event
    }
}

ここで、このタイプの Bean を、セキュリティが構成されているのと同じ Spring コンテキストに配置する必要があります。security-context.xmlで春のセキュリティを構成したとします。次に、このコンテキストで Bean を定義する必要があります

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/security
                           http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <security:global-method-security secured-annotations="enabled" />

    <security:http auto-config="true">
        <!-- Restrict URLs based on role -->
        <security:intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <security:intercept-url pattern="/logoutSuccess*" access="IS_AUTHENTICATED_ANONYMOUSLY" />

        <security:intercept-url pattern="/css/main.css" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <security:intercept-url pattern="/resources/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />

        <security:intercept-url pattern="/**" access="ROLE_USER" />

        <!-- Override default login and logout pages -->
        <security:form-login login-page="/login.html" 
                             login-processing-url="/loginProcess" 
                             default-target-url="/index.jsp" 
                             authentication-failure-url="/login.html?login_error=1" />
        <security:logout logout-url="/logout" logout-success-url="/logoutSuccess.html" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider >
            <security:jdbc-user-service data-source-ref="dataSource" />
        </security:authentication-provider>
    </security:authentication-manager>

   <bean id="authenticationEventListener" 
         class="AuthenticationEventListener"/>


  </beans>

PS

@component アノテーションを使用したくない場合は、xml で直接 Bean を作成できます。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.HelloWorld" 
      scope="singleton" name="componentValue">
   </bean>

</beans>

Xml またはアノテーションのいずれかの方法で、Bean はアプリケーションコンテキストの下になります。

クラスパスのスキャン中に Bean を自動検出して構成する @Component アノテーションが導入されました。

于 2015-05-05T09:00:21.963 に答える