Spring AOP xml配置方法
2017-03-23 15:24
381 查看
面向切面编程
所有支持<aop:aspect>标签的功能只支持单例模式。
配置类截图
package com.test.xml;
import org.aspectj.lang.ProceedingJoinPoint;
public class MoocAspect {
public void before(){
System.out.println("*********before********");
}
public void after(){
System.out.println("*********after*********");
}
public void afterReturn(){
System.out.println("******afterReturn*****");
}
/*
* <aop:around method="around" pointcut-ref="bizservice3"/>
* 在执行around时候必须要有ProceedingJoinPoint类型的参数,并且返回一个Object值。
*
*/
public Object around(ProceedingJoinPoint pjp) {
Object object = null;
try {
System.out.println("******around1*****");
object = pjp.proceed();
System.out.println("******around2*****");
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
public void afterThrowing(){
System.out.println("**********afterThrowing*********");
}
public Object aroundParameter(ProceedingJoinPoint pjp,String name,String sex,int age){
Object o = null;
try {
System.out.println("aroundParameter1:"+name+" "+sex+" "+age);
o = pjp.proceed();
System.out.println("aroundParameter2:"+name+" "+sex+" "+age);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return o;
}
}
业务类:
package com.test.xml;
import java.io.File;
public class AspectBiz {
public void bizservice(){
System.out.println("*********执行bizservice()************");
}
public void bizreturn(){
System.out.println("**********执行bizreturn()**************");
}
public void bizaround(){
System.out.println("**********执行bizaround()**************");
}
public void bizafterthrow() throws Exception{
System.out.println("**********执行bizAfterthrow()**************");
File f =null;
f.getName();
}
/*
* <aop:pointcut id="bizservice5" expression="execution(* com.test.xml.AspectBiz.*aroundParameter(String,String,int)) and args(name,sex,age)" />
* 规定参数名字必须是name,sex,age
*/
public void bizaroundParameter(String name,String sex,int age){
System.out.println("*********bizaroundPararmeter*********");
}
}
测试类:package com.test.xml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.junit.test.UnitTestBase;
/*
* 测试类
*/
@RunWith(BlockJUnit4ClassRunner.class)
public class TestXml extends UnitTestBase {
public TestXml(){
super("classpath:applicationContext.xml");
//super("classpath:round.xml");
}
@Test
public void test1() throws Exception {
AspectBiz ab = super.getBean("aspectBiz");
// ab.bizservice();
// //从慕课网(用的spring4)看到的视频是after应该在afterreturn后执行,实验结果(用的spring3)是afterreturn在after之后
// ab.bizreturn();
// ab.bizaround();
// ab.bizafterthrow();
// ab.bizaroundParameter("zhangfei", "man", 23);
}
}
UnitTestBase类:
package com.junit.test;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTestBase {
private ClassPathXmlApplicationContext context;
private String stringXmlPath;
public UnitTestBase() {};
public UnitTestBase(String stringXmlPath) {
this.stringXmlPath = stringXmlPath;
};
@Before
public void before(){
if(stringXmlPath==null||stringXmlPath.equals("")){
stringXmlPath = "classPath*:spring-*.xml";
}
try{
context = new ClassPathXmlApplicationContext(stringXmlPath.split("[,\\s]+"));
context.start();
}catch(Exception e){
e.printStackTrace();
}
}
@After
public void after(){
context.destroy();
}
@SuppressWarnings("unchecked")
protected <T extends Object>T getBean(String beanId){
return (T)context.getBean(beanId);
}
protected <T extends Object>T getBean(Class<T> clazz){
return (T)context.getBean(clazz);
}
}
applicationContext.xml:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="mookAspect" class="com.test.xml.MoocAspect"/>
<bean id="aspectBiz" class="com.test.xml.AspectBiz"/>
<aop:config>
<aop:aspect id="mookAspectAop" ref="mookAspect">
<aop:pointcut id="bizservice1" expression="execution(public * *(..))" />
<aop:pointcut id="bizservice2" expression="execution(* com.test.xml.AspectBiz.*return(..))" />
<aop:pointcut id="bizservice3" expression="execution(* com.test.xml.AspectBiz.*around(..))" />
<aop:pointcut id="bizservice4" expression="execution(* com.test.xml.AspectBiz.*afterthrow(..))" />
<aop:pointcut id="bizservice11" expression="execution(* com.test.xml.AspectBiz.*aroundParameter(String,String,int)) and args(name,sex,age)"/>
<aop:before method="before" pointcut-ref="bizservice1"/>
<aop:after method="after" pointcut-ref="bizservice1"/>
<aop:after-returning method="afterReturn" pointcut-ref="bizservice2"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="bizservice4"/>
<aop:around method="around" pointcut-ref="bizservice3"/>
<aop:around method="aroundParameter" pointcut-ref="bizservice11"/>
</aop:aspect>
</aop:config>
</beans>
其中applicationContext.xml中的<aop:around method="around" pointcut-ref="bizservice3"/>
<aop:around method="aroundParameter" pointcut-ref="bizservice11"/>这两条配置需要挨着,否则实验测试时候总是包.xml文件初始化错误,原因未找到
再注意<aop:around method="around" pointcut-ref=""/>标签的配置,对应的method必须有一个ProceedingJoinPoint类型的参数,然后次参数对象的proceed()方法。
所有支持<aop:aspect>标签的功能只支持单例模式。
配置类截图
package com.test.xml;
import org.aspectj.lang.ProceedingJoinPoint;
public class MoocAspect {
public void before(){
System.out.println("*********before********");
}
public void after(){
System.out.println("*********after*********");
}
public void afterReturn(){
System.out.println("******afterReturn*****");
}
/*
* <aop:around method="around" pointcut-ref="bizservice3"/>
* 在执行around时候必须要有ProceedingJoinPoint类型的参数,并且返回一个Object值。
*
*/
public Object around(ProceedingJoinPoint pjp) {
Object object = null;
try {
System.out.println("******around1*****");
object = pjp.proceed();
System.out.println("******around2*****");
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
public void afterThrowing(){
System.out.println("**********afterThrowing*********");
}
public Object aroundParameter(ProceedingJoinPoint pjp,String name,String sex,int age){
Object o = null;
try {
System.out.println("aroundParameter1:"+name+" "+sex+" "+age);
o = pjp.proceed();
System.out.println("aroundParameter2:"+name+" "+sex+" "+age);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return o;
}
}
业务类:
package com.test.xml;
import java.io.File;
public class AspectBiz {
public void bizservice(){
System.out.println("*********执行bizservice()************");
}
public void bizreturn(){
System.out.println("**********执行bizreturn()**************");
}
public void bizaround(){
System.out.println("**********执行bizaround()**************");
}
public void bizafterthrow() throws Exception{
System.out.println("**********执行bizAfterthrow()**************");
File f =null;
f.getName();
}
/*
* <aop:pointcut id="bizservice5" expression="execution(* com.test.xml.AspectBiz.*aroundParameter(String,String,int)) and args(name,sex,age)" />
* 规定参数名字必须是name,sex,age
*/
public void bizaroundParameter(String name,String sex,int age){
System.out.println("*********bizaroundPararmeter*********");
}
}
测试类:package com.test.xml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.junit.test.UnitTestBase;
/*
* 测试类
*/
@RunWith(BlockJUnit4ClassRunner.class)
public class TestXml extends UnitTestBase {
public TestXml(){
super("classpath:applicationContext.xml");
//super("classpath:round.xml");
}
@Test
public void test1() throws Exception {
AspectBiz ab = super.getBean("aspectBiz");
// ab.bizservice();
// //从慕课网(用的spring4)看到的视频是after应该在afterreturn后执行,实验结果(用的spring3)是afterreturn在after之后
// ab.bizreturn();
// ab.bizaround();
// ab.bizafterthrow();
// ab.bizaroundParameter("zhangfei", "man", 23);
}
}
UnitTestBase类:
package com.junit.test;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTestBase {
private ClassPathXmlApplicationContext context;
private String stringXmlPath;
public UnitTestBase() {};
public UnitTestBase(String stringXmlPath) {
this.stringXmlPath = stringXmlPath;
};
@Before
public void before(){
if(stringXmlPath==null||stringXmlPath.equals("")){
stringXmlPath = "classPath*:spring-*.xml";
}
try{
context = new ClassPathXmlApplicationContext(stringXmlPath.split("[,\\s]+"));
context.start();
}catch(Exception e){
e.printStackTrace();
}
}
@After
public void after(){
context.destroy();
}
@SuppressWarnings("unchecked")
protected <T extends Object>T getBean(String beanId){
return (T)context.getBean(beanId);
}
protected <T extends Object>T getBean(Class<T> clazz){
return (T)context.getBean(clazz);
}
}
applicationContext.xml:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="mookAspect" class="com.test.xml.MoocAspect"/>
<bean id="aspectBiz" class="com.test.xml.AspectBiz"/>
<aop:config>
<aop:aspect id="mookAspectAop" ref="mookAspect">
<aop:pointcut id="bizservice1" expression="execution(public * *(..))" />
<aop:pointcut id="bizservice2" expression="execution(* com.test.xml.AspectBiz.*return(..))" />
<aop:pointcut id="bizservice3" expression="execution(* com.test.xml.AspectBiz.*around(..))" />
<aop:pointcut id="bizservice4" expression="execution(* com.test.xml.AspectBiz.*afterthrow(..))" />
<aop:pointcut id="bizservice11" expression="execution(* com.test.xml.AspectBiz.*aroundParameter(String,String,int)) and args(name,sex,age)"/>
<aop:before method="before" pointcut-ref="bizservice1"/>
<aop:after method="after" pointcut-ref="bizservice1"/>
<aop:after-returning method="afterReturn" pointcut-ref="bizservice2"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="bizservice4"/>
<aop:around method="around" pointcut-ref="bizservice3"/>
<aop:around method="aroundParameter" pointcut-ref="bizservice11"/>
</aop:aspect>
</aop:config>
</beans>
其中applicationContext.xml中的<aop:around method="around" pointcut-ref="bizservice3"/>
<aop:around method="aroundParameter" pointcut-ref="bizservice11"/>这两条配置需要挨着,否则实验测试时候总是包.xml文件初始化错误,原因未找到
再注意<aop:around method="around" pointcut-ref=""/>标签的配置,对应的method必须有一个ProceedingJoinPoint类型的参数,然后次参数对象的proceed()方法。
相关文章推荐
- Spring中AOP基于XML的两种配置方法
- Spring AOP详解及通过XML配置实现的实现方法
- spring中aop的xml配置方法实例详解
- spring读取properties和xml配置文件的方法
- 读取Spring的配置文件applicationContext.xml几种方法
- applicationContext-common.xml 之spring tx:advice 和 aop:config 配置事务属性详解
- Spring取出bean的方法和xml里的配置
- spring 中加载xml配置文件的方法
- Spring中XML配置文件的十二个最佳方法
- spring aop的使用(注解方式以及基于xml配置方式)
- 在Spring中采用声明式方法对Hibernate和JDBC进行统一的事务配置(AOP)
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- Spring中XML配置文件的十二个最佳方法(下)
- spring aop的使用(注解方式以及基于xml配置方式)
- spring.xml 配置加载jdbc.properties和ehcache.xml 的方法
- spring AOP配置的几种方法
- Spring Aop实现——Annotation方式(注解式)and Schema-based式(xml配置)
- Spring中XML配置文件的十二个最佳方法(上)
- Spring中XML配置文件的十二个最佳方法
- spring在web.xml中配置的两种方法