spring 的aop,急
最近參照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裡的類似,
真的希望你們能幫我解決....
在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裡的類似,
真的希望你們能幫我解決....
相關文章
- Spring 的 AOPSpring
- Spring的AOPSpring
- 四、Spring的AOPSpring
- Spring 中的AOPSpring
- Spring AOPSpring
- spring AOPSpring
- [Spring]AOPSpring
- Spring原始碼-AOP(六)-AOP代理的建立Spring原始碼
- 死磕Spring之AOP篇 - Spring AOP總覽Spring
- Spring AOP APISSpringAPI
- spring-aopSpring
- Spring AOP IOCSpring
- [Spring之AOP]Spring
- Spring(四):AOPSpring
- Spring AOP (上)Spring
- Spring Boot整合Spring AopSpring Boot
- Spring 3.0 AOP 之 AOP 術語 (一)Spring
- Spring AOP的實現原理Spring
- Spring AOP的AspectJ註解Spring
- spring3.0.5的aop使用Spring
- 死磕Spring之AOP篇 - Spring AOP常見面試題Spring面試題
- Spring AOP學習筆記05:AOP失效的罪因Spring筆記
- Spring框架系列(10) - Spring AOP實現原理詳解之AOP代理的建立Spring框架
- Spring——AOP實現Spring
- Spring aop練手Spring
- Spring系列.AOP使用Spring
- Spring AOP 掃盲Spring
- Spring AOP總結Spring
- Spring AOP 詳解Spring
- spring ioc和aopSpring
- Spring AOP概念理解Spring
- Spring自學之路---AOPSpring
- Spring 面向切面AOPSpring
- Spring07:AOPSpring
- Spring AOP介紹Spring
- Spring AOP實踐Spring
- 4、spring核心AOPSpring
- Hello Spring-AOPSpring