1

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)

birdy
  • 9,286
  • 24
  • 107
  • 171

1 Answers1

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>
Community
  • 1
  • 1
Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
  • hmmm so I should set the profile in application context? how do I do that? – birdy Nov 09 '12 at 16:22
  • I was following this blog post and thats how they were doing it: http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/ – birdy Nov 09 '12 at 16:23
  • I think you might be right, because when I explicitly use `@Service("dbService) @Profile("dev") public class DBServiceImpl...` I at least don't get datasource not found exception.. But I this won't be my ideal solution. I much rather set active profile in application context – birdy Nov 09 '12 at 16:29
  • Hmmm I'm reading https://jira.springsource.org/browse/SPR-9035 they agree that this is by design, but it doesn't seem that they post a solution. – Boris Treukhov Nov 09 '12 at 16:30
  • or maybe I'm mistaken. because if I intentionally give profile a bad name like: `@Service("dbService) @Profile("devasdfsfd") public class DBServiceImpl..` even then I don't get datasource not found exception... – birdy Nov 09 '12 at 16:31
  • `@Autowired` always wires by-type so it doesn't matter how you name your beans in this case. – Boris Treukhov Nov 09 '12 at 16:33
  • wow, that works!! how do I buy you a beer? :) thanks for all the help! – birdy Nov 09 '12 at 16:40