将基于注释的 Bean 注入基于 xml 的 bean
Inject annotation based Bean into an xml based bean
我的持久性配置 Class 如下所示:
@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;
}
}
我有一个 xml 配置 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 -->
所以我想注入名为“dataSource”的 bean,并用 @Bean 注释(在持久性配置中 class ) ,在 <jdbc-user-service data-source-ref="dataSource" ... >
,我该怎么做?
试试这个:
<bean class="PersistenceConfig.class"/>
在您的 xml 配置中
提示:确保您拥有此 post 处理器
<bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>
这是因为您在创建 "dataSource" bean 的 PersistenceConfig class 之前加载 xml 配置。首先加载 class,然后加载 xml 配置。
<?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 ">
<context:component-scan base-package="your_package.PersistenceConfig" />
<context:annotation-config /><mvc:annotation-driven/>
<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 -->
我通过更改配置文件的加载顺序使其工作,以便 Spring 加载包含 :
的配置文件
<context:component-scan base-package="com.rsone.*" />
<context:annotation-config />
<mvc:annotation-driven/>
在我的webSecurityConfig.xml(上面的配置文件)
之前
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/mvc-servlet.xml
classpath:webSecurityConfig.xml
</param-value>
</context-param>
我的持久性配置 Class 如下所示:
@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;
}
}
我有一个 xml 配置 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 -->
所以我想注入名为“dataSource”的 bean,并用 @Bean 注释(在持久性配置中 class ) ,在 <jdbc-user-service data-source-ref="dataSource" ... >
,我该怎么做?
试试这个:
<bean class="PersistenceConfig.class"/>
在您的 xml 配置中
提示:确保您拥有此 post 处理器
<bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>
这是因为您在创建 "dataSource" bean 的 PersistenceConfig class 之前加载 xml 配置。首先加载 class,然后加载 xml 配置。
<?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 ">
<context:component-scan base-package="your_package.PersistenceConfig" />
<context:annotation-config /><mvc:annotation-driven/>
<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 -->
我通过更改配置文件的加载顺序使其工作,以便 Spring 加载包含 :
的配置文件<context:component-scan base-package="com.rsone.*" />
<context:annotation-config />
<mvc:annotation-driven/>
在我的webSecurityConfig.xml(上面的配置文件)
之前<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/mvc-servlet.xml
classpath:webSecurityConfig.xml
</param-value>
</context-param>