@Autowired beans not loading after using beans:profiles in spring 3.1

那年仲夏 提交于 2019-12-20 03:52:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!