2013-08-18 75 views
0

我使用的春春註解來配置服務層添加prarameter:一旦春錯誤時,豆

@Configuration 
public class AccountServices { 
    @Bean 
    public List<String> getSourceAccountsList() { //Do Some thing} 

但由於方法改變路徑字符串參數:

下面的代碼工作正常(下面的代碼)我得到的錯誤:

@Configuration 
public class AccountServices { 
    @Bean 
    public List<String> getSourceAccountsList(String userId) {//Dp something } 

的錯誤是:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'getSourceAccountsList' defined in class path resource [net/iranet/isc/saba/services/AccountServices.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:730) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:461) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1025) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:921) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206) 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840) 
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 
    at org.apache.catalina.core.StandardService.start(StandardService.java:525) 
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 
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:988) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770) 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:795) 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723) 
    ... 30 more 
+0

您確定您使用正確的服務嗎?爲什麼你不把服務註釋爲'@ Service'? getSourceAccountsList方法似乎是你需要從其他地方調用的方法,參數是從調用者提供的,而不是配置本身。 –

+0

是的,服務是從Web控制器調用的(struts action)。我發現「現在春天3.2.4」@Service註釋不會比組件做任何事情。但我會使用@Service,因爲他們說它可能會在將來改變 –

回答

2

我認爲你是在一個錯誤的方式使用@Bean,尤其是在@Service層(通常@Bean方法沒有一個GET,但只有在你的情況sourceAccountsList())。
誰提供userId參數,網頁控制器?

如果AccountService從控制器叫你碰到這樣的(僞代碼):

@Service 
public class AccountServices { 
    public List<String> getAccountService(String userId) { 
    // Retrieve List<String> from database, or file, or somewhere else via DAO 
    // directly 
    } 
} 

@Controller 
public class Controller { 
    @Autowired 
    private AccountsService accountService; 

    public void doAction(Request request) { 
    String userId = request.getParameter("userId"); 

    request.putParameter("accountsList",this.accountService.getSourceAccountsList(userId)); 
    forwardTo("/newPage.jsp", request); 
    } 
} 

閱讀有關彈簧裝置控制器/服務/存儲庫(Google search有很多資源的),並從SpringSource
理解層分離後,您可以添加@Transaction管理(例如)以及所需的所有其他功能。

+0

你救了我!謝謝。還有一個幫助。 我預計Controller類應該有一個AccountServices accountServices。 所以春天可以用它來在控制器中注入AccountService。我正在運行一個示例項目,但每件事情都沒有和setAccountService(AccountServices accountServices)一起工作...... –

+0

好的。如果你解決了你的問題,請接受回答 –

+0

很可能是由於自動裝配(@Autowired註解),但我的建議是閱讀關於Spring註釋的官方文檔 –

0

當您使用@Configuration和@Bean註釋來配置bean時,用@Bean註釋的方法的參數被認爲是對另一個bean的引用,所以您應該添加帶有類String的bean以及bean的名稱「userId」 - 名稱用於按名稱自動裝配的參數。

+0

可以請讓我知道我應該如何做註釋! –

+0

你應該用@Bean註解和返回類型聲明另一個方法String –

+0

你的意思是像String setUserId(String userID)和make getSourceAccountsList()使用它!我是非常新的使用春天 –