- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Spring mvc 中遇到错误:
(org.springframework.beans.factory.BeanCreationException:创建名称为“webController”的bean时出错: Autowiring 依赖项注入(inject)失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法 Autowiring 字段:private com. nickolay.app.service.UserService com.nickolay.app.WebController.userService;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项 [com.nickolay.app.service.UserService] 类型的合格 bean:预计至少有 1 个有资格作为此依赖项的 Autowiring 候选者的 bean。依赖项注释)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nickolay.app.service.UserService com.nickolay.app.WebController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nickolay.app.service.UserService] 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)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4997)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5289)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3831)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5616)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nickolay.app.service.UserService com.nickolay.app.WebController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nickolay.app.service.UserService] 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)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 31 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nickolay.app.service.UserService] 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)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 33 more
我的 Controller (WebController.java):
@Controller
public class WebController {
@Autowired
private UserService userService;
.................
}
我的UserService.java:
@Service
public class UserService implements UserServiceInterface {
@Autowired
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
@Transactional
public void create(User user) {
userDao.create(user);
}
................
}
我的HibernateUserDao.java,它实现了UserDao:
@Repository("UserDao")
public class HibernateUserDao implements UserDao {
@Autowired
SessionFactory sessionFactory;
public HibernateUserDao() {
}
@Override
@Transactional
public void create(User user) {
if (user == null) {
throw new NullPointerException();
}
Session session = sessionFactory.getCurrentSession();
session.save(user);
}
.............
}
我的servlet-contents.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"
<annotation-driven />
<tx:annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.nickolay.app" />
<beans:bean id="userDao" class="com.nickolay.app.dao.HibernateUserDao" />
<beans:bean id="roleDao" class="com.nickolay.app.dao.HibernateRoleDao" />
<beans:bean id="userService" class="com.nickolay.app.service.UserService">
</beans:bean>
</beans:beans>
最佳答案
在这种情况下,您无法注入(inject)具体类 UserService。这是因为 Spring 正在为您的类创建一个代理,以便处理 @Transactional。由于您的服务实现了一个接口(interface),Spring 正在创建一个基于 JDK 接口(interface)的代理,即代理实现 UserServiceInterface 并委托(delegate)给 UserService。因此Spring管理的bean不是UserService而是UserServiceInterface
有两种方法可以解决这个问题。最简单的方法是将依赖项从 UserService 更改为 UserServiceInterface
@Controller
public class WebController {
@Autowired
private UserServiceInterface userService;
}
第二种方法是指示 Spring 使用基于 CGLIB 类的代理而不是基于 JDK 接口(interface)的代理。更改您的 tx:annotation-driven 以将 proxy-target-class 指定为 true
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"
<annotation-driven />
<tx:annotation-driven proxy-target-class="true/>
<resources mapping="/resources/**" location="/resources/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.nickolay.app" />
<beans:bean id="userDao" class="com.nickolay.app.dao.HibernateUserDao" />
<beans:bean id="roleDao" class="com.nickolay.app.dao.HibernateRoleDao" />
<beans:bean id="userService" class="com.nickolay.app.service.UserService">
</beans:bean>
</beans:beans>
关于java - NoSuchBeanDefinitionException : No qualifying bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36743755/
根据这个answer ,下面的代码应该没有错误地编译: #include namespace { struct A { int i; }; volatile A a{}; static_assert
我了解导入 Data.List。 但是 importqualified Data.List 语句中的 qualified 是什么意思? 最佳答案 合格的导入使导入的实体仅以合格的形式可用,例如 imp
我正在尝试创建一个组件,该组件将接受一个特定的 bean (fasterxml ObjectMapper)。 如果有一个名为 qualifiedObjectMapper 的限定 bean,我想使用那个
我一直在编写以下代码来支持对右值的函数调用,而不必 std::move明确地在返回值上。 struct X { X& do_something() & { // some co
我正在尝试使用 GCC 编译器编译以下代码 class Class { public: uInt16 temp; const uInt32 function() const; } inline
我试图理解为什么我的编译器警告我有关此代码: static const char *const _menuMain_Strings_1[] __ATTR_PROGMEM__ = { _menuMai
我正在尝试将 Spring 4.x.x 迁移到 Spring boot,它依赖于外部 spring 2.5 jar 中的类。我已经进行了所有 Autowiring 更改,下面是我的应用程序类 @Spr
我从 eclipse git 中查看了最新的源代码:git://git.eclipse.org/gitroot/platform/eclipse.platform.releng.aggregator.
我发现许多门户网站都解释了类似的问题。但我想这是独特的情况。我在 spring mvc 应用程序中遇到错误。 org.springframework.beans.factory.Unsatisfied
我有下面的代码: package far.botshop.backend.controller; /** */ import java.util.logging.Logger; import far
我有 3 个数据源,设置如下: @Configuration @Component public class DataSourceConfig { @Bean("foo") @Conf
如何添加限定符来区分这两个 bean?我知道我需要使用 @Qualifier 注释,但我不确定如何将它添加到 bean 中,然后如何引用适当的 bean 创建 Autowiring 对象。 @Conf
@Override @Autowired(required = true) @Qualifier("hibernateCriteriaBuilder") public void setCriteria
我有 Circle 类: public class Circle { @Autowired @Qualifier("pointA") private Point center;
我在 Java Spring 环境中工作,并且在让 @Qualifier 工作时遇到了问题。我们项目的其他部分正在使用 @Inject 获取一个 bean,但我需要同一个 bean 的两个版本,看起来
“qualifier”的含义是什么?“qualifier”和“keyword”之间的区别是什么? 对于C语言中的volatile限定词,我们可以说volatile是一个关键字,那么“qualifier
我有一个简单的宏来设置一些常见的标题和格式: Dim colString(1 To 17, 1 To 2) As String Dim i As Integer colString(1, 1) = "
我正在将 Teradata 转换为 Hive(版本 0.10.0)。 Teradata 查询: QUALIFY ROW_NUMBER() OVER (PARTITION BY ADJSTMNT,SRC
我是在 PHP 中使用命名空间的新手,它们看起来很简单。然而,当我在一个包含类、接口(interface)和闭包的大文件的顶部添加一个命名空间时,代码完全停止工作。显然,某些元素没有正确限定。 例如:
我正在尝试使用属性占位符作为@Qualifier 的属性,如下所示: @Autowired @Qualifier("${beanName}") private MyBean myBean; 但是,这不
我是一名优秀的程序员,十分优秀!