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

转载于:http://www.itxuexiwang.com/a/shujukujishu/2016/0206/81.html
第一种:注解配置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;

#p#分页标题#e#public class User {
private String username;
private String password;
public String getUsername() {
return username;
#p#分页标题#e# }
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return#p#分页标题#e# password;
}
public void setPassword(String password) {
this.password = password;
}
}
#p#分页标题#e#
/**
*接口类
*/
package com.bjsxt.dao;
import com.bjsxt.model.User;
#p#分页标题#e#

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


实现接口:

#p#分页标题#e#
package com.bjsxt.dao.impl;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import#p#分页标题#e# com.bjsxt.model.User;

@Component("u")
public class UserDAOImpl implements UserDAO {

public#p#分页标题#e# void save(User user) {

System.out.println("user save11d!");
/*throw new RuntimeException("exception");*/ //抛异常
}

#p#分页标题#e#}
操作类:

package com.bjsxt.service;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
#p#分页标题#e#import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

#p#分页标题#e#
@Component("userService")
public class UserService {

private UserDAO userDAO;

#p#分页标题#e# public void init() {
System.out.println("init");
}

public void add(User user) {
userDAO.save(user); #p#分页标题#e#
}
public UserDAO getUserDAO() {
return userDAO;
}

@Resource(name=#p#分页标题#e#"u")
public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
}

public void#p#分页标题#e# destroy() {
System.out.println("destroy");
}
}


加入aop

package com.bjsxt.aop;
#p#分页标题#e#
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; #p#分页标题#e#
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
#p#分页标题#e#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))")*/#p#分页标题#e#
@Before("myMethod()")
public void before() {
System.out.println("method staet");
}
#p#分页标题#e#@After("myMethod()")
public void after() {
System.out.println("method after");
}
@AfterReturning("execution(public * com.bjsxt.dao..*.*(..))")
#p#分页标题#e# public void AfterReturning() {
System.out.println("method AfterReturning");
}
@AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))")
public void AfterThrowing() { #p#分页标题#e#
System.out.println("method AfterThrowing");
}
}
配置文件

<?xml version="1.0" encoding="UTF-8"?>
#p#分页标题#e#<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
#p#分页标题#e# 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 #p#分页标题#e#
"><!-- 要添加最后2行 -->

<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/> <!-- 自动扫描 -->
<aop:aspectj-autoproxy/> <!-- 要添加本行 -->
#p#分页标题#e#</beans>
测试类:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; #p#分页标题#e#

import com.bjsxt.model.User;

//Dependency Injection
//Inverse of Control
public#p#分页标题#e# class UserServiceTest {

@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); #p#分页标题#e#


UserService service = (UserService)ctx.getBean("userService");
System.out.println(service.getClass());
service.add(new User());
System.out.println("###"#p#分页标题#e#);

ctx.destroy();

}

}
结果:

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

#p#分页标题#e#注意:

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

第二种:xml配置aop

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
#p#分页标题#e# 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
#p#分页标题#e# 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行 --> #p#分页标题#e#

<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
<aop:config> #p#分页标题#e#
<aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))"
id="servicePointcut"/>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut-ref="servicePointcut" />
#p#分页标题#e# </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相关。#p#分页标题#e#

xmlns:tx:

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

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

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

类似的,如果默认的xmlns配置的是aop相关的语义解析规范,那么在xml中就可以直接写config这种标签了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

手把手教你学AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值