AOP配置步骤(XML)
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);
}
}
AOP配置步骤(XML)的更多相关文章
- 基于XML的AOP配置
创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...
- 基于 XML 的 AOP 配置(1)
本文连接:https://www.cnblogs.com/qzhc/p/11969734.html 接下来我将用一个很简单的实例 1. 环境搭建 1.1. 第一步:准备必要的代码 业务层代码: Acc ...
- Spring中AOP的基于xml开发和配置
pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- spring基于xml的声明式事务控制配置步骤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 面向切面编程AOP:基于XML文件的配置
除了使用AspectJ注解声明切面,Spring也支持在bean的配置文件中声明切面,这种声明是通过aop scheme中的XML元素完成的. 首先建立一个类: package com.sevenhu ...
- Spring Aop实例之xml配置
AOP的配置方式有2种方式:xml配置和AspectJ注解方式.今天我们就来实践一下xml配置方式. 我采用的jdk代理,所以首先将接口和实现类代码附上 package com.tgb.aop; pu ...
- springmvc简单的xml文件配置步骤
1.配置web.xml的servlet标签,在此标签中配置服务器配置文件 2.配置web.xml的servlet-mapping标签 3.配置application.xml的自动扫描包的位置 4.配置 ...
- SpringMVC(十四):SpringMVC 与表单提交(post/put/delete的用法);form属性设置encrypt='mutilpart/form-data'时,如何正确配置web.xml才能以put方式提交表单
SpringMVC 与表单提交(post/put/delete的用法) 为了迎合Restful风格,提供的接口可能会包含:put.delete提交方式.在springmvc中实现表单以put.dele ...
- 三(二)、AOP配置
一.AOP的配置(注解) 步骤一.导入jar包: 处理那5个jar包之外,还需要导入: aopalliance aspectjweaver spring-aop spring-aspects 步骤二. ...
随机推荐
- java.lang.NullPointerException at org.apache.jsp.**_jsp.jspInit(**_jsp.java)tomcat启动异常解决方法
今天遇到的其他一个问题就是,启动tomcat时,报:java.lang.NullPointerException at org.apache.jsp.**_jsp.jspInit(index_jsp. ...
- 严重 [RMI TCP Connection(3)-127.0.0.1]
学习Servlet时碰到的一个bug. Connected to server [2017-01-08 04:40:33,100] Artifact jspRun:war exploded: Arti ...
- 春招必问的redis持久化(RDB AOF),你能答上来么?
春招面试模拟,如同雷同,纯属巧合!!! 面试的大体流程: 第一步:一般会有笔试题,也可能没有.有笔试题就要好好答了,因为会重视笔试结果,为了节约面试官时间,HR可能先会看,不合格直接让你走人了. 第二 ...
- JDBCTemplate初学简介
JDBCTemplate JdbcTemplate是Spring的一部分,是对数据库的操作在jdbc的封装,处理了资源的建立和释放(不需要我们管理连接了),我们只需要提供SQL语句(不需要我们设置参数 ...
- 前端jQuery日历控件报错 $("#datepicker").datepicker is not a function
使用日历控件时,前端产生错误: $("#datepicker").datepicker is not a function 问题原因 前端在同一个页面,jQuery引入了两次. 解 ...
- Burpsuite Pro 2020.1最新破解版
简介 Burp Suite 是用于攻击web 应用程序的集成平台,包含了许多工具.Burp Suite为这些工具设计了许多接口,以加快攻击应用程序的过程.所有工具都共享一个请求,并能处理对应的HTTP ...
- centos7 安装 iRedmail 后 给nginx添加虚拟主机
iRedmail安装参考官方文档和 https://ywnz.com/linuxyffq/4563.html 准备工作 更新操作系统 yum update -y 安装必要组件 yum install ...
- 将win10激活为专业工作站版并且永久激活(图文详细教程)
简介 win10升级为专业版.教育版.专业工作站版永久激活详细图文教程(注:只要使用相对应的产品密钥,所有的版本都可以激活) win10家庭版其实就是阉割版,越来越多的人想升级为专业版.很多电脑用户选 ...
- Params:params 关键字可以指定在参数数目可变处采用参数的方法参数。
Params:params 关键字可以指定在参数数目可变处采用参数的方法参数. 注意点: 1.一个方法中只能使用一个params来声明不定长参数数组: 2.params参数数组只能放在已定义参数后面 ...
- Enum, Generic and Templates
文 Akisann@CNblogs / zhaihj@Github 本篇文章同时发布在Github上:https://zhaihj.github.io/enum-generic-and-templat ...