package com.spring.chapterEleven; public interface AddLoginMessage { public void setMessage(String message); public String getMessage(); }
package com.spring.chapterEleven; import org.springframework.aop.support.DelegatingIntroductionInterceptor; public class LoginIntroductionAdvice extends DelegatingIntroductionInterceptor implements AddLoginMessage { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
配置
<!-- Introduction通知的配置 --> <bean id="loginIntroductionAdvice" class="LoginIntroductionAdvice"></bean> <!-- Introduction配置代理类的时候和其他通知有所不同 --> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 增加了proxyTargetClass的属性,值设为true --> <property name="proxyTargetClass"> <value>true</value> </property> <!-- 增加了singleton的属性,值设为FALSE --> <property name="singleton"> <value>flase</value> </property> <!-- proxyInterfaces属性改为目标类要实现的接口 --> <property name="proxyInterfaces"> <value>com.spring.AddLoginMessage</value> </property> <property name="target"> <ref local="customerLogin"/> </property> <!-- interceptorNames属性改为Introduction通知对应的bean --> <property name="interceptorNames"> <value>loginIntroductionAdvice</value> </property> </bean>
测试
public static void main(String[] args) throws SQLException, LoginException{ ApplicationContext atx = new FileSystemXmlApplicationContext("WEB-INF/applicationContext.xml"); AddLoginMessage addmsg =(AddLoginMessage) atx.getBean("proxy"); addmsg.setMessage("this is a login test"); System.out.println(addmsg.getMessage()); }