spring 的aop,急

xingyuanjian發表於2008-05-27
最近參照spring自帶的jpetstore列子,試試那關於面向切面程式設計,而我想做的就是,在沒個使用者註冊成功之後,系統根據applicationContext.xml裡的配置自動發郵件給使用者註冊的郵箱中。
在applicationContext.xml中的配置如下:

<aop:config>
<aop:advisor pointcut="execution(* *..longHuaFacade.*(..))" advice-ref="txAdvice"/>
<aop:advisor pointcut="execution(* *..longHuaFacade.saveUser(*..user))" advice-ref="emailAdvice"/>
</aop:config>

<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<bean id="emailAdvice" class="com.longHua.domain.logic.SendRegConfirmationEmailAdvice">
<property name="mailSender" ref="mailSender"/>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"/>
</bean>
那個門面介面程式碼如下:
public interface longHuaFacade {
void saveUser(UserReg user);
void delete(int id);
UserReg getUser(int id);
List<UserReg> getUsers();
List<UserReg> checkRegName(String regName);
List<UserReg> checkRegEmail(String regEmail);
}



當註冊成功之後,我發現我設定的根本就沒點反應,,不知道是不是我配錯了,
SendRegConfirmationEmailAdvice裡的程式碼如下:

package com.longHua.domain.logic;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import com.longHua.domain.UserReg;


public class SendRegConfirmationEmailAdvice implements AfterReturningAdvice, InitializingBean {

private static final String DEFAULT_MAIL_FROM = "xingyuanjian@sina.com";

private static final String DEFAULT_SUBJECT = "歡迎你註冊——龍華電子商務!";

private final Log logger = LogFactory.getLog(getClass());

private MailSender mailSender;

private String mailFrom = DEFAULT_MAIL_FROM;

private String subject = DEFAULT_SUBJECT;

public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}

public void setMailFrom(String mailFrom) {
this.mailFrom = mailFrom;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void afterPropertiesSet() throws Exception {
if (this.mailSender == null) {
throw new IllegalStateException("mailSender is required");
}
}

public void afterReturning(Object returnValue, Method m, Object[] args, Object target) throws Throwable {
UserReg userReg = (UserReg) args[0];
if (userReg.getUserMail() == null || userReg.getUserMail().length() == 0) {
return;
}

StringBuffer text = new StringBuffer();
text.append(userReg.getUserName()).append("你好");
text.append(",歡迎你在我們龍華電子商務註冊,請確認你註冊的資訊,你的使用者名稱是: ").append(userReg.getUserName()).append("<br>");
text.append("你的登陸密碼是:").append(userReg.getUserPwd());

SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(userReg.getUserMail());
mailMessage.setFrom(this.mailFrom);
mailMessage.setSubject(this.subject);
mailMessage.setText(text.toString());
try {
this.mailSender.send(mailMessage);
}
catch (MailException ex) {
logger.warn("An exception occured when trying to send email", ex);
}
}

}


其實所有的程式碼和jpetstore裡的類似,

真的希望你們能幫我解決....

相關文章