05spring_AOP
UserDAO
save
Log.info('数据保存');
public void save(){
//数据保存
Log log = new Log();
log.info();
}
open
Log.info('数据库连接打开');
close
read
write
insert 操作
MyUserDAO extends UserDAO{
public void save(){
super.save();
Log.info("数据保存");
}
}
Log 日志类
aop 概念?
Aspect Oriented Programming面向切面编程(也叫面向方面编程)
项目使用AOP
1)pom.xml文件加入aop的依赖
<!-- springframework AOP 支持 -->
<!-- aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.4</version>
</dependency>
<!-- aop end -->
2)修改src/main/resources/beans.xml spring容器的核心配置文件
<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-4.3.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">
<aop:config>
<aop:pointcut expression="execution(public * com.fz.dao..*.*(..))" id="cc" />
<aop:aspect id="loga" ref="log">
<aop:before method="connect" pointcut-ref="cc" />
<aop:after method="close" pointcut-ref="cc" />
</aop:aspect>
</aop:config>
</beans>
<aop:pointcut expression="execution(public * com.fz.dao..*.*(..))" id="cc" />
<aop:pointcut expression="execution(public * com.fz.dao.UserDAO.save)" id="cc" />
<aop:pointcut expression="execution(public * com.fz.dao..UserDAO.save*(..))" id="cc" />
使用注解实现 AOP
----------------------------------------------------------------------------
1、pom.xml 引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fz</groupId>
<artifactId>spring04</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
<!-- springframework AOP 支持 -->
<!-- aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.4</version>
</dependency>
<!-- aop end -->
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<testSourceDirectory>src/test/java</testSourceDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<!-- 处理无法加载资源配置文件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
2、编写src/main/resources/beans.xml
<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-4.3.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">
<!-- aop注解支持 -->
<aop:aspectj-autoproxy/>
<!-- 容器bean注解支持 -->
<context:annotation-config/>
<context:component-scan base-package="com.fz.entity,com.fz.dao"/>
</beans>
3、aop切入点类编写 src/main/java/com/fz/dao/Log.java
package com.fz.dao;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* Created by webrx on 2017-07-05.
*/
@Component @Aspect
public class Log {
@Before("execution(public * com.fz.entity..*.*(..))")
public void connect(){
System.out.println("连接数据库");
}
@Before("execution(public * com.fz.entity..*.*(..))")
public void init(){
System.out.println("初始化");
}
@After("execution(public * com.fz.entity..User.read(..))")
public void close(){
System.out.println("关闭数据库");
}
}
4、测试程序如下 src/test/java/Demo.java
import com.fz.dao.UserDao;
import com.fz.entity.Book;
import com.fz.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by webrx on 2017-07-05.
*/
public class Demo {
protected BeanFactory f;
@Before
public void init(){
this.f = new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void tt(){
User u = this.f.getBean("user",User.class);
u.read();
System.out.println("----------------------------");
u.del();
System.out.println("----------------------------");
Book book = this.f.getBean("book",Book.class);
book.show();
}
}