您的位置:首页 > 编程语言 > Java开发

Spring-------- 增强(前置,后置,异常,环绕)

2017-08-02 13:43 375 查看
spring的增强分类:前置增强,后置增强,异常增强,环绕增强。

增强的意义在于我们不改变接口,实现类,不动方法的前提下在原内容上增加内容。

首先我们看一下前置增强:前置增强必须实现的接口:MethodBeforeAdvice

[java] view
plain copy

print?

MethodBeforeAdvice

[java] view
plain copy

print?

package cn.happy.spring11;

/**

* Created by lihuohuo on 2017/7/31.

*/

public interface IDoSome {

public void add();

public void add1();

public void add2();

}

[java] view
plain copy

print?

package cn.happy.spring11;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class IDoSomes implements IDoSome{

public void add() {

System.out.println("=========1=======");

}

public void add1() {

System.out.println("=========2=======");

}public void add2() {

System.out.println("=========3=======");

}

}

[java] view
plain copy

print?

package cn.happy.spring11;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class MyBeforeAdvice implements MethodBeforeAdvice{

public void before(Method method, Object[] objects, Object o) throws Throwable {

System.out.println("=========before=======");

}

}

[java] view
plain copy

print?

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

<bean id="Idosome" class="cn.happy.spring11.IDoSomes"></bean>

<bean id="beforeAdvice" class="cn.happy.spring11.MyBeforeAdvice"></bean>

<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="Idosome"></property>

<property name="interceptorNames" value="beforeAdvice"></property>

</bean>

</beans>

[java] view
plain copy

print?

@Test

public void test01(){

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext07.xml");

IDoSome i=(IDoSome)ctx.getBean("someProxy");

i.add();

i.add1();

i.add2();

}

下面是后置增强:实现的接口是:AfterReturningAdvice

[java] view
plain copy

print?

package cn.happy.spring12;

/**

* Created by lihuohuo on 2017/7/31.

*/

public interface IDoSome {

public void add();

public String add1();

public void add2();

}

[java] view
plain copy

print?

package cn.happy.spring12;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class IDoSomes implements IDoSome {

public void add() {

System.out.println("=========1=======");

}

public String add1() {

System.out.println("=========2=======");

return "呵呵abc";

}

public void add2() {

System.out.println("=========3=======");

}

}

[java] view
plain copy

print?

package cn.happy.spring12;

import org.springframework.aop.AfterReturningAdvice;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class MyAfterAdvice implements AfterReturningAdvice{

public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {

System.out.println("========after=======");

if(o!=null){

System.out.println(o.toString().toUpperCase());

}

}

}

[java] view
plain copy

print?

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

<bean id="Idosome" class="cn.happy.spring12.IDoSomes"></bean>

<bean id="afterAdvice" class="cn.happy.spring12.MyAfterAdvice"></bean>

<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="Idosome"></property>

<property name="interceptorNames" value="afterAdvice"></property>

</bean>

</beans>

[java] view
plain copy

print?

@Test

public void test02(){

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext08.xml");

cn.happy.spring12.IDoSome i=(cn.happy.spring12.IDoSome)ctx.getBean("someProxy");

i.add();

String id=i.add1();

System.out.println(id);

i.add2();

}

下面是环绕增强:实现接口是:MethodInterceptor

[java] view
plain copy

print?

package cn.happy.spring13;

/**

* Created by lihuohuo on 2017/7/31.

*/

public interface IDoSome {

public void add();

public String add1();

public void add2();

}

[java] view
plain copy

print?

package cn.happy.spring13;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class IDoSomes implements IDoSome {

public void add() {

System.out.println("=========1=======");

}

public String add1() {

System.out.println("=========2=======");

return "呵呵abc";

}

public void add2() {

System.out.println("=========3=======");

}

}

[java] view
plain copy

print?

package cn.happy.spring13;

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class MethodAdvice 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("之后================");

System.out.println("======temp========="+temp);

return temp;

}

}

[java] view
plain copy

print?

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

<bean id="Idosome" class="cn.happy.spring13.IDoSomes"></bean>

<bean id="methodAdvice" class="cn.happy.spring13.MethodAdvice"></bean>

<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="Idosome"></property>

<property name="interceptorNames" value="methodAdvice"></property>

</bean>

</beans>

[java] view
plain copy

print?

@Test

public void test03(){

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext09.xml");

cn.happy.spring13.IDoSome i=(cn.happy.spring13.IDoSome)ctx.getBean("someProxy");

i.add();

String id=i.add1();

System.out.println(id);

i.add2();

}

最后一个是异常增强:实现接口是:ThrowsAdvice

[java] view
plain copy

print?

package cn.happy.spring14;

/**

* Created by lihuohuo on 2017/7/31.

*/

public interface IDoSome {

public void add() throws UserException;

public String add1();

public void add2();

}

[java] view
plain copy

print?

package cn.happy.spring14;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class IDoSomes implements IDoSome {

public void add() throws UserException {

System.out.println("=========1=======");

throw new UserException("总算有错了");

}

public String add1() {

System.out.println("=========2=======");

return "呵呵abc";

}

public void add2() {

System.out.println("=========3=======");

}

}

[java] view
plain copy

print?

package cn.happy.spring14;

import org.springframework.aop.ThrowsAdvice;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class MyThrowsAdvice implements ThrowsAdvice{

public void afterThrowing(UserException ex){

System.out.println("错误提示:"+ex.getMessage());

}

}

[java] view
plain copy

print?

package cn.happy.spring14;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class UserException extends Exception{

public UserException() {

super();

}

public UserException(String message) {

super(message);

}

}

[java] view
plain copy

print?

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

<bean id="Idosome" class="cn.happy.spring14.IDoSomes"></bean>

<bean id="throwsAdvice" class="cn.happy.spring14.MyThrowsAdvice"></bean>

<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="Idosome"></property>

<property name="interceptorNames" value="throwsAdvice"></property>

</bean>

</beans>

[java] view
plain copy

print?

@Test

public void test04(){

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext10.xml");

cn.happy.spring14.IDoSome i=(cn.happy.spring14.IDoSome)ctx.getBean("someProxy");

try {

i.add();

} catch (UserException e) {

e.printStackTrace();

}

}

还有2种配置文件改变 方法的前置增强

[java] view
plain copy

print?

package cn.happy.spring16;

/**

* Created by lihuohuo on 2017/7/31.

*/

public interface IDoSome {

public void adTd();

public void adTd1();

public void add2();

}

[java] view
plain copy

print?

package cn.happy.spring16;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class IDoSomes implements IDoSome {

public void adTd() {

System.out.println("=========1=======");

}

public void adTd1() {

System.out.println("=========2=======");

}

public void add2() {

System.out.println("=========3=======");

}

}

[java] view
plain copy

print?

package cn.happy.spring16;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**

* Created by lihuohuo on 2017/7/31.

*/

public class MyBeforeAdvice implements MethodBeforeAdvice{

public void before(Method method, Object[] objects, Object o) throws Throwable {

System.out.println("=========before=======");

}

}

第一种使用方法名字 name 对特定的方法进行增强:

[java] view
plain copy

print?

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

<bean id="Idosome" class="cn.happy.spring15.IDoSomes"></bean>

<bean id="beforeAdvice" class="cn.happy.spring15.MyBeforeAdvice"></bean>

<bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">

<property name="advice" ref="beforeAdvice"></property>

<property name="mappedNames" value="add"></property>

</bean>

<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="Idosome"></property>

<property name="interceptorNames" value="beforeAdvisor"></property>

</bean>

</beans>

第二种 根据名的 .*T*.的这种方法

[java] view
plain copy

print?

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

<bean id="Idosome" class="cn.happy.spring16.IDoSomes"></bean>

<bean id="beforeAdvice" class="cn.happy.spring16.MyBeforeAdvice"></bean>

<bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

<property name="advice" ref="beforeAdvice"></property>

<property name="patterns" value=".*T.*"></property>

</bean>

<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="Idosome"></property>

<property name="interceptorNames" value="beforeAdvisor"></property>

<property name="proxyTargetClass" value="true"></property>

</bean>

</beans>

下面是测试类 ,都是一样的,。

[java] view
plain copy

print?

@Test

public void test06(){

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext12.xml");

cn.happy.spring16.IDoSome i=(cn.happy.spring16.IDoSome)ctx.getBean("someProxy");

i.add2();

i.adTd();

i.adTd1();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐