我正在创建JavaMailSender的bean类并自动装配javamailsender但我收到错误
package com.websystique.springsecurity.service;
import java.util.List;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.websystique.springsecurity.dao.EmployeeDao;
import com.websystique.springsecurity.model.Employee;
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDao employeeDAOImpl;
Employee Employee;
@Autowired
private JavaMailSender mailSender;
@Autowired
private SimpleMailMessage simpleMailMessage;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
@Transactional
public void addEmployee(Employee employee) {
employeeDAOImpl.addEmployee(employee);
StringBuilder subject = new StringBuilder();
subject.append("User ");
subject.append(employee.getFirstname());
subject.append(" ");
subject.append(employee.getLastname());
subject.append(" has been added");
String content = subject.toString()+" Successfully!";
sendMail("", content,subject.toString(),employee.getEmail(),"D:\\log.pdf");
}
@Transactional
public List<Employee> getAllEmployees() {
return employeeDAOImpl.getAllEmployees();
}
@Transactional
public void deleteEmployee(Integer employeeId) {
employeeDAOImpl.deleteEmployee(employeeId);
}
@Transactional
public Employee getEmployee(int empid) {
return employeeDAOImpl.getEmployee(empid);
}
@Transactional
public Employee updateEmployee(Employee employee) {
Employee emp = employeeDAOImpl.updateEmployee(employee);
StringBuilder subject = new StringBuilder();
subject.append("User ");
subject.append(emp.getFirstname());
subject.append(" ");
subject.append(emp.getLastname());
subject.append(" has been updated");
String content = subject.toString()+" Successfully!";
sendMail("", content,subject.toString(),emp.getEmail(),"D:\\log.pdf");
return emp;
}
@Transactional
public void setEmployeeDAO(EmployeeDao employeeDAO) {
this.employeeDAOImpl = employeeDAO;
}
@Transactional
public void sendMail(String dear, String content,String subject,String
toEmail,String filePath) {
MimeMessage message = mailSender.createMimeMessage();
try{
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(new InternetAddress("saranpartneritech@gmail.com"));
helper.setTo(new InternetAddress(toEmail));
helper.setSubject(subject);
helper.setText(content);
if(filePath!=null){
FileSystemResource file = new FileSystemResource(filePath);
helper.addAttachment(file.getFilename(), file);
}
}catch (MessagingException e) {
throw new MailParseException(e);
}
mailSender.send(message);
}
}
我无法为bean id =&#34; mailSender&#34;创建bean类。我正在为它工作两天,请让我离开它。
<context:component-scan base-
package="com.websystique.springsecurity.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionService
FactoryBean">
<property name="converters">
<list>
<bean id="roleToUserProfile"
class="com.websystique.springsecurity.util.RoleToUserProfileConverter" />
</list>
</property>
</bean>
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="saranpartneritech@gmail.com" />
<property name="password" value="Saran2595" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<!--
<bean id="mailMail" class="com.mkyong.common.MailMail">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean>
<bean id="customeMailMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="saranpartneritech@gmail.com" />
<property name="subject" value="Customer Report" />
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean> -->
</beans>
错误页面
org.springframework.beans.factory.BeanCreationException:错误 使用名称&#39; employeeController创建bean&#39 ;:注入自动装配 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:私有 com.websystique.springsecurity.service.EmployeeService com.websystique.springsecurity.controller.EmployeeController.employeeServiceImpl; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 使用名称&#39; employeeServiceImpl创建bean&#39 ;:注入自动装配 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:私有 org.springframework.mail.javamail.JavaMailSender com.websystique.springsecurity.service.EmployeeServiceImpl.mailSender; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 合格的bean类型 找到[org.springframework.mail.javamail.JavaMailSender] 依赖:预计至少有1个bean有资格成为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) 在 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446) 在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328) 在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853) 在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) 在 org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1408) 在 org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1398) 在java.util.concurrent.FutureTask.run(FutureTask.java:266)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617) 在java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
您的组件扫描仅包含com.websystique.springsecurity.controller。因此,它将无法扫描其他包以注入依赖项。在顶层软件包(com.websystique.springsecurity)上启用组件扫描,并在xml文件中覆盖bean definations(如果需要)。
答案 1 :(得分:0)
在你的xml中尝试这个,如果它可以恢复