问题
I have used beans:profiles
in my xml like this:
<beans profile="dev">
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.internal.url}" />
<property name="username" value="${jdbc.internal.username}" />
</bean>
</beans>
I've set the spring.active.profiles
in web.xml:
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myapp-servlet.xml</param-value>
</init-param>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</init-param>
</servlet>
My code structure is like this:
//controller
@Controller
public class MyController {
@Autowired
private MyService myService;
....
}
//service implementation
@Service("myservice")
public class MyServiceImpl implements MyService {
@Autowired
DBService dbService;
}
//db service
@Service("dbservice)
public class DBServiceImpl implements DbService {
@Autowired
public void setDataSource (Datasource ds) {
this.jdbcTemplate = new JdbcTemplate(ds);
}
}
Error:
Error creating bean with name 'myController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private MyService MyController.myService; nested exception is org.springframework.beans.factory.BeanCreationException:
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void DBServiceImpl.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
回答1:
My guess it that you are using profile in DispatcherServlet context, while DataSource is likely located in the root application context.
See Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
update: try using context-params (taken from here):
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
来源:https://stackoverflow.com/questions/13311603/autowired-beans-not-loading-after-using-beansprofiles-in-spring-3-1