那些年一起追过的Spring[3]-AOP

JAVA 40 2017-10-09 12:31

核心概念:

切面,Aspect:其实就是我们需要定义的AOP类,它对功能类似的代码进行封装。

导包:

那些年一起追过的Spring[3]-AOP-JEESNS
导包

实现代码:
package com.spring.aop;
import java.util.Date;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogAop {
    @Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))")
    public void logBefore(){
        Date date=new Date();
        System.out.println("在方法执行之前执行日志------");
    }
}
package com.spring.dao;
public interface StudentDao {
 public void insert();
 public void update();
}
package com.spring.aop;
import java.util.Date;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogAop {
    @Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))")
    public void logBefore(){
        Date date=new Date();
        System.out.println("在方法执行之前执行日志------");
    }
}
package com.spring.dao;

public interface StudentDao {
 public void insert();
 public void update();
}
package com.spring.dao.impl;
import com.spring.dao.StudentDao;
public class StudentDaoImpl implements StudentDao {
 @Override
 public void insert() {
    System.out.println("method insert");
 }
@Override
public void update() {
    System.out.println("update method!");
}
}
package com.spring.services;
import com.spring.dao.StudentDao;
public class StudentServices {
 private StudentDao dao;
 public void setDao(StudentDao dao) {
    this.dao = dao;
 }
 public void insert() {
    dao.insert();
 }

 public void update(){
     dao.update();
 }
}
package com.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.services.StudentServices;
public class test01 {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("bean-aop-annotation.xml");
        StudentServices ss=(StudentServices) context.getBean("studentServices");
        ss.insert();
        ss.update();
    }
}

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.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean id="studentServices" class="com.spring.services.StudentServices">
    <property name="dao" ref="studentDao"></property>
</bean>

    <bean id="studentDao" class="com.spring.dao.impl.StudentDaoImpl">
</bean>

    <bean id="logAop" class="com.spring.aop.LogAop"></bean>
</beans>

运行结果:
在方法执行之前执行日志------
method insert
在方法执行之前执行日志------
update method!

关注知了堂 一起学习~

那些年一起追过的Spring[3]-AOP-JEESNS
知了堂
文章评论