AOP配置步骤(XML)
2024-09-09 17:41:00
1、maven依赖
<?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.ly.spring</groupId>
<artifactId>spring05</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--用于解析切入点表达式-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency>
<!--用于整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> </dependencies>
<!--解决IDEA maven变更后自动重置LanguageLevel和JavaCompiler版本的问题-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>13</source>
<target>13</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、实体类
package com.ly.spring.domain; import java.io.Serializable; public class Account implements Serializable {
}
3、service接口
package com.ly.spring.service; import com.ly.spring.domain.Account; import java.util.List; public interface IAccountService {
public List<Account> findAll();
public Account findOne();
public void saveAccount(Account account);
public int updateAccount(Account account);
public void deleteAccount(int id);
}
4、service实现类
package com.ly.spring.service.impl; import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.springframework.stereotype.Service; import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Override
public List<Account> findAll() {
System.out.println("AccountServiceImpl---findAll");
return null;
} @Override
public Account findOne() {
System.out.println("AccountServiceImpl---findOne");
return null;
} @Override
public void saveAccount(Account account) {
System.out.println("AccountServiceImpl---saveAccount");
} @Override
public int updateAccount(Account account) {
System.out.println("AccountServiceImpl---updateAccount");
return 0;
} @Override
public void deleteAccount(int id) {
System.out.println("AccountServiceImpl---deleteAccount");
}
}
5、通知类
package com.ly.spring.utils; import org.springframework.stereotype.Component; @Component("logUtil")
public class LogUtil {
public void writeLog() {
System.out.println("LogUtil --- writeLog");
}
}
6、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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置注解扫描的包-->
<context:component-scan base-package="com.ly.spring"></context:component-scan>
<!--aop相关配置-->
<aop:config>
<!--id指定通知的id,ref指定通知bean-->
<aop:aspect id="logAdvisor" ref="logUtil">
<!--配置通知类型,method指定调用通知bean的方法,pointcut指定切入点表达式-->
<aop:before method="writeLog" pointcut="execution(* com.ly.spring.service.impl.*.*(..))"></aop:before>
</aop:aspect>
</aop:config>
</beans>
7、测试类
package com.ly.spring.test; import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
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;
//替换junit的main方法
@RunWith(SpringJUnit4ClassRunner.class)
//指定spring配置文件的位置
@ContextConfiguration(locations = "classpath:bean.xml")
public class MainTest {
@Autowired
private IAccountService accountService;
@Test
public void test1() {
accountService.findAll();
accountService.findOne();
accountService.saveAccount(new Account());
accountService.updateAccount(new Account());
accountService.deleteAccount(1);
}
}
最新文章
- Art-template模板
- Activty四种启动模式
- js 对象数组根据对象中的属性排序
- SQL索引及视图常用语法
- DSP using MATLAB 示例Example3.8
- 学习RSA公开密钥算法
- vb.net 使用 Regex Replace 正则 替换 Html字串的table中tbody第一个tr下的td为th
- 【Java基础】增强for循环要注意陷阱
- I2C操作笔记——以 AT24C04为例
- GUI (图形界面)知识点
- ASP.NET MVC基于标注特性的Model验证:将ValidationAttribute应用到参数上
- 测试TCP/IP配置
- 读书笔记 effective c++ Item 1 将c++视为一个语言联邦
- javaScript(JS)强制保留两位小数的输入数校验和小数保留
- 使用Template格式化Python字符串
- iOS设备中垂直同步开启后的帧率计数
- An Overview of End-to-End Exactly-Once Processing in Apache Flink (with Apache Kafka, too!)
- [ 10.4 ]CF每日一题系列—— 486C
- Web前端框架与移动应用开发第七章
- POI获取单元格的宽和高