Search code examples
javaspringspring-annotationsxml-configuration

What is xml-configuration representation of @component in spring


I have googled one of my problem and found the soulution via @component annotation.

But in my application I'm using xml configuration, because of annotationes are nasty and not configurable, and you need to recompile all of the code co change smth.

So, my question is: how do I use this solution vith xml-conf? How to implement the component in it?


Solution

  • EDIT

    From your comment I can see that you want to add listener to AuthenticationEvent

    public class AuthenticationEventListener 
            implements ApplicationListener<AbstractAuthenticationEvent> {
    
        @Override
        public void onApplicationEvent(AbstractAuthenticationEvent event) {
            // process the event
        }
    }
    

    Now you have to put a bean of this type in the same spring context where security is configured. Suppose you have configured your spring security in security-context.xml. Then you must define your bean in this context

    <?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.

    If you don't want to use @component annotation you can create the bean directly in your xml.

    <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 or annotation either way your bean will will be under application context.

    @Component annotation was introduced to auto detect and configure beans during class path scanning.