1

我用谷歌搜索了我的一个问题,并通过@component注释找到了解决方案。

但是在我的应用程序中,我使用的是 xml 配置,因为注释很讨厌且不可配置,并且您需要重新编译所有代码来共同更改。

所以,我的问题是:如何通过 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中配置了 spring 安全性。然后你必须在这个上下文中定义你的 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>

附言

如果您不想使用 @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>

无论您的 bean 将在应用程序上下文中,XML 或注解都将如此。

引入了@Component 注释以在类路径扫描期间自动检测和配置 bean。

于 2015-05-05T09:00:21.963 回答