2020-04-03 spring------自动创建代理

一、配置文件

<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p">
<!--4.3  自动创建代理-->
    <bean id="helloService" class="test.spring.proxyServices.Impl.GreetServiceImpl"/>
    <bean id="helloTarget" class="test.spring.proxyServices.Impl.HelloImpl"/>
    <!--定义通知-->
    <bean id="helloAdvice" class="test.spring.proxy.GreetServiceBeforeAdvice"/>
    <!--1. BeanNameAutoProxyCreator 根据Bean名称创建代理-->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames" value="*Service"/>
        <property name="interceptorNames" value="helloAdvice"/>
    </bean>
    <!--2. DefaultAdvisorAutoProxyCreator 根据Advisor本身包含信息创建代理,即匹配名中有say的方法-->
    <bean id="regeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="helloAdvice"/>
        <property name="patterns" value=".*say.*"/>
    </bean>
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
</beans>


二、代码
1.HelloImpl&GreetServiceImpl

package test.spring.proxyServices.Impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import test.spring.proxyServices.IUserDao;
import javax.annotation.Resource;
public class HelloImpl {
    @Autowired
    @Qualifier(value = "userDao1")
    private test.spring.proxyServices.IUserDao userDao;

    public IUserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    public void sayHello(String name) {
        System.out.println("hello:" + name);
        int sum = userDao.getNum();   //sum = name.length();
//        return sum;
    }
}
package test.spring.proxyServices.Impl;
import test.spring.proxyServices.GreetService;
/*实现类*/
public class GreetServiceImpl implements GreetService {

    public String greet(String name) {
        System.out.println("Helle,"+name);
        return name;
    }

    public String greet1() {
        System.out.println("hello,Spring");
        return "spring";
    }
}


2.GreetServiceBeforeAdvice---spring AOP

package test.spring.proxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
/*通知类*/
public class GreetServiceBeforeAdvice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor,ThrowsAdvice {
    /*前置通知*/
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(method);
        System.out.println(args);
        System.out.println(target);
        System.out.println("方法执行之前执行的内容。。。");
    }
    /*后置通知*/
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(method);
        System.out.println(args);
        System.out.println(target);
        System.out.println("方法执行之后执行的内容。。。");
    }


    public Object invoke(MethodInvocation invocation) throws Throwable {

        System.out.println(invocation);
        System.out.println("方法执行之前");
        invocation.proceed();
        System.out.println("方法执行之后");
        return null;
    }

    public void afterThrowing(Method method, Object[] args, Object target, Exception ex){
        System.out.println("系统发生异常。。");
        System.out.println(ex.getMessage());
    }
}


3.test

package test.spring.Controller.proxy;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.spring.proxyServices.GreetService;
import test.spring.proxyServices.IUserDao;
import test.spring.proxyServices.Impl.HelloImpl;
import test.spring.proxyServices.Impl.UserDao1;
public class AutoCreateProxyAction {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        GreetService greetService = (GreetService) applicationContext.getBean("helloService");
        greetService.greet("world!");

        HelloImpl hello = (HelloImpl)applicationContext.getBean("helloHI");
        IUserDao userDao = new UserDao1();
        hello.setUserDao(userDao);
//        System.out.println(hello.sayHello("jing"));
        hello.sayHello("小红");
    }
}


【补充】
sayHello(String name)这个方法有返回值会报异常:org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public int test.spring.proxyServices.Impl.HelloImpl.sayHello(java.lang.String)。没有返回值的情况下代码可运行。原因待查。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值