配置文件:
<?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">
<!-- spring的注解 -->
<!-- <context:annotation-config/> -->
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="person" class="aop.Person">
<property name="message" value="123"></property>
</bean>
<!-- 切点 -->
<bean id="log" class="aop.Log"/>
<!-- 切点通知,前置通知,后置通知,success通,环绕通知 .配置各种通知-->
<!-- <aop:config>
<aop:aspect ref="log">
声明切入点,方法名。第一个*返回的参数,第二个*任意的类名名,第三个参数*是任意的,括号里面的时任意的参数
<aop:pointcut expression="execution(* aop.*.*(..))" id="oprater"/>
配置切点
<aop:before method="before" pointcut-ref="oprater"/>
<aop:after-returning method="success" pointcut-ref="oprater"/>
<aop:after method="after" pointcut-ref="oprater"/>
<aop:around method="around" pointcut-ref="oprater"/>
</aop:aspect>
</aop:config> -->
<!-- 创建bean -->
<bean id="ope" class="aop.Operate"></bean>
<bean id="aop" class="aop.Aop"></bean>
</beans>
配置文件中的注册:
<context:annotation-config>与
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>作用是一样的
实体类中的
package aop;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Person {
private String message;
public String getMessage(){
return message;
}
public void setMessage(String message){
this.message=message;
}
@PostConstruct
public void init(){
System.out.println("开始时先进行初始化"+message);
}
@PreDestroy
public void destroy(){
System.out.println("结束的时候销毁"+message);
}
}
本文介绍了一个Spring配置示例,展示了如何使用Spring AOP(面向切面编程)进行切点定义及通知配置,并利用@PostConstruct和@PreDestroy注解管理Bean的生命周期。
7823

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



