创建bean时出错
Error while creating bean
我已经为我的项目编写了一个邮件实用程序。现在,当我尝试
@Autowired
EmailManager emailManager
我从我的 UserController 收到错误消息,指出无法创建 bean。
在这里,我显示了 Class EmailManager
中编写的代码
@Component
public class EmailManager {
@Autowired
ApplicationContext appContext;
@Autowired
ThreadPoolTaskExecutor taskExecutor;
public Logger logger = Logger.getLogger(EmailManager.class);
public boolean sendChangePasswordNotification(String password,UserVO userVo) throws Exception{
try {
logger.info("Send change password notification to " + userVo.getEmailId());
String cc = PropertyUtil.getProperty("cc");
logger.info("cc:"+cc);
HashMap<String,String> replaceKeyValue = new HashMap<String,String>();
replaceKeyValue.put("username", userVo.getUsername());
replaceKeyValue.put("password", password);
replaceKeyValue.put("firstName", userVo.getFirstName());
HashMap<String,String> recipientMap = new HashMap<String,String>();
recipientMap.put("to", userVo.getEmailId());
recipientMap.put("cc", cc);
MailThread sender1 = (MailThread) appContext.getBean(MailThread.class);
sender1.setEmailOption(Constants.SEND_CHANGE_PASSWORD_NOTIFICATION);
sender1.setRecipientDetails(recipientMap);
sender1.setReplaceKeyValue(replaceKeyValue);
taskExecutor.execute(sender1);
logger.info("Sent change password notification to " + userVo.getEmailId());
} catch (Exception e) {
logger.error("Error", e);
throw e;
}
return true;
}
}
这是我在 eclipse 控制台上获得的完整堆栈跟踪:
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 'emailManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor com.isynergy.tasktrac.notification.EmailManager.taskExecutor; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] 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:326)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4772)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
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: org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor com.isynergy.tasktrac.notification.EmailManager.taskExecutor; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] 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:542)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:323)
... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] 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:1261)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
有什么建议可以解决这个问题吗?
无法创建 EmailManager,因为它已声明对 ThreadPoolTaskExecutor 的依赖关系,并且应用程序上下文中不存在该类型的适用 bean。您需要为应用程序上下文创建类型为 ThreadPoolTaskExecutor 的 bean。
我已经为我的项目编写了一个邮件实用程序。现在,当我尝试
@Autowired
EmailManager emailManager
我从我的 UserController 收到错误消息,指出无法创建 bean。 在这里,我显示了 Class EmailManager
中编写的代码@Component
public class EmailManager {
@Autowired
ApplicationContext appContext;
@Autowired
ThreadPoolTaskExecutor taskExecutor;
public Logger logger = Logger.getLogger(EmailManager.class);
public boolean sendChangePasswordNotification(String password,UserVO userVo) throws Exception{
try {
logger.info("Send change password notification to " + userVo.getEmailId());
String cc = PropertyUtil.getProperty("cc");
logger.info("cc:"+cc);
HashMap<String,String> replaceKeyValue = new HashMap<String,String>();
replaceKeyValue.put("username", userVo.getUsername());
replaceKeyValue.put("password", password);
replaceKeyValue.put("firstName", userVo.getFirstName());
HashMap<String,String> recipientMap = new HashMap<String,String>();
recipientMap.put("to", userVo.getEmailId());
recipientMap.put("cc", cc);
MailThread sender1 = (MailThread) appContext.getBean(MailThread.class);
sender1.setEmailOption(Constants.SEND_CHANGE_PASSWORD_NOTIFICATION);
sender1.setRecipientDetails(recipientMap);
sender1.setReplaceKeyValue(replaceKeyValue);
taskExecutor.execute(sender1);
logger.info("Sent change password notification to " + userVo.getEmailId());
} catch (Exception e) {
logger.error("Error", e);
throw e;
}
return true;
}
}
这是我在 eclipse 控制台上获得的完整堆栈跟踪:
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 'emailManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor com.isynergy.tasktrac.notification.EmailManager.taskExecutor; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] 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:326)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4772)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
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: org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor com.isynergy.tasktrac.notification.EmailManager.taskExecutor; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] 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:542)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:323)
... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] 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:1261)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
有什么建议可以解决这个问题吗?
无法创建 EmailManager,因为它已声明对 ThreadPoolTaskExecutor 的依赖关系,并且应用程序上下文中不存在该类型的适用 bean。您需要为应用程序上下文创建类型为 ThreadPoolTaskExecutor 的 bean。