- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 spring 中遇到 @Autowired 注释的问题。我有一个 RestController 类:
package sptool.controller;
@org.springframework.web.bind.annotation.RestController
public class CategoryController {
@Autowired
private CategoryManager categoryManager;
//Methods
}
CategoryManager接口(interface):
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)}
当我创建ApplicationContext并尝试获取categoryManagerImpl bean时,Bun没有问题:
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
最佳答案
尝试进行以下更改:
@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,因为它没有使用 Service/Component 进行注释。执行上述操作应该可以解决这个问题。
仅供引用,如果您有多个 CategoryManager 实现,我建议您使用 @Component(value="myComponent") 包含实现的标识符
请告诉我这是否有帮助
关于java - spring "Manager"类中的 @Autowired 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37855723/
I have created a hybrid activation and then setup an ssm agent on my on-premise windows system.我创
我对 python/django 编程很陌生,因为我没有编程背景。我正在在线上课,我只想确切地知道 manage.py 文件的作用。我试过用谷歌搜索它,但除了在 django-admin.py 周围放
我的 DependancyInject 存在结构问题。 情况 我正在为基于体素的游戏创建服务器;它是完全调制的,但相关模块有以下3个。 NetworkModule(发送和接收数据包)WorldModu
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 5年前关闭。 Improve thi
上 Docker正在编写的网站: The MANAGER STATUS column shows node participation in the Raft consensus: No value
我正在尝试使用发布管理作为构建版本的工具,但我很难理解码件、工具和操作之间的真正区别。有人可以分解这三个概念之间的差异以及它们如何相互配合吗? 最佳答案 由于它适用于基于代理的版本: 工具旨在提供自定
当尝试在远程环境中在 pycharm 中执行“run manage.py Task...”时,出现以下错误: ssh://vagrant@127.0.0.1:2222/home/vagrant/.vi
在过去的 48 小时里,我一直在努力解决这个问题,这让我发疯了。 我的 SDK Manager.exe 闪烁一个 cmd 屏幕并在不到一秒内关闭。 经过多方搜索,我终于在调整android.bat并以
我在 this tutorial 之后创建了以下自定义管理命令. from django.core.management.base import BaseCommand, CommandError f
我在一家拥有 2,500 多名员工和同样多的 Android 智能手机的非营利组织工作。 近年来,我们测试了许多 EMM 产品。尽管我们只需要一些非常基本的功能,除了一两个特殊功能,但没有一个能真正赢
我已经在我的网站上安装了 Google 标签管理器,但自从新版本的 Google 标签管理器以来,我无法使用预览选项。每次我点击它时,我都会看到我的网站页面打开,但随后出现以下错误:“Tag Assi
我是 django 的新手,并创建了一个与教程中描述的民意调查网站没有太大区别的应用程序。 在网站上我得到: Exception Type: TemplateSyntaxError Exception
https://cloud.google.com/deployment-manager/docs/configuration/templates/create-basic-template 我可以像这
我们正在使用 Microsoft 的发布管理将我们的 Web 应用程序部署到我们的测试环境 (QA)。它是一个直接的 MVC.Net Web 应用程序。我们的构建生成一个 web 部署包,我们有一个命
我想将 python manage.py 缩短为 ./manage.py。 这可能很简单,但我找不到答案。我在有关 django 的问题的答案之一中看到了一步一步的方法,但我没有记住。尝试在 stac
我想将 python manage.py 缩短为 ./manage.py。 这可能很简单,但我找不到答案。我在有关 django 的问题的答案之一中看到了一步一步的方法,但我没有记住。尝试在 stac
我正在使用安装了 SQL Server Data Tools 的 VS 2012。我有一个 ADO NET 源,它使用 .Net Providers\MySQL 数据提供程序,并试图将一些数据推送到
根据我从文档中阅读的内容 https://developer.android.com/topic/libraries/architecture/workmanager , 它说: The task i
这两个类显然是相关的。 SupportFragmentManager 是否用于使用 FragmentTransaction 生成的 Fragments,而“常规”FragmentManager 专门用
我有一个桌子经理(经理ID、姓名、地址、城市、电话)。如果多个经理来自同一城市,我必须显示城市、姓名和电话详细信息。我的代码是:。但这向我展示了第一行中的一个错误,即“不是按表达式分组”。请救救我!
我是一名优秀的程序员,十分优秀!