@Autowired beans does not load after using beans: profiles in spring 3.1

I 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 installed 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 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); } } 

Mistake:

Error creating a bean named 'myController': injection auto-dependencies failed; nested exception org.springframework.beans.factory.BeanCreationException: failed field autowire: private MyService MyController.myService; org.springframework.beans.factory.BeanCreationException nested exception:

Nested org.springframework.beans.factory.BeanCreationException exception: error creating a bean named 'dbService': dependency injection self-timer injection failed; nested exception org.springframework.beans.factory.BeanCreationException: autowire method failed: public void DBServiceImpl.setDataSource (javax.sql.DataSource); org.springframework.beans.factory.NoSuchBeanDefinitionException nested exception: No bean mapping of type [javax.sql.DataSource] found for a dependency: expected at least 1 bean that qualifies as an outsourcing candidate for this dependency. Dependency Annotations: {} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues ​​(AutowiredAnnotationBeanPostProcessor.java:287) in org.springfutableButAfopeButAfopeBaentapapaBentAbopAbentAbentAbopAbentAbentAbentAbopAbentAbentAbentAbentAbentAbenteapaBentAbentAbenteapaBentAbentAbenteapaBeopaBeapAbentaapaBeapaBeapaBeapaBeapaFebopaapaobentaapabentaapabentaapabetaapobopaapabetaapobentaapabetaapobopaapabetaapobentaapybopabopaobyutapapiobyutapapi.besen org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.javaβ–Ί17)

+1
spring spring-mvc autowired spring-3
source share
1 answer

I assume that you are using the profile in the DispatcherServlet context, while the DataSource is most likely in the root context of the application.

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> 
+1
source share

All Articles