名称顾问
public interface ISomeService {
public void doSome();
public String LJL();
}
public class SomeService implements ISomeService {
public void doSome() {
System.out.println("弱水三千只取一瓢饮,沧海万倾唯系一江潮");
}
public String LJL() {
return "ljl";
}
}
public class MyBeforeAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("--------------------前");
Object result=methodInvocation.proceed();
String temp=null;
if(result!=null){
temp=(String)result;
temp=temp.toUpperCase();
}
System.out.println("--------------------后");
return temp;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--目标对象-->
<bean id="someService" class="cn.advice01.SomeService"></bean>
<!--增强 通知-->
<bean id="beforeAdvice" class="cn.advice01.MyBeforeAdvice"></bean>
<!--增强 顾问-->
<bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="beforeAdvice"></property>
<property name="mappedName" value="doSome"></property>
</bean>
<!--aop-->
<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--指定目标对象-->
<property name="target" ref="someService"></property>
<!--做什么样的做强-->
<property name="interceptorNames" value="beforeAdvice"></property>
</bean>
</beans>
/*名称顾问*/
@Test
public void GW01() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("advice01.xml");
cn.advice01.ISomeService ss = (cn.advice01.ISomeService) ctx.getBean("someProxy");
ss.doSome();
String result = ss.LJL();
System.out.println(result);
}
正则顾问
public interface ISomeService { public void doSome(); public String LJL(); }
public class SomeService implements ISomeService { public void doSome() { System.out.println("弱水三千只取一瓢饮,沧海万倾唯系一江潮"); } public String LJL() { return "ljl"; } }
public class MyBeforeAdvice implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("--------------------前"); Object result=methodInvocation.proceed(); String temp=null; if(result!=null){ temp=(String)result; temp=temp.toUpperCase(); } System.out.println("--------------------后"); return temp; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--目标对象--> <bean id="someService" class="cn.advice02.SomeService"></bean> <!--增强 通知--> <bean id="beforeAdvice" class="cn.advice02.MyBeforeAdvice"></bean> <!--增强 顾问--> <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="beforeAdvice"></property> <property name="pattern" value=".*d.*"></property> </bean> <!--aop--> <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--指定目标对象--> <property name="target" ref="someService"></property> <!--做什么样的做强--> <property name="interceptorNames" value="beforeAdvice"></property> <!--业务类型有接口,Spring默认底层使用jdk代理,如不用jdk,强制没有接口的情况下使用cglib代理--> <property name="proxyTargetClass" value="true"></property> </bean> </beans>
/*正则顾问*/
@Test
public void GW02() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("advice02.xml");
cn.advice02.ISomeService ss = (cn.advice02.ISomeService) ctx.getBean("someProxy");
ss.doSome();
String result = ss.LJL();
System.out.println(result);
}