«

Java Spring AOP的两种配置方式

时间:2024-3-2 17:17     作者:韩俊     分类: Java


第一种:注解配置AOP

java中注解配置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;<br style="word-wrap:break-word">

private String password;<br style="word-wrap:break-word">

public String getUsername() {<br style="word-wrap:break-word">

    return username;<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">

public void setUsername(String username) {<br style="word-wrap:break-word">

    this.username = username;<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">

public String getPassword() {<br style="word-wrap:break-word">

    return password;<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">

public void setPassword(String password) {<br style="word-wrap:break-word">

    this.password = password;<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">

}

/**

*接口类

*/

package com.bjsxt.dao;

import com.bjsxt.model.User;


public interface UserDAO {

public void save(User user);<br style="word-wrap:break-word">

}

复制代码
实现接口:

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) {<br style="word-wrap:break-word">


    System.out.println(&quot;user save11d!&quot;);<br style="word-wrap:break-word">

    /*throw new RuntimeException(&quot;exception&quot;);*/ //抛异常<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">


}

复制代码
操作类:

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;  <br style="word-wrap:break-word">


public void init() {<br style="word-wrap:break-word">

    System.out.println(&quot;init&quot;);<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">


public void add(User user) {<br style="word-wrap:break-word">

    userDAO.save(user);<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">

public UserDAO getUserDAO() {<br style="word-wrap:break-word">

    return userDAO;<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">


@Resource(name=&quot;u&quot;)<br style="word-wrap:break-word">

public void setUserDAO( UserDAO userDAO) {<br style="word-wrap:break-word">

    this.userDAO = userDAO;<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">


public void destroy() {<br style="word-wrap:break-word">

    System.out.println(&quot;destroy&quot;);<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">

}

复制代码
加入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(&quot;execution(public * com.bjsxt.service..*.add(..))&quot;)<br style="word-wrap:break-word">

public void myMethod(){};<br style="word-wrap:break-word">


/*@Before(&quot;execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))&quot;)*/<br style="word-wrap:break-word">

@Before(&quot;myMethod()&quot;)<br style="word-wrap:break-word">

public void before() {<br style="word-wrap:break-word">

    System.out.println(&quot;method staet&quot;);<br style="word-wrap:break-word">

} <br style="word-wrap:break-word">

@After(&quot;myMethod()&quot;)<br style="word-wrap:break-word">

public void after() {<br style="word-wrap:break-word">

    System.out.println(&quot;method after&quot;);<br style="word-wrap:break-word">

} <br style="word-wrap:break-word">

@AfterReturning(&quot;execution(public * com.bjsxt.dao..*.*(..))&quot;)<br style="word-wrap:break-word">

public void AfterReturning() {<br style="word-wrap:break-word">

    System.out.println(&quot;method AfterReturning&quot;);<br style="word-wrap:break-word">

} <br style="word-wrap:break-word">

@AfterThrowing(&quot;execution(public * com.bjsxt.dao..*.*(..))&quot;)<br style="word-wrap:break-word">

public void AfterThrowing() {<br style="word-wrap:break-word">

    System.out.println(&quot;method AfterThrowing&quot;);<br style="word-wrap:break-word">

} <br style="word-wrap:break-word">

}

复制代码
配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans&quot;

   xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br style="word-wrap:break-word">

   xmlns:context=&quot;http://www.springframework.org/schema/context&quot;<br style="word-wrap:break-word">

   xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;<br style="word-wrap:break-word">

   xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans<br style="word-wrap:break-word">


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


       &quot;&gt;&lt;!-- 要添加最后2行 --&gt;<br style="word-wrap:break-word">


&lt;context:annotation-config /&gt;<br style="word-wrap:break-word">

&lt;context:component-scan base-package=&quot;com.bjsxt&quot;/&gt;  &lt;!-- 自动扫描 --&gt;<br style="word-wrap:break-word">

&lt;aop:aspectj-autoproxy/&gt;  &lt;!-- 要添加本行 --&gt;<br style="word-wrap:break-word">

</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<br style="word-wrap:break-word">

public void testAdd() throws Exception {<br style="word-wrap:break-word">

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);<br style="word-wrap:break-word">


    UserService service = (UserService)ctx.getBean(&quot;userService&quot;);<br style="word-wrap:break-word">

    System.out.println(service.getClass());<br style="word-wrap:break-word">

    service.add(new User());<br style="word-wrap:break-word">

    System.out.println(&quot;###&quot;);<br style="word-wrap:break-word">


    ctx.destroy();<br style="word-wrap:break-word">


}<br style="word-wrap:break-word">


}

复制代码
结果:

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&quot;

   xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br style="word-wrap:break-word">

   xmlns:context=&quot;http://www.springframework.org/schema/context&quot;<br style="word-wrap:break-word">

   xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;<br style="word-wrap:break-word">

   xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans<br style="word-wrap:break-word">


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


       &quot;&gt;&lt;!-- 要添加最后2行 --&gt;<br style="word-wrap:break-word">


&lt;context:annotation-config /&gt;<br style="word-wrap:break-word">

&lt;context:component-scan base-package=&quot;com.bjsxt&quot;/&gt;<br style="word-wrap:break-word">

&lt;bean id=&quot;logInterceptor&quot; class=&quot;com.bjsxt.aop.LogInterceptor&quot;&gt;&lt;/bean&gt;<br style="word-wrap:break-word">

&lt;aop:config&gt;<br style="word-wrap:break-word">

    &lt;aop:pointcut expression=&quot;execution(public * com.bjsxt.service..*.add(..))&quot; <br style="word-wrap:break-word">

    id=&quot;servicePointcut&quot;/&gt;<br style="word-wrap:break-word">

    &lt;aop:aspect id=&quot;logAspect&quot; ref=&quot;logInterceptor&quot;&gt;<br style="word-wrap:break-word">

        &lt;aop:before method=&quot;before&quot;  pointcut-ref=&quot;servicePointcut&quot; /&gt;<br style="word-wrap:break-word">

    &lt;/aop:aspect&gt;<br style="word-wrap:break-word">


&lt;/aop:config&gt;<br style="word-wrap:break-word">

</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

热门推荐