Java Spring自动连线异常(无法自动连线字段..)
在Java Spring自动连线异常(无法自动连线字段..),java,spring,autowired,Java,Spring,Autowired,在RegistrationService,@autowiredRegistrationDAO和registerCustomer正常工作。 但它不能自动连线激活mailservice 我没有发现问题。当我在RegistrationService中删除ActivationMailService时,它会起作用。有人能告诉我我的代码有什么问题吗 我已将该项目上载到 我在运行应用程序时遇到以下错误: Hibernate: drop table if exists Customer ERROR: org.s
RegistrationService
,@autowiredRegistrationDAO
和registerCustomer
正常工作。
但它不能自动连线激活mailservice
我没有发现问题。当我在RegistrationService
中删除ActivationMailService
时,它会起作用。有人能告诉我我的代码有什么问题吗
我已将该项目上载到
我在运行应用程序时遇到以下错误:
Hibernate: drop table if exists Customer
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'activationMailServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.game.portal.utils.SendMail com.game.portal.services.impl.ActivationMailServiceImpl.sendMail; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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:289)
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:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
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$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.game.portal.utils.SendMail com.game.portal.services.impl.ActivationMailServiceImpl.sendMail; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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:517)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
... 22 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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:289)
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.findAutowireCandidates(DefaultListableBeanFactory.java:912)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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:517)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
... 35 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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: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.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
... 37 more
Jan 14, 2014 11:45:14 AM org.apache.catalina.core.StandardContext listenerStart
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 'activationMailServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.game.portal.utils.SendMail com.game.portal.services.impl.ActivationMailServiceImpl.sendMail; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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:289)
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:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
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$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.game.portal.utils.SendMail com.game.portal.services.impl.ActivationMailServiceImpl.sendMail; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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:517)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
... 22 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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:289)
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.findAutowireCandidates(DefaultListableBeanFactory.java:912)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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:517)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
... 35 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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: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.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
... 37 more
管理bean:
package com.game.portal.managedbeans;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import com.game.portal.entities.Customer;
import com.game.portal.services.RegistrationService;
@ViewScoped
@ManagedBean
public class CustomerBean implements Serializable {
private static final long serialVersionUID = 7291708796066664438L;
@ManagedProperty(value="#{registrationService}")
RegistrationService registrationService;
Customer customer = new Customer();
public void registerCustomer(){
customer.setName("xxx");
customer.setSurname("xxx");
customer.setUsername("xxx");
customer.setEmail("xxx@yahoo.com");
customer.setPassword("xxxx");
registrationService.registerCustomer(customer);
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public RegistrationService getRegistrationService() {
return registrationService;
}
public void setRegistrationService(RegistrationService registrationService) {
this.registrationService = registrationService;
}
}
这里是注册服务
package com.game.portal.services;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.portal.dao.RegistrationDao;
import com.game.portal.entities.Company;
import com.game.portal.entities.Customer;
@Service
public class RegistrationService {
@Autowired
RegistrationDao registrationDao;
@Autowired
ActivationMailService activationMailService;
public void registerCustomer(Customer customer) {
System.out.println("RegistrationUserBean:: Registering user " + customer.getName() + " " + customer.getUsername() + ", with email " + customer.getEmail());
registrationDao.registerCustomer(customer);
activationMailService.sendActivationMail(customer);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration success", "success");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void registerCompany(Company company) {
System.out.println("RegistrationUserBean:: Registering user " + company.getName() + " " + company.getUsername() + ", with email " + company.getEmail());
registrationDao.registerCompany(company);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration success", "success");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
ActivationMailService
package com.game.portal.services;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.portal.dao.RegistrationDao;
import com.game.portal.entities.Company;
import com.game.portal.entities.Customer;
@Service
public class RegistrationService {
@Autowired
RegistrationDao registrationDao;
@Autowired
ActivationMailService activationMailService;
public void registerCustomer(Customer customer) {
System.out.println("RegistrationUserBean:: Registering user " + customer.getName() + " " + customer.getUsername() + ", with email " + customer.getEmail());
registrationDao.registerCustomer(customer);
activationMailService.sendActivationMail(customer);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration success", "success");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void registerCompany(Company company) {
System.out.println("RegistrationUserBean:: Registering user " + company.getName() + " " + company.getUsername() + ", with email " + company.getEmail());
registrationDao.registerCompany(company);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration success", "success");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
注意:我在SendMail
、HashMap
和EmailTemplateService
上测试了如何删除@Autowired
。它起作用了。在激活设备
上的@Autowired
没有问题。真奇怪
package com.game.portal.services;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.portal.entities.Customer;
import com.game.portal.utils.SendMail;
@Service
public class ActivationMailService {
@Autowired
protected ActivationCodeService activationCodeService;
@Autowired
protected SendMail sendMail;
@Autowired
protected HashMap<String, String> emailMap;
@Autowired
private EmailTemplateService mailTemplateService;
protected static final Logger logger = Logger.getLogger("ActivationMailService");
public void sendActivationMail(Customer customer) {
try {
sendMail.send("test", "test", customer.getEmail(), createActivationMailMessage(customer));
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
}
}
private String createActivationMailMessage(Customer customer) throws UnsupportedEncodingException {
String emailTemplate = mailTemplateService.generateTemplate("activation", emailMap.get("domain_name"));
return emailTemplate;
}
}
根据例外情况:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl]
春天似乎找不到可以注射的豆子
@Autowired
JavaMailSenderImpl mailSender;
在SendMail
类中
您确定定义了类JavaMailSenderImpl
的bean吗
顺便说一句,直接注入实现类(而不是它的接口)有点不寻常。服务类中的方法应该在您的自动连线类中注释为@Transactionalwrite@Service(“sendMail”)您可能希望将日志级别设置为DEBUG或更低,以找出Spring不创建bean的原因。我也没有看到
import org.springframework.stereotype.Service代码>在ActivationMailService
中,但您确实使用了该注释。你只在这篇文章里加了这个吗?如果是这样的话,那就是它不起作用的原因。我认为这不是一个简单的概念。如果没有完整的跟踪,是不可能知道的。但是,在ActivationService内的HashMap上有一个可疑的@Autowired注释。该自动连线注释的行为很可能与您预期的不同。这个定义在哪里?@Giovanni,这是复制粘贴错误。我添加了import org.springframework.stereotype.Service。这不是错误的原因。@Hyness,我明天将发布完整的stactrace。我在您的上下文文件中没有看到任何JavaMailSenderImpl
bean定义。我更改了它,但仍然是相同的错误@自动连线JavaMailSender@用户1662738但您是否检查了是否定义了实现JavaMailSender
的bean?我在你发布的代码中看不到任何内容。只是有一个类似的问题-这是因为我试图自动连接实现,而不是接口。我认为使用@Transactional annotation Spring会创建代理,这样您就不会一直拥有您认为拥有的代理。切换到自动连接接口为我解决了这个问题。我刚刚遇到了一个类似的问题(注入实现而不是接口),而你最终发现了我的错误。非常感谢。不知道我怎么会看不到这一点。如果这个类已经用@Transactional注释了,那么对于方法来说并不重要。