要用到AOP,除了两个必须的包common-logging和spring,还需要jar包:spring-aspects.jar,aspectjrt-1.5.0,aspjectjrt-1.2,cglib-2.1.3,asm-1.5.3.cglib和asm这两个包有的版本好像不行,我试过几个版本cglib-2.1.3和asm-1.5.3应该是没问题的。
集合这几个jar包我们就可以写demo了。
同样的写两个pojo类,Protelinterview和Interviewee:
package javaapplication2;
public class Interviewee {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void performe(){
System.out.println("Thank you for giving the chance to introduce myself,my name is "+this.name+" and I am "+this.age+"......");
}
}
package javaapplication2;
public class Protelinterview {
public Interviewee person;
public Interviewee getPerson() {
return person;
}
public void setPerson(Interviewee person) {
this.person = person;
}
public void sayHello()
{
System.out.println("Hi!Welcome to protel");
}
public void introduceYourself()
{
System.out.println("please introduceyourself with your personal resume");
}
public void interviewSuccess()
{
System.out.println("OK! "+person.getName()+ " You are our found,please work at next week!");
}
public void interviewFail()
{
System.out.println("Sorry! "+person.getName()+" You are not our chose");
}
}
请注意sayHello(),introduceYourself(),interviewSuccess(),interviewFail()这四个方法,我们现在是要将调用Interviewee.performe()之前先运行sayHello(),introduceYourself(),之后是interviewSuccess()或者interviewFail().
根据以上提前,我们配置文件:democonfig2.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: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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="personalinfor" class="javaapplication2.Personalinfor">
<property name="age" value="24"/>
</bean>
<bean id="daniel" class="javaapplication2.Danielinfor" >
<property name="p" ref="personalinfor"/>
</bean>
<bean id="interviewee" class="javaapplication2.Interviewee">
<property name="name" value="A"/>
<property name="age" value="24"/>
</bean>
<bean id="protelinterview" class="javaapplication2.Protelinterview">
<property name="person" ref="interviewee" />
</bean>
<aop:config>
<aop:aspect ref="protelinterview">
<aop:before method="sayHello" pointcut="execution(* *.*.performe(..))"/>
<aop:before method="introduceYourself" pointcut="execution(* *.*.performe(..))"/>
<aop:after-returning method="interviewSuccess" pointcut="execution(* *.*.performe(..))" />
<aop:after-throwing method="interviewFail" pointcut="execution(* *.*.performe(..))" />
</aop:aspect>
</aop:config>
</beans>
--我们定义了一个切面<aop:aspect ref="protelinterview">,然后切点方法<aop:before method="sayHello" pointcut="execution(* *.*.performe(..))"/>,在运行performe(..)之前,先运行sayHello.我们要注意aop:before ,aop:after-returning,aop:after-throwing这个三个的区别,第一个是performe()执行之前运行,第二个是performe()是成功执行后运行,第三个是performe()执行抛异常运行的。此外还有注意pointcut="execution(* *.*.performe(..)),第一个*指的是执行performe()后的返回类型可以是任意,*.*.performe(..)指的是方法的路径.performe(..)参数可以使任意.
写一个测试类:
package javaapplication2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Springdemo {
public static void main(String[] args) throws Exception {
ApplicationContext cet = new ClassPathXmlApplicationContext("democonfig2.xml");
Interviewee interviewee = (Interviewee)cet.getBean("interviewee");
interviewee.setName("jason");
interviewee.setAge(24);
interviewee.performe();
}
}
集合这几个jar包我们就可以写demo了。
同样的写两个pojo类,Protelinterview和Interviewee:
package javaapplication2;
public class Interviewee {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void performe(){
System.out.println("Thank you for giving the chance to introduce myself,my name is "+this.name+" and I am "+this.age+"......");
}
}
package javaapplication2;
public class Protelinterview {
public Interviewee person;
public Interviewee getPerson() {
return person;
}
public void setPerson(Interviewee person) {
this.person = person;
}
public void sayHello()
{
System.out.println("Hi!Welcome to protel");
}
public void introduceYourself()
{
System.out.println("please introduceyourself with your personal resume");
}
public void interviewSuccess()
{
System.out.println("OK! "+person.getName()+ " You are our found,please work at next week!");
}
public void interviewFail()
{
System.out.println("Sorry! "+person.getName()+" You are not our chose");
}
}
请注意sayHello(),introduceYourself(),interviewSuccess(),interviewFail()这四个方法,我们现在是要将调用Interviewee.performe()之前先运行sayHello(),introduceYourself(),之后是interviewSuccess()或者interviewFail().
根据以上提前,我们配置文件:democonfig2.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: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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="personalinfor" class="javaapplication2.Personalinfor">
<property name="age" value="24"/>
</bean>
<bean id="daniel" class="javaapplication2.Danielinfor" >
<property name="p" ref="personalinfor"/>
</bean>
<bean id="interviewee" class="javaapplication2.Interviewee">
<property name="name" value="A"/>
<property name="age" value="24"/>
</bean>
<bean id="protelinterview" class="javaapplication2.Protelinterview">
<property name="person" ref="interviewee" />
</bean>
<aop:config>
<aop:aspect ref="protelinterview">
<aop:before method="sayHello" pointcut="execution(* *.*.performe(..))"/>
<aop:before method="introduceYourself" pointcut="execution(* *.*.performe(..))"/>
<aop:after-returning method="interviewSuccess" pointcut="execution(* *.*.performe(..))" />
<aop:after-throwing method="interviewFail" pointcut="execution(* *.*.performe(..))" />
</aop:aspect>
</aop:config>
</beans>
--我们定义了一个切面<aop:aspect ref="protelinterview">,然后切点方法<aop:before method="sayHello" pointcut="execution(* *.*.performe(..))"/>,在运行performe(..)之前,先运行sayHello.我们要注意aop:before ,aop:after-returning,aop:after-throwing这个三个的区别,第一个是performe()执行之前运行,第二个是performe()是成功执行后运行,第三个是performe()执行抛异常运行的。此外还有注意pointcut="execution(* *.*.performe(..)),第一个*指的是执行performe()后的返回类型可以是任意,*.*.performe(..)指的是方法的路径.performe(..)参数可以使任意.
写一个测试类:
package javaapplication2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Springdemo {
public static void main(String[] args) throws Exception {
ApplicationContext cet = new ClassPathXmlApplicationContext("democonfig2.xml");
Interviewee interviewee = (Interviewee)cet.getBean("interviewee");
interviewee.setName("jason");
interviewee.setAge(24);
interviewee.performe();
}
}