1

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?

TEXHIK
  • 1,369
  • 1
  • 11
  • 34
  • 1
    Tip: You'll have to rebuild if you change the XML files anyway, and the annotations are usually much more readable and maintainable. – chrylis -cautiouslyoptimistic- May 05 '15 at 08:52
  • With annotations you should to look throw all project code to find something, and you can just change configuration right in war file and only redeploy your app. But anyway, using annotations in my project is impossible. – TEXHIK May 05 '15 at 08:56

1 Answers1

0

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.

mirmdasif
  • 6,014
  • 2
  • 22
  • 28
  • Works to load my class, but some reasons event listning in it does dot work: `implements ApplicationListener` `onApplicationEvent(AbstractAuthenticationEvent event){System.out.println("smth");}` – TEXHIK May 05 '15 at 09:07
  • Thanks! But the last small question: what does `name="componentValue"` mean and for what is it need (it wokrs for me without this attribute)? – TEXHIK May 05 '15 at 10:01
  • 1
    This attribute is optional. If you do not specify this attribute then name attribute will be taken from class name. – mirmdasif May 05 '15 at 10:05