Java框架篇---spring aop两种配置方式

第一种:注解配置AOP

注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 
1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around). 
2. 开发需要被拦截的类。 
3. 将切面配置到xml中,当然,我们也可以使用自动扫描Bean的方式。这样的话,那就交由Spring AoP容器管理。

另外需要引用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar

实例:

User.java
package com.oumyye.model;public class User {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
/**
*接口类
*/
package com.oumyye.dao;
import com.oumyye.model.User;public interface UserDAO {public void save(User user);
}

实现接口:

package com.oumyye.dao.impl;import org.springframework.stereotype.Component;import com.oumyye.dao.UserDAO;
import com.oumyye.model.User;@Component("u")
public class UserDAOImpl implements UserDAO {public void save(User user) {System.out.println("user save11d!");/*throw new RuntimeException("exception");*/ //抛异常}}

操作类:

package com.oumyye.service;
import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;import com.oumyye.dao.UserDAO;
import com.oumyye.model.User;@Component("userService")
public class UserService {private UserDAO userDAO;  public void init() {System.out.println("init");}public void add(User user) {userDAO.save(user);}public UserDAO getUserDAO() {return userDAO;}@Resource(name="u")public void setUserDAO( UserDAO userDAO) {this.userDAO = userDAO;}public void destroy() {System.out.println("destroy");}
}

加入aop

package com.oumyye.aop;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LogInterceptor {@Pointcut("execution(public * com.oumyye.service..*.add(..))")public void myMethod(){};/*@Before("execution(public void com.oumyye.dao.impl.UserDAOImpl.save(com.oumyye.model.User))")*/@Before("myMethod()")public void before() {System.out.println("method staet");} @After("myMethod()")public void after() {System.out.println("method after");} @AfterReturning("execution(public * com.oumyye.dao..*.*(..))")public void AfterReturning() {System.out.println("method AfterReturning");} @AfterThrowing("execution(public * com.oumyye.dao..*.*(..))")public void AfterThrowing() {System.out.println("method AfterThrowing");}
}

配置文件

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  "><!-- 要添加最后2行 --><context:annotation-config /><context:component-scan base-package="com.oumyye"/>  <!-- 自动扫描 --><aop:aspectj-autoproxy/>  <!-- 要添加本行 -->
</beans>

测试类:

package com.oumyye.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.oumyye.model.User;//Dependency Injection
//Inverse of Control
public class UserServiceTest {@Testpublic void testAdd() throws Exception {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");UserService service = (UserService)ctx.getBean("userService");System.out.println(service.getClass());service.add(new User());System.out.println("###");ctx.destroy();}}

结果:

class com.oumyye.service.UserService$$EnhancerByCGLIB$$7b201784
method staet
user save11d!
method AfterReturning
method after
###

注意:

  • @Aspect:意思是这个类为切面类
  • @Componet:因为作为切面类需要 Spring 管理起来,所以在初始化时就需要将这个类初始化加入 Spring 的管理;
  • @Befoe:切入点的逻辑(Advice)
  • execution…:切入点语法

第二种:XML配置AOP

实例同上:只是配置文件不同

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  "><!-- 要添加最后2行 --><context:annotation-config /><context:component-scan base-package="com.oumyye"/><bean id="logInterceptor" class="com.oumyye.aop.LogInterceptor"></bean><aop:config><aop:pointcut expression="execution(public * com.oumyye.service..*.add(..))" id="servicePointcut"/><aop:aspect id="logAspect" ref="logInterceptor"><aop:before method="before"  pointcut-ref="servicePointcut" /></aop:aspect></aop:config>
</beans>

下面的<beans>是Spring的配置标签,beans里面几个重要的属性

  xmlns:

  是默认的xml文档解析格式,即spring的beans。地址是http://www.springframework.org/schema/beans。

  通过设置这个属性,所有在beans里面声明的属性,可以直接通过<>来使用,比如<bean>等等。

  xmlns:xsi:

  是xml需要遵守的规范,通过URL可以看到,是w3的统一规范,后面通过xsi:schemaLocation来定位所有的解析文件。

  xmlns:aop:

  这个是重点,是我们这里需要使用到的一些语义规范,与面向切面AOP相关。

  xmlns:tx:

  Spring中与事务相关的配置内容。

  一个XML文件,只能声明一个默认的语义解析的规范。

  例如上面的xml中就只有beans一个是默认的,其他的都需要通过特定的标签来使用,比如aop,它自己有很多的属性,如果要使用,前面就必须加上aop:xxx才可以。比如上面的aop:config。

  类似的,如果默认的xmlns配置的是aop相关的语义解析规范,那么在xml中就可以直接写config这种标签了。

Java框架篇---spring aop两种配置方式相关推荐

  1. Spring AOP两种使用方式以及如何使用解析

    AOP是一种面向切面编程思想,也是面向对象设计(OOP)的一种延伸. 在Spring实现AOP有两种实现方式,一种是采用JDK动态代理实现,另外一种就是采用CGLIB代理实现,Spring是如何实现的 ...

  2. Spring AOP两种实现机制是什么?

    Spring AOP两种实现机制是什么? 1.如果是有接口声明的类进行AOP 时,spring调用的是java.lang.reflection.Proxy 类来做处理 2.如果是没有接口声明的类时, ...

  3. SpringBoot的properties和yml两种配置方式, 配置注入参数, 以及配置文件读取失效的问题

    SpringBoot支持两种配置方式,一种是properties文件,一种是yml 首先在pom文件中添加依赖: <dependency><groupId>org.spring ...

  4. AOP 详解 、AOP 中通知类型 、AOP 两种实现方式(Schema-base 和 AspectJ)

    一.AOP AOP:中文名称面向切面编程 英文名称:(Aspect Oriented Programming) 正常程序执行流程都是纵向执行流程 3.1 又叫面向切面编程,在原有纵向执行流程中添加横切 ...

  5. Spring的两种代理方式:JDK动态代理和CGLIB动态代理

    代理模式 代理模式的英文叫做Proxy或Surrogate,中文都可译为"代理",所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者 ...

  6. Oracle RAC集群资源的两种配置方式,Admin Managed 和 Policy Manager,以及实验

    对于Oracle RAC集群数据库,有两种资源管理方式:Administrator Managed(管理员管理的),Policy Managed(策略管理的) 要理解这两个概念,首先应该了解Serve ...

  7. Java中匿名类的两种实现方式

    使用匿名内部类课使代码更加简洁.紧凑,模块化程度更高.内部类能够访问外部内的一切成员变量和方法,包括私有的,而实现接口或继承类做不到.然而这个不是我说的重点,我说的很简单,就是匿名内部类的两种实现方式 ...

  8. Java中定义字符串的两种常见方式、使用==和equals()比较字符串

    在讲使用==和equals()比较字符串之前,我们首先要讲定义字符串的两种常见方式,一种是定义一个常量,即直接定义字符串,一种是通过new关键字定义一个变量,即使用String类定义字符串,如下图: ...

  9. Vite内网ip访问,两种配置方式

    问题 使用vite运行项目的时候,控制台会只出现127.0.0.1(localhost)本地地址访问项目.不可以通过公司内网ip访问,其他团队成员无法访问,这是因为没有将服务暴露在局域网中: 两种解决 ...

最新文章

  1. 【opencv】(6) 图像轮廓处理
  2. python绘制灰度图片直方图-python+opencv 灰度直方图及其二值化
  3. matplotlib中文乱码
  4. foreach 语句
  5. 使用VS2003创建WEB程序的时候出现AutoMation服务器不能创建对象错误
  6. 爬取许嵩新歌《雨幕》弹幕,告诉你什么才是真正的创作!
  7. 玩转Mybatis —— 一个小demo,带你快速入门Mybatis
  8. SIM800A/C只能发短信不能收短信解决方案
  9. [PHP]如何使用Mobile_Detect来判断访问网站的设备:安卓,平板,电脑
  10. RPG游戏《黑暗之光》流程介绍与代码分析之(九):技能系统的实现
  11. 纯C#实现JPEG解码器在超大图片切割中的应用
  12. 贴片晶振为什么要邻层挖空敷铜,背后的原理原来是这样的!#终南小师傅
  13. 复杂网络 社交网络_通过角色的动态社交网络探索哈利·波特
  14. Java的学习(下)
  15. 两张人脸图像比对ocr技术
  16. GNN-Retro 逆合成路线规划
  17. LeetCode 387、字符串中的第一个唯一字符
  18. 嵌入式计算机的发展阶段,嵌入式系统的定义与发展历史
  19. 在vue中使用canvas实现简单特效(下雨天)
  20. 如何考量私有云的解决方案

热门文章

  1. asp.net 3.5 知识点
  2. Objective-C 2.0 with Cocoa Foundation--- 4,继承
  3. python——time模块实现指定时间触发器
  4. 华为机试——句子逆序
  5. C/C++——一些与输入有关的istream类成员函数
  6. 安装qtceator后无法启动help插件 Qt编译错误:cannot find -lGL 解决办法
  7. 1218 鼠标样式 cursor
  8. 02-mysql数据库的特点-卸载-安装-配置-mysql5.5版本
  9. dj鲜生-31-用户中心-功能需求分析
  10. 数据结构与算法-列表相关时间复杂度