public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
import com.jt.pojo.User;
public interface UserMapper {
void addUser(User user);
}
import com.jt.pojo.User;
import org.springframework.stereotype.Repository;
@Repository
public class UserMapperImpl implements UserMapper {
//??事务控制应该在哪一层完成!!dao/mapper service
@Override
public void addUser(User user) {
System.out.println("用户入库"+user);
}
}
import com.jt.pojo.User;
public interface UserService {
void addUser(User user);
}
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
//事务控制应该放在Service层中控制
@Override
public void addUser(User user) {
try {
System.out.println("事务开始");
userMapper.addUser(user);
System.out.println("事务结束");
} catch (Exception e) {
e.printStackTrace();
System.out.println("事务回滚");
}
}
}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration//标识我是一个配置类
@ComponentScan("com.jt")
public class SpringConfig {
}
import com.jt.config.SpringConfig;
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestUser {
//Controller-Service-Mapper(Dao)
//Spring中规定:
// 如果传入的是接口的类型 则自动查找/注入 该接口的实现类
// 该接口只有一个实现类
@Test
public void testTx(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
//写接口的类型/实现类的类型?
UserService userService = context.getBean(UserService.class);
// UserService userService = (UserService) context.getBean("userServiceImpl");
User user = new User();
user.setId(101);
user.setName("SpringAop测试入门案例");
userService.addUser(user);
}
}
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class StaticProxy implements UserService {
//要求引入目标对象
@Autowired //ByType byName
//@Qualifier("target")
private UserService target;
//目的: 对原有方法进行扩展
@Override
public void addUser(User user) {
try {
System.out.println("事务开始");
target.addUser(user);
System.out.println("事务结束");
} catch (Exception e) {
e.printStackTrace();
System.out.println("事务回滚");
}
}
}
@Test
public void testStaticProxy(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = (UserService) context.getBean("userService");
User user = new User();
user.setId(10001);
user.setName("测试代理机制");
//执行用户调用
userService.addUser(user);
}
官网API:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {}
result = method.invoke(target,args);
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//能否利用一个工厂动态为目标对象创建代理
public class JDKProxyFactory {
//要求用户传递目标对象
//关于匿名内部类用法说明: 匿名内部类引用外部参数 要求参数必须final修饰
public static Object getProxy(final Object target) {
//1.调用java API实现动态代理
/**
* 参数分析: 3个参数
* 1.ClassLoader loader, 类加载器(获取目标对象的Class)
* 2.类[] interfaces, JDK代理要求 必须有接口
* java中可以多实现
* 3.InvocationHandler h 对目标方法进行扩展
*/
//1.获取类加载器
ClassLoader classLoader = target.getClass().getClassLoader();
//2.获取接口数组
Class[] interfaces = target.getClass().getInterfaces();
//3.通过动态代理创建对象
Object proxy = Proxy.newProxyInstance(classLoader, interfaces, new InvocationHandler() {
//invoke方法: 代理对象调用方法时invoke执行,扩展方法的编辑位置
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//proxy: 代理对象本身
//method: 用户调用的方法对象
//args: 用户调用方法的参数
// result 标识目标方法执行的返回值
Object result = null;
try {
//添加事务的控制
System.out.println("事务开始");
//执行目标方法
// target真实的目标对象,method方法对象,args方法参数
result = method.invoke(target, args);
System.out.println("事务提交");
} catch (Exception e) {
e.printStackTrace();
System.out.println("事务回滚");
}
return result;
}
});
return proxy;
}
}
/**
* 测试JDK动态代理
*/
@Test
public void testJDKProxy(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
//1.获取用户目标对象
UserService target = (UserService) context.getBean("target");
//2.获取代理对象
UserService userService = (UserService) JDKProxyFactory.getProxy(target);
//3.打印代理对象的类型
System.out.println(userService.getClass());
//4.用户完成调用
User user = new User();
user.setId(10001);
user.setName("JDK动态代理完成");
//执行用户调用
userService.addUser(user);
}
将公共的部分写到动态代理中,之后其他的业务类调用即可
public interface DeptService {
void addDept();
}
import org.springframework.stereotype.Service;
@Service("deptService")
public class DeptServiceImpl implements DeptService {
@Override
public void addDept() {
//添加事务
System.out.println("调用DeptMapper实现入库操作");
//提交事务
}
}
@Test
public void testTx() {
//1.获取目标对象
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
DeptService target = (DeptService) context.getBean("deptService");
//2.获取代理对象
DeptService deptService = (DeptService) JDKProxyFactory.getProxy(target);
//通过代理对象 调用方法 扩展了方法!!!!!
deptService.addDept(); //invoke
}
public interface UserService {
void addUser();
void deleteUser();
}
@Service("target")
public class UserServiceImpl implements UserService{
@Override
public void addUser() {
System.out.println("新增用户");
}
@Override
public void deleteUser() {
System.out.println("删除用户");
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKProxyFactory {
//编辑静态方法获取代理对象
public static Object getProxy(final Object target) {
//3个参数 1.类加载器 2.对象的接口
ClassLoader classLoader = target.getClass().getClassLoader();
Class[] interfaces = target.getClass().getInterfaces();
Object proxy = Proxy.newProxyInstance(classLoader, interfaces, new InvocationHandler() {
//代理对象执行目标方法时执行
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//让用户执行目标方法
Long startTime = System.currentTimeMillis(); //开始时间
//执行目标方法 获取返回值 可能为null
Object result = method.invoke(target);
Long endTime = System.currentTimeMillis(); //结束时间
System.out.println("程序执行:" + (endTime - startTime) + "毫秒");
//将返回值传递给调用者
return result;
}
});
return proxy;
}
}
@Test
public void test01(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
//1.获取目标对象
UserService target = (UserService) context.getBean("target");
//2.获取代理对象
UserService proxy = (UserService) JDKProxyFactory.getProxy(target);
System.out.println(proxy.getClass());
//3.调用业务方法
proxy.addUser();
proxy.deleteUser();
}