当bean名称动态传递时,从上下文获取bean时出现异常

时间:2017-01-23 14:35:48

标签: java spring spring-boot

我的代码如下。

接口

@Component
public interface Service {
    public String getConfig();
}

2实施

@Service("UserService")
  public class userService implements Service{
        @Override
        public String getConfig() {
            // logic goes here
         return "result";
        }
}

客户实施

@Service("CustomerService")
public class userService implements Service{
    @Override
    public String getConfig() {
        // logic goes here
     return "result";
    }
}

我有一个配置类,我需要动态bean

@Configuration
public class MyConfig {
    @Autowired
    ApplicationContext context;
    @Bean
    public  Service getConfigBean(final String configName) {
        Service service = (Service) context.getBean(configName);
        return service;
    }
}

来自我的控制器/任何其他类我只想获取bean对象

   @RestController
@RequestMapping("/user")
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class UserController {
    @Autowired
    MyConfig myConfig;

    @Autowired
    Service  service;

    @RequestMapping(produces = MediaType.TEXT_HTML , method = RequestMethod.GET)
    public String getInitiate()
    {
        service =  stateConversationConfig.getConfigBean("UserService");      // will get this String dynamically or based on logic
        System.out.println("service object : "+service);
        return "ok";
    }
}

但是当我在tomcat上运行我的应用程序时,它无法部署以及异常

   SEVERE: Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'getConfigBean' defined in class path resource [xxx/xxx/xxx/xxxxx.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: : No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at xx.xxx.xxxx.xxx.xxx.main(Application.java:36)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)

我做错了什么。?

1 个答案:

答案 0 :(得分:0)

Bean是由Spring IoC容器管理的对象。创建应用程序上下文时,会构造bean,但Spring无法创建configBean,因为它不知道应将哪个值传递给configName

如果您想要动态服务,我建议您创建2个单独的bean(userServicescustomerService)和正常函数(不是@BeangetConfigBean(String configName)。< / p>

相关问题