我对spring中的@Autowired注释有问题。我有一个RestController类:
package sptool.controller;
@org.springframework.web.bind.annotation.RestController
public class CategoryController {
@Autowired
private CategoryManager categoryManager;
//Methods
}
接口:
package sptool.managers;
public interface CategoryManager {
public void save(Category category);
public Category getCategoryById(int id);
public List<Category> getAllCategories();
public void updateCategory(Category category);
public void deleteCategory(int id);
}
CategoryManagerImpl类:
package sptool.managers;
@Service
public class CategoryManagerImpl implements CategoryManager {
@Autowired
private CategoryDao categoryDao;
//Methods
}
每次编译代码时,我都会遇到一个异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private sptool.managers.CategoryManager sptool.controller.CategoryController.categoryManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [sptool.managers.CategoryManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Bun当我创建ApplicationContext并尝试获取categoryManagerImpl bean时,没有问题:
applicationContext = new AnnotationConfigApplicationContext(ConfigurationClass.class);
application.getBean("controllerManagerImpl"); //<< works
有人知道哪里会有问题吗?
谢谢
添加了
配置类:
package sptool.config;
@Configuration
@ComponentScan({"sptool.managers", "sptool.dao", "sptool.controller", "sptool.model"})
@PropertySource({"classpath:application.properties"})
public class ConfigurationClass {
@Autowired
private Environment env;
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
@Bean(name = "sessionFactory")
public LocalSessionFactoryBean sessionFactoryBean()
{
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(env.getProperty("annotated.packages"));
sessionFactory.setPackagesToScan( new String[] {"sprool.model"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
Properties hibernateProperties()
{
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", env.getProperty("spring.jooq.sql-dialect"));
properties.setProperty("hibernate.show_sql", env.getProperty("show.sql"));
properties.setProperty("hibernate.current_session_factory", env.getProperty("current.session.class"));
properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
return properties;
}
@Bean(name = "transactionManager")
@Autowired
public HibernateTransactionManager hibernateTransactionManager(SessionFactory sessionFactory)
{
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
}
堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private sptool.managers.CategoryManager sptool.controller.CategoryController.categoryManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [sptool.managers.CategoryManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at sptool.controller.App.main(App.java:18) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private sptool.managers.CategoryManager sptool.controller.CategoryController.categoryManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [sptool.managers.CategoryManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 22 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [sptool.managers.CategoryManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 24 common frames omitted
由于此CategoryManagerImpl
是用@Service
注释的,因此不需要在spring servlet中再次声明它(如
applicationContext = new AnnotationConfigApplicationContext(ConfigurationClass.class);
application.getBean("controllerManagerImpl"); //<< works
最后,CategoryManager将被CategoryManagerImpl很好地注入。
尝试执行以下更改:
@Service
public interface CategoryManager {
public void save(Category category);
public Category getCategoryById(int id);
public List<Category> getAllCategories();
public void updateCategory(Category category);
public void deleteCategory(int id);
}
@Component
public class CategoryManagerImpl implements CategoryManager {
@Autowired
private CategoryDao categoryDao;
//Methods
}
您面临的问题是,spring无法识别bean CategoryManager,因为它没有使用服务/组件进行注释。这样做应该可以解决这个问题。
仅供参考,如果您有一个以上的类别管理器实现,我建议您使用@Component(value="myComponent")为实现包含标识符
如果有帮助,请告诉我
我仍然是Spring的新手,我正在尝试正确设置ehcache Spring注解。我使用的是Spring 3.2.3 ehCache 2.4和ehcache-spring-注解-1.2。 当我尝试访问对cacheManager的引用时,它总是空的。所有jar都在构建路径ehcache上。xml在类路径中,没有xml错误。我还尝试在组件扫描中包含类,并使用@Resource而不是自动连接。我卡住了!
我正在查看工作区中的一些旧示例。我看不出自动连线是如何完成的,因为没有@Autowired。Spring Boot默认配置。 它工作得很好,但是如果没有自动配线,这些豆子是如何自动配线的呢?它们是自动作为字段还是在构造函数中?
我有个小问题。如果类是用@component、@service、@controller或@repository注释的,并且我想注入它的依赖项,我需要@autowired吗? 这段代码对我来说非常适用,因为它是UserDeviceService中指定的@Service注释。是因为那个吗?
我有一个带有自动扫描和@Component注释的Spring项目。一些组件需要使用@Autow的注入到不同的bean中。默认情况下,它是否将是作为单例创建的相同组件bean?如果是,如何将同一组件的不同实例注入不同的bean中? 附言:我知道它接近基础,听起来很一般。只是想自己说清楚。 提前致谢
和和注释之间有什么区别? 我们应该在什么时候使用它们每一个?
我有一节这样的课- 我试图注入'pty'变量从另一个类'环境'这是自动的,我得到这个异常时,我的服务器启动 错误创建名为'某物类'的bean:注入自动生成的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreation异常:无法自动连接方法:公共无效service.facade.ActionFacade.setPty(java.lang.字符串)