我正在创建一个使用Controller,Service和Dao类的应用程序.Service和Dao被创建为使用其实现类实现的接口。我有一个名为UserGroupController的控制器类,其服务为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;
例外是:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userGroupController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.smartwcm.core.user.service.UserGroupService com.smartwcm.admin.user.controller.UserGroupController.userGroupService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.smartwcm.core.user.service.UserGroupService] 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:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.smartwcm.core.user.service.UserGroupService com.smartwcm.admin.user.controller.UserGroupController.userGroupService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.smartwcm.core.user.service.UserGroupService] 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:514)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 28 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.smartwcm.core.user.service.UserGroupService] 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:949)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 30 more
答案 0 :(得分:1)
您应该遵循标准的存储库,服务,控制器模式。
@Controller :控制器注释用于表示该类是控制器。它包含为http请求提供服务的方法,请求映射在方法级别或类级别完成。
@Service :使用@Service注释所有服务类。您的所有业务逻辑都将位于服务类中。 Controller和DAO层在那里没有任何业务逻辑。
@Repository :使用@Repository注释所有DAO类。您的所有数据库访问逻辑都应该在DAO类中。
所以在你的情况下,注释应该是:
@Repository("userGroupDao")
public class UserGroupDaoImpl implements UserGroupDao
@Service("userGroupService")
public class UserGroupServiceImpl implements UserGroupService
@Controller
public class UserGroupController
答案 1 :(得分:0)
为何使用@Repository
对其进行注释?您可以将它用于DAO课程。
(1)使用 @Service
进行注释
@Service
public interface UserGroupService
(2)或者您可以在Spring配置中将UserGroupService定义为bean:
<bean id="userGroupService" class="com.your.package.UsergroupService" />
答案 2 :(得分:0)
如果将@Repository
注释移动到实现类,它将起作用。请在下面找到示例代码:
接口类:
public interface Test {
public void printMessage(String msg);
}
实施班级:
@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!!");
}
}
如果我将@Repository
注释移动到界面,则弹出No bean named 'testService' is defined
注意:您实际上应该使用@Service
注释您的服务类,而不是@Repository
答案 3 :(得分:0)
@Autowired
有时令人困惑。您可以使用更通用的java @Resource (jsr250)
注释。 @Resource
表示按名称为我提供已知资源。该名称是从带注释的setter或字段的名称中提取的,或者是从name-Parameter中获取的。 @Resource
有一个内置的回退功能,当解析按名称失败时,它会启动。在这种情况下,它会按类型回退到@Autowired
- 类型的解决方案。
@Controller
@RequestMapping("module/usergroup")
public class UserGroupController
{
@Resource(name="UserGroupService")
UserGroupService userGroupService;
对于您的服务,您可以选择@Component
或@Service
@Service
public interface UserGroupService { }
希望它能解决你的问题。查看@Autowired
和@Resource
之间的区别,以便更清晰。 @Resource vs @Autowired
@Service
或@Component
注释用于spring容器,它将动态创建对象。如果您使用@Resource
,则甚至可以选择退出上述注释(假设您在服务类的每个位置使用@Resource
)
答案 4 :(得分:-1)
您需要将@Service
注释添加到服务类实现中,以便可以通过类路径扫描获取它,添加到Spring上下文中,然后才有资格成为自动装配。
package com.foo.bar.service;
@Service
public class UserGroupServiceImpl implements UserGroupService
{
@Autowired
@Qualifier(value = "userGroupDao")
UserGroupDao userGroupDao;
还要确保如果您的服务类位于包com.foo.bar.service
中,那么您的XML包含在组件扫描包标记中。例如:
<context:component-scan base-package="com.foo.bar" />