2017-01-23 54 views
0

我有以下代碼。從上下文獲取bean時發生異常,當bean名稱動態傳遞

接口

@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"; 
    } 
} 

我有一個配置類,我需要動態豆

@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) 

我是否部署

做錯事。?

+0

注意java命名約定。類名應以大寫字母開頭 – Jens

+0

從控制器的「服務」字段中刪除「@ Autowired」。你正在設置它的方法,所以你不想自動佈線... –

回答

0

Bean是由Spring IoC容器管理的對象。創建應用程序上下文時,bean會被構建,但Spring無法創建您的configBean,因爲它不知道應將什麼值傳遞給configName

如果你要動態的服務,我建議你創建2種獨立的豆類(userServicescustomerService)和正常功能(不與@BeangetConfigBean(String configName)

+0

黃,感謝您的幫助..它似乎我正在尋找功能,這是不可能在春天。感謝您的建議。 –

相關問題