什么是 xml-@component 在 spring 中的配置表示
What is xml-configuration representation of @component in spring
我用谷歌搜索了我的一个问题,并通过 @component
注释找到了解决方案。
但在我的应用程序中,我使用了 xml 配置,因为注释很讨厌且不可配置,并且您需要重新编译所有代码并更改 smth。
所以,我的问题是:如何通过 xml-conf 使用此解决方案?里面的组件怎么实现?
EDIT
从您的评论中我可以看出您想向 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>
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 注释被引入以在 class 路径扫描期间自动检测和配置 bean。
我用谷歌搜索了我的一个问题,并通过 @component
注释找到了解决方案。
但在我的应用程序中,我使用了 xml 配置,因为注释很讨厌且不可配置,并且您需要重新编译所有代码并更改 smth。
所以,我的问题是:如何通过 xml-conf 使用此解决方案?里面的组件怎么实现?
EDIT
从您的评论中我可以看出您想向 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>
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 注释被引入以在 class 路径扫描期间自动检测和配置 bean。