spring的AOP

AOP(Aspect Oriented Programming,面向方面编程)也叫面向切面编程,可以说是OOP(Object Oriented Programing,面向对象编程)的补充和完善。OOP引入封装、继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合。当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力。

AOP可以将系统中非核心的业务提取出来,进行单独处理。可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术,可以灵活的添加业务逻辑。一般用于事务管理、权限控制、错误处理等各种业务中。简单来说就是可以在一段程序之前或者之后做一些事。AOP的核心思想就是“将应用程序中的商业逻辑同对其提供支持的通用服务进行分离”。AOP可以降低模块间的耦合度,并有利于未来的可操作性和可维护性。

AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处都基本相似。比如权限认证、日志、事务处理。Aop 的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。

实现AOP的技术,主要分为两大类:一是采用动态代理技术(基本不怎么用),利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行;二是采用静态织入的方式,引入特定的语法创建“方面”,从而使得编译器可以在编译期间织入有关“方面”的代码。

AOP使用场景
Authentication 权限
Caching 缓存
Context passing 内容传递
Error handling 错误处理
Lazy loading 懒加载
Debugging 调试
logging 记录跟踪
tracing 优化
profiling and monitoring 校准
Performance optimization 性能优化
Persistence 持久化
Resource pooling 资源池
Synchronization 同步
Transactions 事务

使用注解的方式

spring-aop-annotation-beans.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<!-- 指定spring去哪些包下扫描组件 -->
	<context:component-scan base-package="com.spring.aop.annotation" />

	<!-- 开启spring对注解的支持 -->
	<context:annotation-config />
	
	<!-- 开启对aop注解的支持 -->
	<aop:aspectj-autoproxy />
</beans>

User实体类

package com.spring.aop.annotation.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {

	@Value("1004")
	private int id;
	@Value("张三")
	private String username;
	@Value("456456")
	private String password;
	@Value("zhangsan@qq.com")
	private String emain;
	@Value("17845983765")
	private String phone;

	public User() {
		super();
	}

	public User(int id, String username, String password, String emain, String phone) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.emain = emain;
		this.phone = phone;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmain() {
		return emain;
	}

	public void setEmain(String emain) {
		this.emain = emain;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", emain=" + emain + ", phone="
				+ phone + "]";
	}

}

UserDao层

package com.spring.aop.annotation.dao;

import com.spring.aop.annotation.entity.User;

public interface UserDao {

	void add(User user);

	void del(int id);

	void update(User user);

	User load(int id);

}

UserDaoImpl

package com.spring.aop.annotation.dao.impl;

import org.springframework.stereotype.Repository;

import com.spring.aop.annotation.dao.UserDao;
import com.spring.aop.annotation.entity.User;
import com.spring.aop.xml.customNote.LogInfoNote;

@Repository
public class UserDaoImpl implements UserDao {

	@Override
	@LogInfoNote(value = "添加用户")
	public void add(User user) {
		System.out.println("添加了" + user);
	}

	@Override
	@LogInfoNote(value = "删除用户")
	public void del(int id) {
		System.out.println("删除了id为" + id + "的数据");
	}

	@Override
	@LogInfoNote(value = "修改用户")
	public void update(User user) {
		System.out.println("修改数据为" + user);
	}

	@Override
	public User load(int id) {
		User user = new User(2, "李四", "lll", "ls@qq.com", "17598463574");
		System.out.println(user);
		return user;
	}

}

UserDaoStaticProxyImpl

//package com.spring.aop.annotation.dao.impl;
//
//import org.springframework.stereotype.Repository;
//
//import com.spring.aop.annotation.dao.UserDao;
//import com.spring.aop.annotation.entity.User;
//import com.spring.aop.annotation.log.LogInfo;
//
 静态代理,相当于重新写了UserDaoImpl,基本不使用
//@Repository(value = "userDao")
//public class UserDaoStaticProxyImpl implements UserDao {
//
//	@Override
//	public void add(User user) {
//		LogInfo.outInfo("新增");
//		System.out.println("添加了" + user);
//	}
//
//	@Override
//	public void del(int id) {
//		LogInfo.outInfo("删除");
//		System.out.println("删除了id为" + id + "的数据");
//	}
//
//	@Override
//	public void update(User user) {
//		LogInfo.outInfo("修改");
//		System.out.println("修改数据为" + user);
//	}
//
//	@Override
//	public User load(int id) {
//		User user = new User(2, "李四", "lll", "ls@qq.com", "17598463574");
//		System.out.println(user);
//		return user;
//	}
//
//}

UserService层

package com.spring.aop.annotation.service;

import com.spring.aop.annotation.entity.User;

public interface UserService {

	void add(User user);

	void del(int id);

	void update(User user);

	User load(int id);

}

UserServiceImpl

package com.spring.aop.annotation.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.aop.annotation.dao.UserDao;
import com.spring.aop.annotation.entity.User;
import com.spring.aop.annotation.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public void add(User user) {
		userDao.add(user);
	}

	@Override
	public void del(int id) {
		userDao.del(id);
	}

	@Override
	public void update(User user) {
		userDao.update(user);
	}

	@Override
	public User load(int id) {
		return userDao.load(id);
	}

}

UserController层

package com.spring.aop.annotation.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.spring.aop.annotation.entity.User;
import com.spring.aop.annotation.service.UserService;

@Controller
@Scope("prototype")
public class UserController {

	@Autowired
	private User user;

	@Autowired // 根据类型自动注入
	private UserService userService;

	@Value("1")
	private int id;

	@Value("1")
	private List<String> names;

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}

	public void outUser() {
		System.out.println(user);
	}

	public void outNames() {
		System.out.println(names);
	}

	/*******************************************/

	public void add() {
		userService.add(user);
	}

	public void del() {
		userService.del(id);
	}

	public void update() {
		userService.update(user);
	}

	public User load() {
		User user = null;
		user = userService.load(id);
		return user;
	}

}

LogInfo类

package com.spring.aop.annotation.log;

import java.util.Date;

public class LogInfo {

	public static void outInfo(String log) {
		System.out.println(log + "日志--->" + new Date());
	}

}

LogInfoNote类

package com.spring.aop.annotation.customNote;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

// 注解是一种特殊的类,运行时加载,作用于类或方法上对其进行控制与限制
@Retention(RetentionPolicy.RUNTIME) // 运行时生效
public @interface LogInfoNote {

	// 声明一个属性value,类型为String,default默认值为空字符串
	String value() default "";

}

AspectLog类

//package com.spring.aop.annotation.section;
//
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.After;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.stereotype.Component;
//
//import com.spring.aop.annotation.log.LogInfo;
//
//@Component
//@Aspect
//public class AspectLog {
//
//	/**
//	 * @Description: 在方法执行之前执行切面内容
//	 * @ ("execution(* com.spring.annotation.dao.*.*(..))") 
//	 * @ 第一个*表示任意返回值 
//	 * @ 第二个*表示com.spring.annotation.dao包下的所有类
//	 * @ 第三个*表示类下的所有函数
//	 * @ (..)表示函数内的任意参数
//	 * @ Before:设置横切点,表示前置通知
//	 * @ After:设置横切点,表示后置通知
//	 */
//
//	// 配置横切点
//	@Pointcut("execution(* com.spring.aop.annotation.dao.*.*(..))")
//	public void pointcut() {}
//
//	// 设置横切点,表示前置通知
	@Before("execution(* com.spring.aop.annotation.dao.*.*(..))")
//	// 只对增删改进行日志打印
	@Before("execution(* com.spring.aop.annotation.dao.*.add*(..)) || execution(* com.spring.aop.annotation.dao.*.update*(..)) || execution(* com.spring.aop.annotation.dao.*.del*(..))")
//	@Before("pointcut()")
//	public void beforeAddLog(JoinPoint joinPoint) {
//		// 得到执行语句的包名及其地址:com.spring.aop.annotation.dao.impl.UserDaoImpl@5609159b
//		// System.out.println(joinPoint.getTarget());
//		// 得到执行语句的函数名:add
//		// System.out.println(joinPoint.getSignature().getName());
//		String name = joinPoint.getSignature().getName();
//		LogInfo.outInfo("开始" + name);
//	}
//
//	// 设置横切点,表示后置通知
	@After("execution(* com.spring.aop.annotation.dao.*.*(..))")
//	@After("pointcut()")
//	public void afterAddLog(JoinPoint joinPoint) {
//		// 只对增删改进行日志打印
//		String name = joinPoint.getSignature().getName();
//		LogInfo.outInfo("结束" + name);
//	}
//
//	// 设置横切点,表示前、后置通知
	@Around("execution(* com.spring.aop.annotation.dao.*.*(..))")
	@Around("pointcut()")
	public void aroundAddLog(ProceedingJoinPoint proceedingJoinPoint) {
		LogInfo.outInfo("增加");
		try {
			proceedingJoinPoint.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		LogInfo.outInfo("增加");
	}
//
//}

GetAspectLog类

package com.spring.aop.annotation.section;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.spring.aop.annotation.log.LogInfo;
import com.spring.aop.xml.customNote.LogInfoNote;

@Component
@Aspect
public class GetAspectLog {

	/**
	 * @Description: 在方法执行之前执行切面内容
	 * @ ("execution(* com.spring.aop.annotation.dao.*.*(..)) && @annotation(logInfoNote)")
	 * @ 第一个*表示任意返回值 
	 * @ 第二个*表示com.spring.annotation.dao包下的所有类
	 * @ 第三个*表示类下的所有函数
	 * @ (..)表示函数内的任意参数
	 * @ Before:设置横切点,表示前置通知
	 * @ After:设置横切点,表示后置通知
	 * @ Around:设置横切点,表示前、后置通知
	 * @ annotation(logInfoNote):动态获得UserDaoImpl中@LogInfoNote的value的值
	 */

	// 设置横切点,表示前置通知
//	@Before("execution(* com.spring.aop.annotation.dao.*.*(..)) && @annotation(logInfoNote)")
//	public void beforeAddLog(LogInfoNote logInfoNote) {
//		String value = logInfoNote.value();
//		LogInfo.outInfo("开始" + value);
//	}

	// 设置横切点,表示后置通知
//	@After("execution(* com.spring.aop.annotation.dao.*.*(..)) && @annotation(logInfoNote)")
//	public void afterAddLog(LogInfoNote logInfoNote) {
//		String value = logInfoNote.value();
//		LogInfo.outInfo("结束" + value);
//	}

	// 设置横切点,表示前、后置通知
	@Around("execution(* com.spring.aop.annotation.dao.*.*(..)) && @annotation(logInfoNote)")
	public void aroundAddLog(ProceedingJoinPoint proceedingJoinPoint, LogInfoNote logInfoNote) {
		String value = logInfoNote.value();
		LogInfo.outInfo("开始" + value);
		try {
			proceedingJoinPoint.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		LogInfo.outInfo("结束" + value);
	}

}

TestUser测试类

package com.spring.aop.annotation.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.aop.annotation.controller.UserController;
import com.spring.aop.annotation.entity.User;

public class TestUserController {

	private BeanFactory bf = new ClassPathXmlApplicationContext("spring-aop-annotation-beans.xml");

	@Test
	public void addTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		User user = new User(1, "Tom", "101010", "Tom@qq.com", "15632475984");
		uc.setUser(user);
		uc.add();
	}

	@Test
	public void outUserTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.outUser();
	}

	@Test
	public void outNamesTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.outNames();
	}

	@Test
	public void delTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.del();
	}

	@Test
	public void updateTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		User user = new User(1003, "Tom", "101010", "Tom@qq.com", "15632475984");
		uc.setUser(user);
		uc.update();
	}

	@Test
	public void loadTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.load();
	}

}

使用xml的方式

spring-aop-xml-beans.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<!-- 指定spring去哪些包下扫描组件 -->
	<context:component-scan base-package="com.spring.aop.xml" />

	<!-- 开启spring对注解的支持 -->
	<context:annotation-config />

	<!-- 配置aop切面 -->
	<aop:config>
		<!-- 设置切面 -->
		<aop:aspect id="aspectLog" ref="aspectLog">
			<!-- 设置切点 -->
			<!-- <aop:pointcut expression="execution(* com.spring.aop.xml.dao.*.*(..))" id="pointcut"/> -->
			<!-- 动态获得UserDaoImpl中@LogInfoNote的value的值 -->
			<aop:pointcut expression="execution(* com.spring.aop.xml.dao.*.*(..)) &amp;&amp; @annotation(logInfoNote)" id="pointcut"/>
			<!-- 设置前置通知 -->
			<!-- <aop:before method="beforeAddLog" pointcut-ref="pointcut"/> -->
			<!-- 设置后置通知 -->
			<!-- <aop:after method="afterAddLog" pointcut-ref="pointcut"/> -->
			<!-- 设置前、后置通知 -->
			<aop:around method="aroundAddLog" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config>
</beans>

User实体类

package com.spring.aop.xml.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {

	@Value("1004")
	private int id;
	@Value("张三")
	private String username;
	@Value("456456")
	private String password;
	@Value("zhangsan@qq.com")
	private String emain;
	@Value("17845983765")
	private String phone;

	public User() {
		super();
	}

	public User(int id, String username, String password, String emain, String phone) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.emain = emain;
		this.phone = phone;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmain() {
		return emain;
	}

	public void setEmain(String emain) {
		this.emain = emain;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", emain=" + emain + ", phone="
				+ phone + "]";
	}

}

UserDao层

package com.spring.aop.xml.dao;

import com.spring.aop.xml.entity.User;

public interface UserDao {

	void add(User user);

	void del(Integer id);

	void update(User user);

	User load(Integer id);

}

UserDaoImpl

package com.spring.aop.xml.dao.impl;

import org.springframework.stereotype.Repository;

import com.spring.aop.xml.customNote.LogInfoNote;
import com.spring.aop.xml.dao.UserDao;
import com.spring.aop.xml.entity.User;

@Repository
public class UserDaoImpl implements UserDao {

	@Override
	@LogInfoNote(value = "添加用户")
	public void add(User user) {
		System.out.println("添加了" + user);
	}

	@Override
	@LogInfoNote(value = "删除用户")
	public void del(Integer id) {
		System.out.println("删除了id为" + id + "的数据");
	}

	@Override
	@LogInfoNote(value = "修改用户")
	public void update(User user) {
		System.out.println("修改数据为" + user);
	}

	@Override
	public User load(Integer id) {
		User user = new User(2, "李四", "lll", "ls@qq.com", "17598463574");
		System.out.println(user);
		return user;
	}

}

UserService层

package com.spring.aop.xml.service;

import com.spring.aop.xml.entity.User;

public interface UserService {

	void add(User user);

	void del(int id);

	void update(User user);

	User load(int id);

}

UserServiceImpl

package com.spring.aop.xml.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.aop.xml.dao.UserDao;
import com.spring.aop.xml.entity.User;
import com.spring.aop.xml.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public void add(User user) {
		userDao.add(user);
	}

	@Override
	public void del(int id) {
		userDao.del(id);
	}

	@Override
	public void update(User user) {
		userDao.update(user);
	}

	@Override
	public User load(int id) {
		return userDao.load(id);
	}

}

UserController层

package com.spring.aop.xml.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.spring.aop.xml.entity.User;
import com.spring.aop.xml.service.UserService;

@Controller
@Scope("prototype")
public class UserController {

	@Autowired
	private User user;

	@Autowired // 根据类型自动注入
	private UserService userService;

	@Value("1")
	private int id;

	@Value("1")
	private List<String> names;

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}

	public void outUser() {
		System.out.println(user);
	}

	public void outNames() {
		System.out.println(names);
	}

	/*******************************************/

	public void add() {
		userService.add(user);
	}

	public void del() {
		userService.del(id);
	}

	public void update() {
		userService.update(user);
	}

	public User load() {
		User user = null;
		user = userService.load(id);
		return user;
	}

}

LogInfo类

package com.spring.aop.xml.log;

import java.util.Date;

public class LogInfo {

	public static void outInfo(String log) {
		System.out.println(log + "日志--->" + new Date());
	}

}

LogInfoNote类

package com.spring.aop.xml.customNote;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

// 注解是一种特殊的类,运行时加载,作用于类或方法上对其进行控制与限制
@Retention(RetentionPolicy.RUNTIME) // 运行时生效
//@Target(ElementType.TYPE) // 作用于类、接口或枚举上
public @interface LogInfoNote {

	// 声明一个属性value,类型为String,default默认值为空字符串
	String value() default "";

}

AspectLog类

package com.spring.aop.xml.section;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import com.spring.aop.xml.customNote.LogInfoNote;
import com.spring.aop.xml.log.LogInfo;

@Component
public class AspectLog {

	// 和spring-aop-xml-beans.xml的<aop:pointcut expression="execution(* com.spring.aop.dao.*.*(..)) &amp;&amp; @annotation(logInfoNote)" id="pointcut"/>使用
	// 设置前置通知
//	public void beforeAddLog(LogInfoNote logInfoNote) {
//		String value = logInfoNote.value();
//		LogInfo.outInfo(value + "开始");
//	}
//	// 设置后置通知
//	public void afterAddLog(LogInfoNote logInfoNote) {
//		String value = logInfoNote.value();
//		LogInfo.outInfo(value + "结束");
//	}
	// 设置前、后置通知
	public void aroundAddLog(ProceedingJoinPoint proceedingJoinPoint, LogInfoNote logInfoNote) {
		String value = logInfoNote.value();
		LogInfo.outInfo(value + "开始");
		try {
			proceedingJoinPoint.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		LogInfo.outInfo(value + "结束");
	}

	// 和spring-aop-xml-beans.xml的<aop:pointcut expression="execution(* com.spring.aop.dao.*.*(..))" id="pointcut"/>使用
	// 设置前置通知
//	public void beforeAddLog(JoinPoint joinPoint) {
//		// 动态获得UserDaoImpl中@LogInfoNote的value的值
//		Object[] objs = joinPoint.getArgs();
//		Class<?>[] parameterTypes = new Class[objs.length];
//		for (int i = 0; i < objs.length; i++) {
//			parameterTypes[i] = objs[i].getClass();
//		}
//		try {
//			Method method = joinPoint.getTarget().getClass().getMethod(joinPoint.getSignature().getName(), parameterTypes);
//			if (method.isAnnotationPresent(LogInfoNote.class)) {
//				LogInfoNote logInfoNote = method.getAnnotation(LogInfoNote.class);
//				String value = logInfoNote.value();
//				LogInfo.outInfo("开始" + value);
//			}
//		} catch (NoSuchMethodException e) {
//			e.printStackTrace();
//		} catch (SecurityException e) {
//			e.printStackTrace();
//		}
//	}
//	// 设置后置通知
//	public void afterAddLog(JoinPoint joinPoint) {
//		// 动态获得UserDaoImpl中@LogInfoNote的value的值
//		Object[] objs = joinPoint.getArgs();
//		Class<?>[] parameterTypes = new Class[objs.length];
//		for(int i = 0; i < objs.length; i++) {
//			parameterTypes[i] = objs[i].getClass();
//		}
//		try {
//			Method method = joinPoint.getTarget().getClass().getMethod(joinPoint.getSignature().getName(), parameterTypes);
//			if(method.isAnnotationPresent(LogInfoNote.class)) {
//				LogInfoNote logInfoNote = method.getAnnotation(LogInfoNote.class);
//				String value = logInfoNote.value();
//				LogInfo.outInfo("结束" + value);
//			}
//		} catch (NoSuchMethodException e) {
//			e.printStackTrace();
//		} catch (SecurityException e) {
//			e.printStackTrace();
//		}
//	}

}

TestUser测试类

package com.spring.aop.xml.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.aop.xml.controller.UserController;
import com.spring.aop.xml.entity.User;

public class TestUserController {

	private BeanFactory bf = new ClassPathXmlApplicationContext("spring-aop-xml-beans.xml");

	@Test
	public void addTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		User user = new User(1, "Tom", "101010", "Tom@qq.com", "15632475984");
		uc.setUser(user);
		uc.add();
	}

	@Test
	public void outUserTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.outUser();
	}

	@Test
	public void outNamesTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.outNames();
	}

	@Test
	public void delTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.del();
	}

	@Test
	public void updateTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		User user = new User(1003, "Tom", "101010", "Tom@qq.com", "15632475984");
		uc.setUser(user);
		uc.update();
	}

	@Test
	public void loadTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.load();
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值