Spring 前置通知和后置通知

User 实体类

package entity;
/**
 * 用户实体类
 */
public class User implements java.io.Serializable {
	private Integer id; // 用户ID
	private String username; // 用户名
	private String password; // 密码
	private String email; // 电子邮件
	public User() {
		super();
	}
	public User(Integer id, String username, String password, String email) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.email = email;
	}
	// getter & setter
	public Integer getId() {
		return id;
	}
	public void setId(Integer 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 getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "\nUser [id=" + id + ", username=" + username + ", password=" + password + ", email="
				+ email + "]";
	}
}

 

UserDao 接口与实现

package dao;
import java.util.List;
import entity.User;
/**
 * 增加DAO接口,定义了所需的持久化方法
 */
public interface UserDao {
	public void save(User user);
	public List<User> getAll();
}

 

package dao.impl;
import java.util.ArrayList;
import java.util.List;
import dao.UserDao;
import entity.User;
/**
 * 用户 DAO 类,实现 IDao 接口,负责 User 类持久化操作
 * */
public class UserDaoImpl implements UserDao {
	@Override
	public void save(User user) {
		//这里并没实现完整的数据库操作,仅为说明问题
		System.out.println("===============================");
		System.out.println("保存");
		System.out.println("===============================");
	}
	@Override
	public List<User> getAll() {
		List<User> users = new ArrayList<User>();
		users.add(new User(1, "景临境 ", "123456", "963@qq.com"));
		users.add(new User(2, "何开 ", "123456", "963@qq.com"));
		users.add(new User(3, "左阻 ", "123456", "963@qq.com"));
		users.add(new User(4, "暴天明", "123456", "963@qq.com"));
		System.out.println("===============================");
		System.out.println("得到所有的用户");
		System.out.println("===============================");
		return users;
	}
}

 

UserBiz 接口与实现

package biz;
import java.util.List;
import entity.User;
/**
 * 用户业务接口,定义了所需的业务方法
 */
public interface UserBiz {
	public void addNewUser(User user);
	public List<User> findAllUsers();
}

 

package biz.impl;
import java.util.List;
import dao.UserDao;
import dao.impl.UserDaoImpl;
import entity.User;
import biz.UserBiz;
/**
 * 用户业务类,实现对 User 功能的业务管理
 * */
public class UserBizImpl implements UserBiz {
	//声明接口类型的引用,和具体实现类解耦合
	private UserDao userDao = new UserDaoImpl();
	//dao 属性的 setter 访问器,会被 Spring 调用,实现市值注入
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Override
	public void addNewUser(User user) {
		//调用用户 DAO 的方法保存用户信息
		userDao.save(user);
	}
	@Override
	public List<User> findAllUsers() {
		// TODO Auto-generated method stub
		return userDao.getAll();
	}
}

 

 aop 实现

package aop;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;
/**
 * 通过 MethodBeforeAdvice 实现前置增强
 */
public class LoggerBefore implements MethodBeforeAdvice {
	private static final Logger log = Logger.getLogger(LoggerBefore.class);
	/**
	 * @param method 方法对象
	 * @param arguments 方法参数数组
	 * @param target 调用方法的对象
	 */
	@Override
	public void before(Method method, Object[] arguments, Object target)
			throws Throwable {
		log.info("调用"+target+"的"+method.getName()+"方法。方法的返回值:"
				+Arrays.toString(arguments));
	}
}

 

package aop;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;
/**
 * 通过 AfterReturningAdvice 实现后置增强
 * */
public class LoggerAfterReturning implements AfterReturningAdvice {
	private static final Logger log = Logger.getLogger(LoggerAfterReturning.class);
	
	/**
	 * @param returnValue 返回值
	 * @param method 方法对象
	 * @param arguments 方法参数数组
	 * @param target 调用方法的对象
	 */
	@Override
	public void afterReturning(Object returnValue, Method method, Object[] arguments,
			Object target) throws Throwable {
		log.info("调用"+target+"的"+method.getName()+"方法。方法的返回值:"
			+returnValue);
	}
}

 

 applicationContext.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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
	">
	<!-- DAO类 -->
	<bean id="userDao" class="dao.impl.UserDaoImpl" />
	<!-- 业务类 -->
	<bean id="userBiz" class="biz.impl.UserBizImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>
	<!-- 前置增强 -->
	<bean name="loggerBefore" class="aop.LoggerBefore"/>
	<!-- 后置增强 -->
	<bean name="loggerAfterReturning" class="aop.LoggerAfterReturning"/>
	<!-- AOP 织入 -->
	<aop:config>
		<!-- 切点(织入点)业务包和子包中所有的类和方法都是织入点 -->
		<aop:pointcut expression="execution(* biz..*.*(..))" id="pointcut"/>
		<!-- 要织入的对象 -->
		<aop:advisor advice-ref="loggerBefore" pointcut-ref="pointcut"/>
		<aop:advisor advice-ref="loggerAfterReturning" pointcut-ref="pointcut"/>
	</aop:config>
</beans>

 

 Test 测试数据

package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.User;
import biz.UserBiz;
public class Test {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserBiz userBiz = (UserBiz) ctx.getBean("userBiz");
		User user = new User();
		user.setId(1);
		user.setUsername("test");
		user.setPassword("123456");
		user.setEmail("963@qq.com");
		userBiz.addNewUser(user);
		userBiz.findAllUsers();
	}
}

 

效果图:

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值