我搜索了我的一个问题,并通过@component
注释找到了这个问题。
但是在我的应用程序中我使用xml配置,因为注释很讨厌而且不可配置,你需要重新编译所有代码更改smth。
所以,我的问题是:我如何使用这个解决方案与xml-conf?如何在其中实现组件?
答案 0 :(得分:0)
修改
根据您的评论我可以看到您想要将监听器添加到 AuthenticationEvent
public class AuthenticationEventListener
implements ApplicationListener<AbstractAuthenticationEvent> {
@Override
public void onApplicationEvent(AbstractAuthenticationEvent event) {
// process the event
}
}
现在你必须在这个配置了安全性的相同的spring上下文中放入这种类型的bean。假设您已在 security-context.xml 中配置了spring security。然后,您必须在此上下文中定义您的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>
P.S。
如果您不想使用@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将在应用程序上下文中。
引入了@Component注释,以便在类路径扫描期间自动检测和配置bean。