wang5598
飘逸的ITER
2012-01-05 10:18

Spring 注入为空

  • spring

在一个SSH的项目中遇到一个问题,我有一个dao的接口BaseJdbcDao,和一个dao的实现类BaseJdbcDaoImpl,实现类已经通过注解提交给容器进行管理,然后有一个工具类MailHelper想通过自动装配载入dao的实现类,可是每次都没有装配成功,代码如下,请教各位:
[code="java"]
package base;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.support.rowset.SqlRowSet;
public interface BaseJdbcDao {

public abstract List queryForList(String sql);

public abstract int queryForInt(String sql);

public abstract SqlRowSet queryForRowSet(String sql);

public abstract int update(String sql);

public abstract Map queryForMap(String sql);

public abstract void delete(String sql);

public abstract void insert(String sql);

}
[/code]
[code="java"]
package base.impl;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import base.BaseJdbcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("basejdbcdao")
public class BaseJdbcDaoImpl implements BaseJdbcDao {

private  JdbcTemplate jdbcTemplate;

public  JdbcTemplate getJdbcTemplate() {
    return jdbcTemplate;
}

@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

public List queryForList(String sql){
    System.out.println(sql);
       return this.getJdbcTemplate().queryForList(sql);
    }

public int queryForInt(String sql){
    System.out.println(sql);
    return this.getJdbcTemplate().queryForInt(sql);
}

public SqlRowSet queryForRowSet(String sql){
    System.out.println(sql);
    return this.getJdbcTemplate().queryForRowSet(sql);
}

public int update(String sql){
    System.out.println(sql);
    return this.getJdbcTemplate().update(sql);
}

public Map queryForMap(String sql){
    System.out.println(sql);
    return this.getJdbcTemplate().queryForMap(sql);
}

public void delete(String sql){
    System.out.println(sql);
    this.getJdbcTemplate().update(sql);
}

public void insert(String sql){
    System.out.println(sql);
    this.getJdbcTemplate().execute(sql);
}

}
[/code]
[code="java"]package util;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import java.util.Date;
import javax.activation.DataHandler;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import util.ByteArrayDataSource;
import javax.mail.PasswordAuthentication;
import base.BaseJdbcDao;
import base.impl.BaseJdbcDaoImpl;

public class MailHelper {

@Autowired
@Qualifier("basejdbcdao")
private BaseJdbcDao dao;

private String email;
private String mobile;
private String school;
private String name;
private String remark;
private String appstr; 

public MailHelper(String school,String name,String mobile,String email,String remark,String appstr){
    this.email =  email;
    this.mobile = mobile;
    this.school = school;
    this.name = name;
    this.remark = remark;
    this.appstr = appstr;
}

public MailHelper(){};

public void sendMail(){
   this.insertData(school,name,mobile,email,remark,appstr);
}



/**
 * 帐号申请的功能
 * @param template
 * @param pschool
 * @param pname
 * @param pmobile
 * @param pemail
 * @param premark
 */
public void insertData(String pschool,String pname,String pmobile,String pemail,String premark,String appstr){
    String email = changeContent( pemail);
    String mobile = changeContent( pmobile);
    String school =changeContent (pschool);
    String name =changeContent (pname);
    String remark = changeContent (premark);
    String to  = "test@163.com";
    String subject = "申请";
    StringBuilder sb = new StringBuilder("insert into maillist(linkman,school,mobile,mail,remark)values('"+name+"','"+school+"','"+mobile+"','"+email+"','"+remark+"')");
    dao.update(sb.toString());
    remark = "内容略。。";
    this.send(  to, subject, remark);
}

/**
 * Sending a mail with given values.<br>
 * content parameter means the main email text,it refers to a html file. 
 * 
 * @param smtpServer
 * @param to
 * @param from
 * @param subject
 * @param content
 * @param emailUsername
 * @param emailUserpass
 * @author WYQ
 */ 
private String send(String to,String subject,String content) {
    try {
        HashMap<String,String> map = readParamters();
        String smtpserver = map.get("smtpserver");
        String publicaccount = map.get("publicaccount");
        String publicpwd = map.get("publicpwd");
        String from = map.get("publicmail");

        Properties props = System.getProperties();
        props.put("mail.smtp.port", "25");
        props.put("mail.smtp.starttls.enable","false");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", smtpserver);

        Session session = null;
        session = Session.getDefaultInstance(props,new PasswordAuthenticator(publicaccount,publicpwd));

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(  to, false));
        msg.setSubject(subject);
        msg.setDataHandler(new DataHandler(new ByteArrayDataSource(content,"text/html; charset=\"utf-8\"")));
        msg.setHeader("X-Mailer", "XML-Mail");
        msg.setSentDate(new Date());
        // -- Send the message --
        Transport.send(msg);
        return "success";
    } catch (Exception ex) {
        return "Error" + ex;
    }
}


private HashMap<String,String> readParamters(){
    HashMap<String,String> map = new HashMap<String,String>();
    Properties properties =new Properties();
    try {
           properties.load(MailHelper.class.getClassLoader().getResourceAsStream("mail.properties"));
           map.put("smtpserver",   properties.getProperty("smtpserver"));
           map.put("publicmail",   properties.getProperty("publicmail"));
           map.put("publicaccount",   properties.getProperty("publicaccount"));
           map.put("publicpwd",   properties.getProperty("publicpwd"));
          } catch (IOException e) {
           e.printStackTrace();
     }
      return map;
}

}

class PasswordAuthenticator extends Authenticator {
private String username;
private String password;

public PasswordAuthenticator(String username, String password) {
    this.username = username;
    this.password = password;
}
protected PasswordAuthentication  getPasswordAuthentication() {
    return new PasswordAuthentication (username, password);
}

}

[/code]
然后我在struts的action类中调用MailHelper的send方法进行邮件发送时,打印的dao总是为空,action的代码是:
[code="java"]
/**
* 帐号申请-邮件发送
* @return
*/
public String sendMail(){
MailHelper mh = new MailHelper();
mh.sendMail();
success = true;
return SUCCESS;
}
[/code]
spring的自动扫描和注解配置已经开启,也已经把dao的package路径和MailHelper的package路径加入到基类中了,
[code="java"]

[/code]
现在就是弄不明白,为什么通过new出来的MailHelper对象中注入的dao总是为空呢。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换