java - spring中@component的xml配置表示是什么

标签 java spring spring-annotations xml-configuration

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

但是在我的应用程序中,我使用 xml 配置,因为注释很麻烦并且不可配置,并且您需要重新编译所有代码以进行更改。

所以,我的问题是:如何通过 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 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。

关于java - spring中@component的xml配置表示是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30048205/

相关文章:

python - 类似于python中的spring sleuth的框架

java - 错误: org. springframework.web.context.ContextLoader - 上下文初始化失败org.springframework.beans.factory.BeanCreationException

java - Spring Task Executor 在上一个任务完成之前不会触发

java - 在运行时更改 Spring RequestMapping 注解值

java - 如何使返回多个对象的TestNG @DataProvider仅返回一个?

java - sendRedirect 还是 request Dispatch 效率更高?

spring - 如果目标上的消费者已关闭,则通知 ActiveMQ 生产者

java - List<String> 到 ArrayList<String> 的转换问题

java - 指定 Jackson 的字段命名策略

mapping - spring-data-elasticsearch - @Field/FieldIndex.not_analyzed 被忽略