配置文件bean
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="log" class="aop.Log">
</bean>
<!-- 在aop:config中配置各种通知 -->
<aop:config>
<aop:aspect ref="log">
<!-- 声明切入点 ,方法名,第二个*表示方法名,小括号里面的俩个小..代表方法的参数-->
<aop:pointcut expression="execution( * aop.*.*(..))"
id="operate" />
<!-- 各个通知类型 -->
<aop:before method="before" pointcut-ref="operate" />
<aop:around method="around" pointcut-ref="operate"/>
<aop:after method="after" pointcut-ref="operate" />
<!-- 异常通知没有效果哦,不知道什么情况....待解决 -->
<!-- <aop:after-throwing method="error" pointcut="execution( * aop.Operate.*(..))"
/> -->
<aop:after-returning method="success" pointcut-ref="operate" />
</aop:aspect>
</aop:config>
<bean id="ope" class="aop.Operate"></bean>
<bean id="aop" class="aop.Aop"></bean>
</beans>
这个配置文件没有使用注解,标签<aop:config></aop:config>是面向切面的开始,
一下是方法中的切面和切点
====切面
package aop;
/***
*
* @author wudi
* 定义切面
*/
public class Operate {
public void parse(){
String string="12345";
try {
int s=Integer.parseInt(string);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("出错的原因"+e);
}
}
}
====切面
package aop;
/***
* 切面
* @author wudi
*
*/
public class Aop {
public void show (){
String string="12345";
try {
int s=Integer.parseInt(string);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("出错的原因"+e);
}
}
}
=====切点
package aop;
/***
*
* @author wudi
* 定义切点
*/
public class Log {
//前置通知
public void before(){
System.out.println("前置通知");
}
//环绕通知
public void around(){
System.out.println("环绕通知");
}
//后置通知
public void after(){
System.out.println("后置通知");
}
//成功通知
public void success(){
System.out.println("登陆成功");
}
//失败后通知
public void error(Exception e){
System.out.println("登陆失败 "+e.getMessage());
}
}
=====测试的切点
package aop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
@Test
public void show(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
Operate operate=(Operate)applicationContext.getBean("ope");
operate.parse();
}
@Test
public void shows(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
Aop aop =(Aop)applicationContext.getBean("aop");
aop.show();
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="log" class="aop.Log">
</bean>
<!-- 在aop:config中配置各种通知 -->
<aop:config>
<aop:aspect ref="log">
<!-- 声明切入点 ,方法名,第二个*表示方法名,小括号里面的俩个小..代表方法的参数-->
<aop:pointcut expression="execution( * aop.*.*(..))"
id="operate" />
<!-- 各个通知类型 -->
<aop:before method="before" pointcut-ref="operate" />
<aop:around method="around" pointcut-ref="operate"/>
<aop:after method="after" pointcut-ref="operate" />
<!-- 异常通知没有效果哦,不知道什么情况....待解决 -->
<!-- <aop:after-throwing method="error" pointcut="execution( * aop.Operate.*(..))"
/> -->
<aop:after-returning method="success" pointcut-ref="operate" />
</aop:aspect>
</aop:config>
<bean id="ope" class="aop.Operate"></bean>
<bean id="aop" class="aop.Aop"></bean>
</beans>
这个配置文件没有使用注解,标签<aop:config></aop:config>是面向切面的开始,
一下是方法中的切面和切点
====切面
package aop;
/***
*
* @author wudi
* 定义切面
*/
public class Operate {
public void parse(){
String string="12345";
try {
int s=Integer.parseInt(string);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("出错的原因"+e);
}
}
}
====切面
package aop;
/***
* 切面
* @author wudi
*
*/
public class Aop {
public void show (){
String string="12345";
try {
int s=Integer.parseInt(string);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("出错的原因"+e);
}
}
}
=====切点
package aop;
/***
*
* @author wudi
* 定义切点
*/
public class Log {
//前置通知
public void before(){
System.out.println("前置通知");
}
//环绕通知
public void around(){
System.out.println("环绕通知");
}
//后置通知
public void after(){
System.out.println("后置通知");
}
//成功通知
public void success(){
System.out.println("登陆成功");
}
//失败后通知
public void error(Exception e){
System.out.println("登陆失败 "+e.getMessage());
}
}
=====测试的切点
package aop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
@Test
public void show(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
Operate operate=(Operate)applicationContext.getBean("ope");
operate.parse();
}
@Test
public void shows(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
Aop aop =(Aop)applicationContext.getBean("aop");
aop.show();
}
}
本文介绍了一个基于 Spring 的面向切面编程(AOP)的实际配置案例,详细展示了如何通过 XML 配置文件来定义切面、切入点及各类通知,并提供了具体的类实现代码和测试方法。
1万+

被折叠的 条评论
为什么被折叠?



