Spring Dynamic unified agent interface and transaction processing

Dynamic proxy interface of Here Insert Picture Descriptionthe interface:

public class UserServiceImpl implements UserService {
    public void addUser() {
        System.out.println("addUser");
    }
}

Test categories:

 @Test
    public void t3(){
        final UserServiceImpl userService=new UserServiceImpl();
        /*
        * ClassLoader loader, 一般调用当前类的类加载器
        * Class<?>[] interfaces,
        * InvocationHandler h
        * */
        InvocationHandler handler= new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("开启事务");
                Object object=  method.invoke(userService,args);
                System.out.println("关闭事务");
                return object;
            }

        };
        UserService userServiceProxy= (UserService) Proxy.newProxyInstance(Test01.class.getClassLoader(),
                UserServiceImpl.class.getInterfaces(),handler );
        userServiceProxy.addUser();

    }

Unified Transaction Processing

Unified transaction categories:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/*
* 重写了 BeanPostProcessor
* 配置的方法非常简单,只要配置给容器就可以了
* */
public class MyBeanPostProcess implements BeanPostProcessor{
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Before-Initialization");
        System.out.println(bean);
        System.out.println(beanName);
        return bean;
    }
    public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
        System.out.println("After-Initialization");
        if(beanName.equals("userService" )){
            InvocationHandler handler= new InvocationHandler() {
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("开启事务");
                    Object object=  method.invoke(bean,args);
                    System.out.println("关闭事务");
                    return object;
                }
            };
            // BeanPostProcessor全局生效  需要使用名称进行过滤
            return Proxy.newProxyInstance(MyBeanPostProcess.class.getClassLoader(),
                    bean.getClass().getInterfaces(),handler);
        }else {
            return bean;
        }
    }
}

applicationContext.xml configuration;

<bean class="com.xbj.dao.impl.UserServiceImpl" id="userService" />
<bean class="com.xbj.dao.MyBeanPostProcess" />
 <!--配置BeanPostProcess  全局生效-->

userService:

public interface UserService {
    void addUser();
}

UserServiceImpl:

public class UserServiceImpl implements UserService {
    public void addUser() {
        System.out.println("addUser");
    }
}

Test categories:

    private ClassPathXmlApplicationContext context;
    @Before
    public void t1(){
        context= new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    @Test
    public void t2(){
        UserService userService=context.getBean("userService",UserService.class);
        userService.addUser();
        context.close();
    }

Guess you like

Origin blog.csdn.net/qq_37651267/article/details/92079669