Spring aop 编程

一.手动aop编程

接口:

/**
 * 接口
 *
 */
public interface UserService {
	
	public void addUser();
   
   public void saveUser();
}

目标类(实现类)

public class UserServiceImpl implements UserService{

	@Override
	public void addUser() {
		// TODO Auto-generated method stub
		System.out.println("添加用户");
	}

	@Override
	public void saveUser() {
		// TODO Auto-generated method stub
		System.out.println("保存用户");
	}
	
  }
通知:

/**
 * 继承aop联盟
 *
 */
public class MyAuAspect implements MethodInterceptor{
  
	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("开始执行");
		Object obj=invocation.proceed();
		System.out.println("执行结束");
		return obj;
	}

}

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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd">
    <!--切面类 -->
    <bean id="myAuAspectId" class="com.study.aop.MyAuAspect"></bean>
    <!--目标类 -->
    <bean id="userServiceId" class="com.study.poxy.UserServiceImpl"></bean>
    <!--创建代理对象  -->
    <bean id="poxyServiceId" class="org.springframework.aop.framework.ProxyFactoryBean">
    <!--在之前我们学习jdk的动态代理和CGLIB的动态代理的时候,要传输接口,目标对象,方法的回调等参数,所以我们现在也要设置这些参数  -->
    <property name="interfaces" value="com.study.poxy.UserService"></property>
    <property name="target" ref="userServiceId"></property>
    <property name="interceptorNames" value="myAuAspectId"></property>
    </bean>
</beans>

测试:

@Test
	public void Test(){
		String xmlPath="com/study/test/applicationContext.xml";
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
	UserService  userService  = (UserService) applicationContext.getBean("poxyServiceId");
	userService.addUser();
	}


AOP全自动配置:

先添加Aspectj.weaver 包

  <!--aspectj.weaver配置 -->
  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.5</version>
</dependency>

applicationContext1.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.xsd
	                    http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!--切面类 -->
    <bean id="myAuAspectId" class="com.study.aop.MyAuAspect"></bean>
    <!--目标类 -->
    <bean id="userServiceId" class="com.study.poxy.UserServiceImpl"></bean>
    <!--  
    aop配置:
        使用<aop:config>进行配置 proxy-target-class表示是否使用CGLIB动态代理
    <aop:pointcut>设置切点,就是表明那些方法会被拦截,使用其代理
    expression表达式说明:
    execution(* com.itheima.c_spring_aop.*.*(..))
    * com.itheima.c_spring_aop.*.*(..) 分别表示如下:
    返回值,         包                                  类,方法,参数                     *表示任意
    <aop:advisor> 设置切面,包括通知和切点
    -->
    
    <aop:config proxy-target-class="true">
    <aop:pointcut expression="execution(* com.study.poxy.*.*(..))" id="pointCutId"/>
    <aop:advisor advice-ref="myAuAspectId" pointcut-ref="pointCutId"/>
    </aop:config>
    
</beans>

测试:

@Test
	public void Test(){
		String xmlPath="com/study/test/applicationContext1.xml";
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
	UserService  userService  = (UserService) applicationContext.getBean("userServiceId");
	userService.addUser();
	}



猜你喜欢

转载自blog.csdn.net/strong_yu/article/details/68486167