Search code examples
springspring-dataspring-ioc

Inject annotation based Bean into an xml based bean


My persistence configuration Class looks like bellow :

@Configuration
@EnableTransactionManagement
public class PersistenceConfig {

    @Autowired
    private Environment env;

    // code here

    @Bean(name = "dataSource")
    public DataSource dataSource()
    {
        System.out.println("------------------------datasource----------------------");
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        return dataSource;
    }


}

And i have an xml configuration class :

<?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:oauth="http://www.springframework.org/schema/security/oauth2"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:sec="http://www.springframework.org/schema/security" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd ">

<authentication-manager alias="authenticationManager"
                            xmlns="http://www.springframework.org/schema/security">
     <authentication-provider>

        <user-service>
            <user name="username" password="password" authorities="USER_ROLE" />
        </user-service>


        <jdbc-user-service data-source-ref="dataSource"
                               users-by-username-query="query here"
                               authorities-by-username-query="query here" />

    </authentication-provider>
</authentication-manager>
<!-- code here -->

So i want to inject the bean called "dataSource" annotated with @Bean (in the persistence config class) , in <jdbc-user-service data-source-ref="dataSource" ... > , how can I do it ?


Solution

  • I made it work by changing the loading order of my config files so that Spring load the config file that contains :

    <context:component-scan base-package="com.rsone.*" />
    <context:annotation-config />
    <mvc:annotation-driven/>
    

    before my webSecurityConfig.xml ( the above config file )

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                WEB-INF/mvc-servlet.xml
                classpath:webSecurityConfig.xml
            </param-value>
    </context-param>