Spring的aop和简单实现
什么是AOP?
- 面向切面编程,一个项目有很多不同的业务功能,把这些业务功能,当成核心点,而还有很多非核心点,比如日志,权限控制等,我们想把这些非核心的点加入到核心点中,那我们就可以使用AOP,把核心点切开一个个面.加入其中.
- 切面的目的是加强方法.
- AOP的目的是为了减少代码重复,去降低各个业务模块之间的耦合度.
怎么实现AOP?
- 导包,(aspectjweaver)
- 写一个增强类,类中有增强方法.
- 在Spring配置文件中添加AOP配置.(你要告诉Spring那个类是增强类?在什么时候要执行增强方法?)
- 执行测试
:-结构:
:-pom文件:
<?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.fan.springtest</groupId>
<artifactId>spring-test</artifactId>
<version>1.0.0</version>
<dependencies>
<!--spring基础包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!--spring测试包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!--测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--javaBean-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--aspectjweaver-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
</project>
:-增强类AopEnhance:
package com.fan.spring.aop.enhance;
import org.springframework.stereotype.Component;
@Component
public class AopEnhance {
public void add(){
System.out.println("我已经增强了该方法");
}
}
:-spring配置文件:
<?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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--ioc注解扫描器-->
<context:component-scan base-package="com.fan.spring.aop"/>
<!--DI解析器-->
<context:annotation-config/>
<!--1.未使用注解的方式-->
<aop:config>
<!--1.加入增强类-->
<aop:aspect ref="aopEnhance">
<!--1.1.需要增强什么方法-->
<aop:pointcut id="add" expression="execution(* aop*(..))"/>
<!--1.2.在什么时候增强哪些方法-->
<aop:before method="add" pointcut-ref="add"/>
</aop:aspect>
</aop:config>
</beans>
:-核心方法接口:
package com.fan.spring.aop.service;
public interface IAopService {
void aopSay();
}
:-核心实现:
package com.fan.spring.aop.service.impl;
import com.fan.spring.aop.service.IAopService;
import org.springframework.stereotype.Service;
@Service
public class AopServiceImpl implements IAopService {
public void aopSay() {
System.out.println("需要被增强的方法");
}
}
:-测试类:
package com.fan.spring.aop.test;
import com.fan.spring.aop.service.IAopService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config-aop.xml")
public class AopTest {
@Autowired
private IAopService iAopService;
@Test
public void aopTest(){
iAopService.aopSay();
}
}
:-结果
总结
- 不要忘记导包
- 知道怎么做,知道怎么说
捷径才是最远的路,不要把未来交给明天的自己.