Spring中的Aop即:面向切面

来源:互联网 发布:徐州八方网络 编辑:程序博客网 时间:2024/05/18 16:13

关于今天学习的Aop主要是两点

一:动态代理模式,二:拦截模式

一般状况下,aop和IOC是一块使用的

所讲第一点:就是WriteLog中的日志实现

还是沿用之前的例子。我们在之前的基础上再添加一个Animal1的类,在这个类中,我们定义两个参数:一个是animalname,一个是animalsex(分别获取到他们的get和set方法);

然后,同样的我们在Animal接口类中定义两个方法name和sex,这两个方法都交给AniamlImpl来实现。同样的在ManagerAnimal中,定义一个animal对象参数。记得,一定要获取到他的set值,因为这是依赖植入。与昨天不同的是,在aop中,我们需要对我们所调试程序的内容进行统计,而要完成这项工作,在aop中,他有专门的方法,ProceedingJoinPoint表示的就是注入点,表示在什么地方开始拦截。关于拦截的条件,会在我们所配备的test.xml文件中(这个完由自己写)

现在,我们先来写writeLog这个类。

public class WriteLog {
//加入切入点
 public void writeLog(ProceedingJoinPoint joinpoint) throws Throwable{

//这一块所用的是动态代理,因为在Aop( joinpoint.)的底层中,已经实现了这个动态代理的内容(暂且这么理解)

//所以,这个方法就等同与以前所学习的动态代理中的invoke方法
String result=(String) joinpoint.proceed();
System.out.println("切入点输出为"+result);
 }
}

然后我们来在test.xml中进行文件的配备,源码奉上

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

//从此行开始,所配备的才是针对此类所写的内容,之前的都是一样。当然,在此之前,我们也是需要传进去关于Spring的3.0以上的架包(此处所用为4.0)
<bean id="log" class="Test.WriteLog"></bean>
<!-- 配备切入点 -->
<!-- 配置两个切入点 -->
<aop:config > 
<!-- 把log植入,把代理类置入 -->
  <aop:aspect ref="log">
  <!-- 定义切入点 -->
  <!-- aop包下的所有以name开头的方法 -->
  <aop:pointcut expression="execution(* Test.*.name*(..))" id="nameMethod"/>
  <aop:around method="writeLog" pointcut-ref="nameMethod"/>
  </aop:aspect>
  
</aop:config>
<aop:config >
<!-- 把log植入,把代理类置入 -->
  <aop:aspect ref="log">
  <!-- 定义切入点 -->
  <!-- aop包下的所有以sex开头的方法 -->
  <aop:pointcut expression="execution(* Test.*.sex*(..))" id="sexMethod"/>
  <aop:around method="writeLog" pointcut-ref="sexMethod"/>
  </aop:aspect>
  
</aop:config>
       <bean id="animal" class="Test.AnimalImpl" scope="singleton"></bean>
       <bean id="manageranimal" class="Test.ManagerAnimal" scope="singleton">
      <property name="animal" ref="animal"></property>
        </bean>
       </beans>

这个配置完成后,今天所使用的调试方法也会改变

我们使用的是JUnit4需要导入此包(另外需要添加四个包)

所以,我们需要在web.xml文件进行相关操作的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

//因为在使用这种方法测试时,我们需要用的有监听器
  <listener>
  <listener-class >org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- spring配置文件 -->
  <context-param>
  <param-name>contextConfigLocation</param-name> 
  <param-value>classpath:test.xml</param-value>
  <!-- 如果将xml放在WEB-INF中,表示方式为/WEB-INF/bean.xml -->
  </context-param>
</web-app>

当然,我们还要重写一个测试类TestService

package Test;


import javax.annotation.Resource;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


/**
 * 调试
 * @author Administrator
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)//指定测试用例的运行器
@ContextConfiguration(locations="classpath:test.xml")
public class TestService {
   private ManagerAnimal manageranimal;




@Resource
public void setManageranimal(ManagerAnimal manageranimal) {
this.manageranimal = manageranimal;
}
@Test
   public void name(){
  manageranimal.name();
   }
}

在这里一定要主要,对应你写的"classpath:test.xml",在这里,今天就错了两次。切记

另外,关于@Resource和@Test一定要写

截止到这,比较重要的我们就算写完了

现在附上全部代码

public interface Animal {
 //他有两个方法
public void name(Animal1 animal);
public void sex(Animal1 animal);

}

public class Animal1 {
 private String animalname;
 private String animalsex1;
public String getAnimalname() {
return animalname;
}
public void setAnimalname(String animalname) {
this.animalname = animalname;
}
public String getAnimalsex1() {
return animalsex1;
}
public void setAnimalsex1(String animalsex1) {
this.animalsex1 = animalsex1;
}


}

public class AnimalImpl implements Animal{





public void name(Animal1 animal) {
// TODO Auto-generated method stub
System.out.println("这是一只狮子");
}


public void sex(Animal1 animal) {
// TODO Auto-generated method stub
System.out.println("这只狮子是公狮");
}


}

public class ManagerAnimal {
//定义一个Animal对象
private Animal animal;
//获取get,set方法


public Animal getAnimal() {
return animal;
}


public void setAnimal(Animal animal) {
this.animal = animal;
}
//调用animal中name 方法
public void name(){
Animal1 animal=new Animal1();
this.animal.name(animal);
}
}

这只是上午所学

0 0