spring aop两种配置方式_java

第一种:注解配置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.bjsxt.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.bjsxt.dao;
import com.bjsxt.model.User; 

public interface UserDAO {
 public void save(User user);
} 

实现接口:

package com.bjsxt.dao.impl; 

import org.springframework.stereotype.Component; 

import com.bjsxt.dao.UserDAO;
import com.bjsxt.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.bjsxt.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.bjsxt.dao.UserDAO;
import com.bjsxt.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.bjsxt.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.bjsxt.service..*.add(..))")
 public void myMethod(){}; 

 /*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.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.bjsxt.dao..*.*(..))")
 public void AfterReturning() {
 System.out.println("method AfterReturning");
 }
 @AfterThrowing("execution(public * com.bjsxt.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/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd
 http://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.bjsxt"/> <!-- 自动扫描 -->
 <aop:aspectj-autoproxy/> <!-- 要添加本行 -->
</beans>

测试类:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 

import com.bjsxt.model.User; 

//Dependency Injection
//Inverse of Control
public class UserServiceTest { 

 @Test
 public 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.bjsxt.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/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd
 http://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.bjsxt"/>
 <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
 <aop:config>
 <aop:pointcut expression="execution(public * com.bjsxt.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这种标签了。

以上就是spring aop两种配置方式,大家了解了吗?之后还有更多关于spring aop两种配置方式的文章和大家分享,一定要继续关注哦

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索spring
, aop
, xml配置
, aop配置
aop事务配置
spring aop配置方式、spring aop 配置、spring boot aop配置、spring mvc aop配置、spring aop注解配置,以便于您获取更多的相关知识。

时间: 2024-09-17 04:46:15

spring aop两种配置方式_java的相关文章

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

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

MapXtreme 2005 学习心得 第八节Session问题澄清-两种配置方式致开发方式不同(九)

前言: 出差一个月了,终于回来了,本来想继续写CYQ.Data的序列文章,突然看到MapXtreme有一条回应; 就写多一篇文章,澄清下我上一篇文章MapXtreme 2005 学习心得 一此问题(八)所说中的Session问题.     之前的调试方式 在MapXtreme 安装目录下,有一个MapXtremeControl的源码. 在上一篇中,由于调试不了源码,而只能在外面反复大量调试,以结果来猜测问题.以至于得出Session不稳定的结论.     先说说怎么调试MapXtremeCont

Java 中 Form表单数据的两种提交方式_java

1 GET - 从指定的服务器中获取数据 1.1 GET方法 使用GET方法时,查询字符串(键值对)被附加在URL地址后面一起发送到服务器,例如:http://localhost:8080//customer/customer_info?res=json&mt=0&custId=1 1.2 特点 (1) GET请求能够被缓存 (2) GET请求会保存在浏览器的浏览记录中 (3) 以GET请求的URL能够保存为浏览器书签 (4) GET请求有长度限制(不能多于1024字节) (5) GET请

JAVA中 Spring定时器的两种实现方式_java

目前有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz. 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 import java.util.TimerTask; public class EmailReportTask extends TimerTask{ @Override public void run() { ... } } 在Spring定义 ... 配置Spring定时器 <bean id=&quo

struts2单个文件上传的两种实现方式_java

通过2种方式模拟单个文件上传,效果如下所示 开发步骤如下: 1.新建一个web工程,导入struts2上传文件所需jar,如下图 目录结构 2.新建Action 第一种方式 复制代码 代码如下: package com.ljq.action; import java.io.File; import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext; import com.opensymph

Spring与Hibernate两种组合方式

Spring与Hibernate大致有两种组合方式,主要区别是一种是在Hibernate中的hibernate.cfg.xml中配置数据源,一种是借助Spring的jdbc方式在Spring的applicationContext.xml文件中配置数据源,然后在Spring配置sessionFactory的bean有些区别 下面大致的说明一下 第一种 1.hibernate.cfg.xml文件 xml version='1.0' encoding='utf-8'?>          "-/

Hibernate的两种配置文件格式

Hibernate有两种配置文件格式,一种是XML,默认为hibernate.cfg.xml,一种是properties,默认为hibernate.properties不同的配置文件,Hibernate有两种配置文件格式,一种是XML,默认为hibernate.cfg.xml,一种是properties,默认为hibernate.properties 不同的配置文件,对Hibernate的初始化方法是不一样的 比如,如果采用properties文件作为配置文件 那么初始化的代码大致为 Confi

android intent-intent 两种启动方式各有什么特点

问题描述 intent 两种启动方式各有什么特点 简述 intent. 两种启动方式 各有什么特点........................................................................... 解决方案 第一种:显示启动 在Mainfiest的配置: 以设置组件方式启动: Intent i=new Intent() i.setComponent(new ComponentName(""com.oo.intent"&quo

Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式。

原文:Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式. Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags(); 一.Android Activity 四种启动模式 lunchMode 在Manifest 配置文件中的属性如下: (1) android:launchMode="standard" (2)android:launchMod