伙计们,我想注册并注入带有配置注释的spring bean但没有成功。这是我想在抽象级别创建的bean:
public class MyBean{
private AnotherBean anotherBean;
private String someString;
public MyBean(AnotherBean anotherBean, String someString) {
this.anotherBean= anotherBean;
this.someString= someString;
}
public MyBean(){}
public MyPojo someMethod(MyPojo oject, final String anyString) {
//some logic
}
}
这是我的配置类:
@Configuration
public class BeanConfiguration {
private static final String SOME_STRING = "someString";
@Inject
@Qualifier("anotherBean")
private AnotherBean otherBean;
@Bean(name = "myBean")
public MyBean getMyBean(){
MyBean myBean= new MyBean(otherBean, SOME_STRING );
return myBean;
}
}
我在应用程序上下文中声明了配置bean:
<bean class="configuration.package.BeanConfiguration"/>
所以问题是spring容器创建了一个BeanConfiguration对象,但getMyBean()方法永远不会被命中,所以我的bean永远不会被注册,当我尝试注入一个异常时告诉我bean无法自动连接,所以它没有注入(这是正常的,因为bean没有注册)。这是堆栈跟踪
10:33:03.322 [http-apr-8080-exec-8] ERROR o.s.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bet': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.bean.package.MyBean
my.bean.package.MyBean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [my.bean.package.MyBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) ~[AutowiredAnnotationBeanPostProcessor.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122) ~[AbstractAutowireCapableBeanFactory.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) ~[AbstractAutowireCapableBeanFactory.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) ~[AbstractAutowireCapableBeanFactory.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) ~[AbstractBeanFactory$1.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[DefaultSingletonBeanRegistry.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) ~[AbstractBeanFactory.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[AbstractBeanFactory.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) ~[DefaultListableBeanFactory.class:3.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) ~[AbstractApplicationContext.class:3.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) ~[AbstractApplicationContext.class:3.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651) ~[FrameworkServlet.class:3.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599) ~[FrameworkServlet.class:3.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665) ~[FrameworkServlet.class:3.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518) ~[FrameworkServlet.class:3.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459) ~[FrameworkServlet.class:3.2.2.RELEASE]
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) [HttpServletBean.class:3.2.2.RELEASE]
at javax.servlet.GenericServlet.init(GenericServlet.java:158) [servlet-api.jar:3.1.FR]
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1241) [catalina.jar:8.0.9]
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1154) [catalina.jar:8.0.9]
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827) [catalina.jar:8.0.9]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) [catalina.jar:8.0.9]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [catalina.jar:8.0.9]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) [catalina.jar:8.0.9]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136) [catalina.jar:8.0.9]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:8.0.9]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) [catalina.jar:8.0.9]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.9]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526) [catalina.jar:8.0.9]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) [tomcat-coyote.jar:8.0.9]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655) [tomcat-coyote.jar:8.0.9]
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277) [tomcat-coyote.jar:8.0.9]
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381) [tomcat-coyote.jar:8.0.9]
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370) [tomcat-coyote.jar:8.0.9]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_60-ea]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_60-ea]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.9]
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_60-ea]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.bean.package.MyBean my.bean.package.myBean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [my.bean.package.MyBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) ~[AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.class:3.2.2.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) ~[InjectionMetadata.class:3.2.2.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) ~[AutowiredAnnotationBeanPostProcessor.class:3.2.2.RELEASE]
... 37 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [my.bean.package.MyBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:986) ~[DefaultListableBeanFactory.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:856) ~[DefaultListableBeanFactory.class:3.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768) ~[DefaultListableBeanFactory.class:3.2.2.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) ~[AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.class:3.2.2.RELEASE]
... 39 common frames omitted
所以我无法理解为什么我的豆子没有注册,所以如果有人可以提供帮助,我将不胜感激。
答案 0 :(得分:0)
第一个问题是因为你使用private with inject而你没有set函数,而另一个问题是因为你的xml配置中没有<bean class="....MyBean"/>