aop控制
package com.sky.advice;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component // 1.交给spring管理
@Aspect // 2.将当前类声明成切面类
public class WriteLog {
// 配置切入点表达式,将切入点增强
// Before:前置通知
// AfterReturning:后置通知
// bean:增强某个bean(类)下的所有方法,一般定位到Service
// 这个bean下的所有方法都被增强了,在执行这个bean下面的方法的时候,先执行我这个方法
// @Before(value = "bean(userService)")
// * com.sky.service.UserService.update(..)
@AfterReturning(value = "execution(* com.sky.service.UserService.update(..))")
public void logInfo(){
// System.currentTimeMillis():时间戳
System.out.println(System.currentTimeMillis());
}
}
测试
package com.sky.test;
import com.sky.controller.UserController;
import com.sky.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// 加载容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController userController = (UserController) applicationContext.getBean("userController");
// 监控 update()
userController.update();
System.out.println("=======================================================");
// 不监控 info()
userController.info();
// System.out.println(userController);
// int login = userController.login("111111", "222222");
// System.out.println(login);
// if (login == 1) {
// System.out.println("测试OK");
// } else {
// System.out.println("测试ON");
// }
}
}
package com.sky.controller;
import com.sky.service.UserService;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
@Controller
public class UserController {
// @Resource:jdk提供的注入方式,注入对象
@Resource
private UserService service;
public int login(String username, String pwd){
System.out.println("controller======界面");
System.out.println("对象类型注入:"+service);
return service.login(username,pwd);
}
public void update(){
System.out.println("UserControllerUpdate");
service.update();
}
public void info(){
System.out.println("UserControllerUpdate");
service.info();
}
}
package com.sky.service;
import com.sky.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
// @Autowired:spring提供的注入方式
@Autowired
private UserMapper mapper;
public int login(String username, String pwd){
System.out.println("service======界面");
System.out.println("对象类型注入:"+mapper);
return mapper.login(username,pwd);
}
// 监控 update() 这个方法,只要方法运行,就记录他的运行时间
// 不修改这个方法的源码,也要实现对这个方法的记录了
public void update(){
System.out.println("UserServiceUpdate");
mapper.update();
}
public void info() {
System.out.println("UserServiceUpdate");
mapper.info();
}
}
package com.sky.mapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
@Repository
public class UserMapper {
// @Value:一般数据类型注入的注入方式,spring提供
@Value("普通类型注入")
private String hell;
public int login(String username, String pwd){
System.out.println("mapper======界面");
System.out.println("普通类型注入:"+hell);
int i = 0;
if ("111111".equals(username) && "222222".equals(pwd)) {
i = 1;
}
return i;
}
public void update() {
System.out.println("UserMapperUpdate");
}
public void info() {
System.out.println("UserMapperInfo");
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置注解扫描包:扫描com.sky包下的所有组件-->
<context:component-scan base-package="com.sky"></context:component-scan>
<!--aop的自动代理-->
<aop:aspectj-autoproxy/>
</beans>
结果展示