Java 尝试使用@Autowired注释时出现org.springframework.beans.factory.BeanCreationException
我正在创建一个使用控制器、服务和Dao类的应用程序。服务和Dao被创建为接口,使用它们的实现类实现。我有一个名为UserGroupController的控制器类,其服务为UserGroupService。代码段应该是:Java 尝试使用@Autowired注释时出现org.springframework.beans.factory.BeanCreationException,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在创建一个使用控制器、服务和Dao类的应用程序。服务和Dao被创建为接口,使用它们的实现类实现。我有一个名为UserGroupController的控制器类,其服务为UserGroupService。代码段应该是: @Controller @RequestMapping("module/usergroup") public class UserGroupController { @Autowired UserGroupService userGroupService; 这
@Controller
@RequestMapping("module/usergroup")
public class UserGroupController
{
@Autowired
UserGroupService userGroupService;
这个@AutoWired注释导致了这个问题,当我只删除这个注释时,一切都会正常工作。但是如果我不使用这个注释,每当我尝试访问我的服务类的任何方法时,我都会得到一个空指针异常
我的服务级别如下所示:
@Repository("userGroupService")
public interface UserGroupService
其实施是:
public class UserGroupServiceImpl implements UserGroupService
{
@Autowired
@Qualifier(value = "userGroupDao")
UserGroupDao userGroupDao;
例外情况是:
严重:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例时发生异常
org.springframework.beans.factory.BeanCreationException:创建名为“userGroupController”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动连线字段:com.smartwcm.core.user.service.UserGroupService com.smartwcm.admin.user.controller.UserGroupController.UserGroupService;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项类型为[com.smartwcm.core.user.service.UserGroupService]的匹配bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowiredrequired=true}
位于org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValuesAutowiredAnnotationBeanPostProcessor.java:288
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBeanAbstractAutowireCapableBeanFactory.java:1120
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBeanAbstractAutowireCapableBeanFactory.java:522
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanAbstractAutowireCapableBeanFactory.java:461
位于org.springframework.beans.factory.support.AbstractBeanFactory$1.getObjectAbstractBeanFactory.java:295
位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingletonDefaultSingletonBeanRegistry.java:223
位于org.springframework.beans.factory.support.AbstractBeanFactory.doGetBeanAbstractBeanFactory.java:292
位于org.springframework.beans.factory.support.AbstractBeanFactory.getBeanAbstractBeanFactory.java:194
位于org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleTonsDefaultListableBeanFactory.java:607
位于org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitializationAbstractApplicationContext.java:932
位于org.springframework.context.support.AbstractApplicationContext.refreshAbstractApplicationContext.java:479
位于org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContextContextLoader.java:383
位于org.springframework.web.context.ContextLoader.initWebApplicationContextContextLoader.java:283
位于org.springframework.web.context.ContextLoaderListener.contextInitializedContextLoaderListener.java:112
位于org.apache.catalina.core.StandardContext.listenerStartStandardContext.java:4206
位于org.apache.catalina.core.StandardContext.startStandardContext.java:4705
位于org.apache.catalina.core.ContainerBase.startContainerBase.java:1057
位于org.apache.catalina.core.StandardHost.startStandardHost.java:840
位于org.apache.catalina.core.ContainerBase.startContainerBase.java:1057
在org.apache.catalina.core.StandardEngine.startStandardEngine.java:463
位于org.apache.catalina.core.StandardService.startStandardService.java:525
位于org.apache.catalina.core.StandardServer.startStandardServer.java:754
位于org.apache.catalina.startup.catalina.startCatalina.java:595
在sun.reflect.NativeMethodAccessorImpl.invoke0Native方法中
位于sun.reflect.NativeMethodAccessorImpl.invokeNativeMethodAccessorImpl.java:39
在sun.reflect.DelegatingMethodAccessorImpl.invokeDelegatingMethodAccessorImpl.java:25
位于java.lang.reflect.Method.invokeMethod.java:597
位于org.apache.catalina.startup.Bootstrap.startBootstrap.java:289
位于org.apache.catalina.startup.Bootstrap.mainBootstrap.java:414
原因:org.springframework.beans.factory.BeanCreationException:无法自动连线字段:com.smartwcm.core.user.service.UserGroupService com.smartwcm.admin.user.controller.UserGroupController.UserGroupService;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项类型为[com.smartwcm.core.user.service.UserGroupService]的匹配bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖注释
离子:{@org.springframework.beans.factory.annotation.Autowiredrequired=true}
位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor$AutoWiredFeldElement.InjectAutoWiredNotationBeanPostProcessor.java:514
位于org.springframework.beans.factory.annotation.InjectionMetadata.InjectionMetadata.java:87
位于org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValuesAutowiredAnnotationBeanPostProcessor.java:285
... 28多
原因:org.springframework.beans.factory.NoSuchBean定义异常:未找到依赖项类型为[com.smartwcm.core.user.service.UserGroupService]的匹配bean:应至少有1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowiredrequired=true}
位于org.springframework.beans.factory.support.DefaultListableBeanFactory.RaiseNouchBeanDefinitionExceptionDefaultListableBeanFactory.java:949
位于org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependencyDefaultListableBeanFactory.java:818
位于org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependencyDefaultListableBeanFactory.java:730
位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor$AutoWiredFeldElement.InjectAutoWiredNotationBeanPostProcessor.java:486
... 30多
您需要将@Service注释添加到您的服务类实现中,这样就可以通过类路径扫描获取它,并将其添加到Spring上下文中,然后就可以进行自动连接
package com.foo.bar.service;
@Service
public class UserGroupServiceImpl implements UserGroupService
{
@Autowired
@Qualifier(value = "userGroupDao")
UserGroupDao userGroupDao;
另外,如果您的服务类位于包com.foo.bar.service中,请确保该包包含在XML中的组件扫描包标记中。例如:
为什么要用@Repository注释它?您可以将其用于DAO类
1改为使用@Service注释
2或者您可以在Spring配置中将UserGroupService定义为bean:
<bean id="userGroupService" class="com.your.package.UsergroupService" />
如果将@Repository注释移动到实现类中,它将起作用。请在下面找到示例代码: 接口类: 实现类: 配置类: 主要类别: 如果我将@Repository注释移动到接口,spring不会抛出任何名为“testService”的bean 注意:实际上应该用@service而不是@Repository注释服务类。@Autowired有时会让人困惑。您可以使用更通用的java@Resource jsr250注释@资源意味着按名称获取已知资源。名称从带注释的setter或字段的名称中提取,或从name参数中提取@资源有一个内置的回退,当按名称解析失败时,该回退将生效。在本例中,它返回到@Autowired类型解析
@Controller
@RequestMapping("module/usergroup")
public class UserGroupController
{
@Resource(name="UserGroupService")
UserGroupService userGroupService;
对于您的服务,您可以从@Component或@service中进行选择
希望它能解决你的问题。请查看@Autowired和@Resource之间的区别,以了解更多信息
@服务或@Component注释用于spring容器,它将动态创建对象。如果您使用@Resource,您甚至可以选择退出上面的注释,前提是您在服务类的每个位置都使用@Resource您应该遵循标准的存储库、服务、控制器模式 @控制器:控制器注释用于表示类是控制器。它包含服务于http请求的方法,请求映射在方法级别或类级别完成。 @服务:用@Service注释所有服务类。您的所有业务逻辑都将在服务类中。控制器层和DAO层中没有任何业务逻辑。 @Repository:用@Repository注释所有DAO类。所有数据库访问逻辑都应该在DAO类中 因此,在您的情况下,注释应该是:
@Repository("userGroupDao")
public class UserGroupDaoImpl implements UserGroupDao
@Service("userGroupService")
public class UserGroupServiceImpl implements UserGroupService
@Controller
public class UserGroupController
好啊那么你得到的BeanCreationException是什么呢?没有这些,我们真的帮不上忙。如果您删除@Autowired,那么您的userGroupService将为null,因为不会向其中注入任何内容异常消息非常清楚:没有与您请求的注入相匹配的bean可用。弄清楚为什么没有包括这个bean。你应该声明你的usergroupserviceinmpl,你也不需要在@Service或@Repository注释后面加任何内容来限定它们。只要它们是通过类路径扫描获取的,它们就有资格通过类型自动连线谢谢,这正是我所寻找的解决方案……该解决方案对我的应用程序有效,而且也很有教育意义
@Repository("testService")
public class TestImpl implements Test {
@Override
public void printMessage(String msg) {
System.out.println(msg);
}
}
@Configuration
@ComponentScan("com.praxissummit.eperf.api.domain.test")
public class TestConfig {
}
public class Main {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(TestConfig.class);
ctx.getBean("testService", Test.class).printMessage("Hello world!!");
}
}
@Controller
@RequestMapping("module/usergroup")
public class UserGroupController
{
@Resource(name="UserGroupService")
UserGroupService userGroupService;
@Service
public interface UserGroupService { }
@Repository("userGroupDao")
public class UserGroupDaoImpl implements UserGroupDao
@Service("userGroupService")
public class UserGroupServiceImpl implements UserGroupService
@Controller
public class UserGroupController