I get an error on a simple Spring project and I don't know the cause of this error -
No qualifying bean of type [services.ExportService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
我在一个简单的Spring项目上犯了一个错误,我不知道这个错误的原因——没有合格的类型(服务)。发现了依赖项:预期至少有1个bean可以作为这个依赖项的autowire候选项。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.ExportService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
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:410)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4937)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1551)
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.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:622)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:569)
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.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1454)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:74)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1295)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1387)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:818)
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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.ExportService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:440)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:418)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:546)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
My Controller class:
我的控制器类:
package com.springapp.mvc;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
public class HelloController {
@Resource
private services.ExportService exportService = null;
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello";
}
@RequestMapping(value="/export/data",produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody
public String doTheTest(final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
long start = System.currentTimeMillis();
this.exportService.export();
long end = System.currentTimeMillis();
String str = "Se realizeaza exportul...";
return str;
}
}
ExportJob:
ExportJob:
package domain;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Entity
@Table(name = "SIM_PORTAL_EXPORT_JOB")
@SequenceGenerator(name = "GENERATOR", sequenceName = "SIM_PORTAL_EXPORT_SEQ", allocationSize = 1)
public class ExportJob implements Serializable {
@Id
@Column(name = "JOB_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GENERATOR")
private Long id;
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
private Date dataInceput;
@Temporal(TemporalType.TIMESTAMP)
private Date dataFinal;
@Column(length = 2048)
private String error;
public String getError() {
return error;
}
public void setError(String value) {
this.error = value;
}
public Long getId() {
return id;
}
public void setId(Long value) {
this.id = value;
}
public Date getDataInceput() {
return dataInceput;
}
public void setDataInceput(Date value) {
dataInceput = value;
}
public Date getDataFinal() {
return dataFinal;
}
public void setDataFinal(Date value) {
dataFinal = value;
}
}
ExportServiceImpl:
ExportServiceImpl:
package services;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import domain.ExportJob;
import utils.Transactions;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Repository
public class ExportServiceImpl implements ExportService {
private static final Logger LOG = LoggerFactory.getLogger(Transactions.class);
@Resource
private ApplicationContext context = null;
@PersistenceContext
private EntityManager entityManager = null;
private ExportService proxy = null;
@PostConstruct
public void postConstruct() {
this.proxy = this.context.getBean(ExportService.class);
}
public void export() throws Exception {
Exception error = null;
ExportJob entry = this.proxy.updateLogEntryStart();
this.proxy.updateLogEntryEnd(entry, error);
}
@Transactional
public ExportJob updateLogEntryStart() {
ExportJob entry = new ExportJob();
entry.setDataInceput(new java.util.Date());
this.entityManager.persist(entry);
this.entityManager.flush();
return entry;
}
@Transactional
public void updateLogEntryEnd(ExportJob entry, Exception error) {
entry.setDataFinal(new java.util.Date());
entry.setError(StringUtils.abbreviate(error != null ? error.getClass() + ": " + error.getMessage() : null, 2048));
this.entityManager.merge(entry);
}
}
ExportService:
ExportService:
package services;
import domain.ExportJob;
import java.util.List;
public interface ExportService {
public void export() throws Exception;
ExportJob updateLogEntryStart();
void updateLogEntryEnd(ExportJob entry, Exception error);
}
mvc-dispatcher-servlet.xml
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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">
<context:component-scan base-package="com.springapp.mvc"/>
<context:component-scan base-package="services"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
UPDATE:
Error after modifying those:
错误后修改的:
@Service
public class ExportServiceImpl implements ExportService {
import services.ExportService;
......
@Resource
private ExportService exportService;
mvc-dispatcher-servlet:
mvc-dispatcher-servlet:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.springapp.mvc"/>
<context:component-scan base-package="services"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
error is still the same (Artifact goSpringMvc:war exploded: Error during artifact deployment):
错误仍然是相同的(工件goSpringMvc:war:工件部署期间的错误):
Dec 10, 2014 4:55:48 PM org.apache.catalina.core.ApplicationContext log
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.ExportService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
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:410)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4937)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1551)
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.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:622)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:569)
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.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1454)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:74)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1295)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1387)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:818)
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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.ExportService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:440)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:418)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:546)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
... 55 more
Dec 10, 2014 4:55:48 PM org.apache.catalina.core.ApplicationContext log
2 个解决方案
#1
1
You should mark your ExporterServiceImpl with @Service, by the docs
您应该将ExporterServiceImpl标记为@Service,由文档标记
The Resource annotation marks a resource that is needed by the application.
资源注释标记应用程序需要的资源。
and
和
If the annotation is applied to the component class, the annotation declares a resource that the application will look up at runtime.
如果将注释应用到组件类,则注释声明应用程序将在运行时查找的资源。
so when used on a class-level it doesn't specify the class to be a Spring bean (hence your error), rather just defers the look-up of a target resource to runtime
因此,在类级别上使用时,它不会指定类为Spring bean(因此会出现错误),而是将目标资源的查找延迟到运行时
UPDATE with respect to the comment
关于评论的更新
This is surely the issue with the code you've posted in the question. For what concerns not working after the changes, wouldn't know, but, will give you some pointers that are at least confusing.
这肯定是您在问题中发布的代码的问题。对于那些在更改之后不工作的关注点,我们不知道,但是,它会给您一些至少是令人困惑的指示。
You can consider to use @Autowire, you might check out the @Resource vs @Autowired, in short @Resource is not suppose to inject a dependency by type, but Spring enables this as a fallback. I would still advise to give @Autowired a try.
您可以考虑使用@Autowire,您可以查看@Resource vs @Autowired,简而言之,@Resource不是假定按类型注入依赖项,但Spring允许将其作为回退。我仍然建议尝试@Autowired一下。
I also notice that you don't have the <mvc:annotation-driven/>
in your mvc-dispatcher-servlet.xml, this is not causing your issue directly, but the handlers for controller mappings won't be created without it.
我还注意到,在您的mvc-dispatcher-servlet中没有
Finally, a suspicious, but not an important change is, you access your beans with a fully qualified name, e.g. services.ExporterService, and you initialize it to null. This should also not pose any problem, but its unconventional, you should simply add an import declaration and omit setting to null.
最后,一个值得怀疑但并不重要的更改是,您使用完全限定的名称(例如服务)访问bean。ExporterService,将其初始化为null。这也不应该造成任何问题,但是它是非常规的,您应该简单地添加一个导入声明并省略设置为null。
#2
1
Try using @Autowired and enabling .
尝试使用@Autowired和enable。
But besides that, just so you know by Spring's best practice, there are 3 layers:
除此之外,正如你所知,在Spring的最佳实践中,有三层:
@Controller, @Service, @Repository.
@ @ controller,@ service。
@Controller is obviously the controller, @Repository would be your DAO classes, in this instance you name your class "Service", but in reality you're using it as a DAO class, so it should actually get a @Repository annotation. @Service layer is where you'll do your conversions, business logic, caching, etc.
@Controller显然是控制器,@Repository是你的DAO类,在这个实例中你将你的类命名为“Service”,但实际上你把它用作DAO类,所以它实际上应该得到一个@Repository注解。@Service层是您进行转换、业务逻辑、缓存等的地方。
#1
1
You should mark your ExporterServiceImpl with @Service, by the docs
您应该将ExporterServiceImpl标记为@Service,由文档标记
The Resource annotation marks a resource that is needed by the application.
资源注释标记应用程序需要的资源。
and
和
If the annotation is applied to the component class, the annotation declares a resource that the application will look up at runtime.
如果将注释应用到组件类,则注释声明应用程序将在运行时查找的资源。
so when used on a class-level it doesn't specify the class to be a Spring bean (hence your error), rather just defers the look-up of a target resource to runtime
因此,在类级别上使用时,它不会指定类为Spring bean(因此会出现错误),而是将目标资源的查找延迟到运行时
UPDATE with respect to the comment
关于评论的更新
This is surely the issue with the code you've posted in the question. For what concerns not working after the changes, wouldn't know, but, will give you some pointers that are at least confusing.
这肯定是您在问题中发布的代码的问题。对于那些在更改之后不工作的关注点,我们不知道,但是,它会给您一些至少是令人困惑的指示。
You can consider to use @Autowire, you might check out the @Resource vs @Autowired, in short @Resource is not suppose to inject a dependency by type, but Spring enables this as a fallback. I would still advise to give @Autowired a try.
您可以考虑使用@Autowire,您可以查看@Resource vs @Autowired,简而言之,@Resource不是假定按类型注入依赖项,但Spring允许将其作为回退。我仍然建议尝试@Autowired一下。
I also notice that you don't have the <mvc:annotation-driven/>
in your mvc-dispatcher-servlet.xml, this is not causing your issue directly, but the handlers for controller mappings won't be created without it.
我还注意到,在您的mvc-dispatcher-servlet中没有
Finally, a suspicious, but not an important change is, you access your beans with a fully qualified name, e.g. services.ExporterService, and you initialize it to null. This should also not pose any problem, but its unconventional, you should simply add an import declaration and omit setting to null.
最后,一个值得怀疑但并不重要的更改是,您使用完全限定的名称(例如服务)访问bean。ExporterService,将其初始化为null。这也不应该造成任何问题,但是它是非常规的,您应该简单地添加一个导入声明并省略设置为null。
#2
1
Try using @Autowired and enabling .
尝试使用@Autowired和enable。
But besides that, just so you know by Spring's best practice, there are 3 layers:
除此之外,正如你所知,在Spring的最佳实践中,有三层:
@Controller, @Service, @Repository.
@ @ controller,@ service。
@Controller is obviously the controller, @Repository would be your DAO classes, in this instance you name your class "Service", but in reality you're using it as a DAO class, so it should actually get a @Repository annotation. @Service layer is where you'll do your conversions, business logic, caching, etc.
@Controller显然是控制器,@Repository是你的DAO类,在这个实例中你将你的类命名为“Service”,但实际上你把它用作DAO类,所以它实际上应该得到一个@Repository注解。@Service层是您进行转换、业务逻辑、缓存等的地方。