Spring之AOP编程
package cn.african.service;
public interface UserService {
public void addUser();
public void updateUser();
public void deleteUser();
}
package cn.african.service;
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("addUser...");
}
@Override
public void updateUser() {
System.out.println("updateUser...");
}
@Override
public void deleteUser() {
System.out.println("deleteUser...");
}
}
2. 切面类
package cn.african.aspect;
public class MyAspect {
public void before() {
System.out.println("before");
}
public void after() {
System.out.println("after");
}
}
3. 工厂类
package cn.african.factory;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import cn.african.aspect.MyAspect;
import cn.african.service.UserService;
import cn.african.service.UserServiceImpl;
public class MyFactoryBean {
public static UserService createService() {
// 1. 目标对象
UserService userService = new UserServiceImpl();
// 2. 切面对象
MyAspect myAspect = new MyAspect();
// 3. 代理对象
UserService proxyService = (UserService) Proxy.newProxyInstance(
MyFactoryBean.class.getClassLoader(),
userService.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//前置通知
myAspect.before();
//执行目标类的方法
Object object = method.invoke(userService, args);
//后置通知
myAspect.after();
return object;
}
});
//4. 返回代理对象
return proxyService;
}
}
InvocationHandler h)
package cn.african.test;
import org.junit.Test;
import cn.african.factory.MyFactoryBean;
import cn.african.service.UserService;
public class TestJdkProxy {
@Test
public void testJdkProxy() {
UserService userService = MyFactoryBean.createService();
userService.addUser();
System.out.println("--------------");
userService.updateUser();
System.out.println("--------------");
userService.deleteUser();
}
}
package cn.african.service;
public class UserServiceImpl {
public void addUser() {
System.out.println("addUser...");
}
public void updateUser() {
System.out.println("updateUser...");
}
public void deleteUser() {
System.out.println("deleteUser...");
}
}
2. 切面类
package cn.african.aspect;
public class MyAspect {
public void before() {
System.out.println("before");
}
public void after() {
System.out.println("after");
}
}
package cn.african.factory;
import java.lang.reflect.Method;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import cn.african.aspect.MyAspect;
import cn.african.service.UserService;
import cn.african.service.UserServiceImpl;
public class CglibBeanFactory {
public static UserService createService() {
// 目标类
UserServiceImpl userService = new UserServiceImpl();
// 切面类
MyAspect myAspect = new MyAspect();
// 1. 核心类
Enhancer enhancer = new Enhancer();
// 2. 设置父类
enhancer.setSuperclass(UserServiceImpl.class);
// 3. 设置回调函数
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
myAspect.before();
Object object = method.invoke(userService, args);
myAspect.after();
return object;
}
});
// 4. 创建代理
UserServiceImpl proxyService = (UserServiceImpl) enhancer.create();
return proxyService;
}
}
package cn.african.test;
import org.junit.Test;
import cn.african.factory.CglibBeanFactory;
import cn.african.service.UserService;
public class TestCglibProxy {
@Test
public void demo01() {
UserService userService = CglibBeanFactory.createService();
userService.addUser();
System.out.println("--------------");
userService.updateUser();
System.out.println("--------------");
userService.deleteUser();
}
}
package org.aopalliance.aop;
/**
* Tag interface for Advice. Implementations can be any type
* of advice, such as Interceptors.
* @author Rod Johnson
* @version $Id: Advice.java,v 1.1 2004/03/19 17:02:16 johnsonr Exp $
*/
public interface Advice {}
try{
//1. 前置通知
//2. 执行目标方法
//3. 后置通知
}catch{
//4. 抛出异常通知
}
package cn.african.service;
public interface UserService {
void addUser();
void updateUser();
void deleteUser();
}
package cn.african.service;
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("addUser...");
}
@Override
public void updateUser() {
System.out.println("updateUser...");
}
@Override
public void deleteUser() {
System.out.println("deleteUser...");
}
}
package cn.african.aspect;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// 前置通知
System.out.println("before");
// 手动执行目标方法,这是一个放行方法
Object object = invocation.proceed();
// 后置通知
System.out.println("after");
return object;
}
}
3. bean-aop.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- 1. 目标类 -->
<bean id="userService" class="cn.african.service.UserServiceImpl"></bean>
<!-- 2. 切面类 -->
<bean id="myAspect" class="cn.african.aspect.MyAspect"></bean>
<!-- 3. 代理类 -->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="cn.african.service.UserService"></property>
<property name="target" ref="userService"></property>
<property name="interceptorNames" value="myAspect"></property>
<!-- <property name="optimize" value="true"></property> --> <!--如果声明optimize=true,无论是否有接口,都采用cglib代理-->
</bean>
</beans>
package cn.african.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.african.service.UserService;
public class TestAop {
@Test
public void demo01() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("beans-aop.xml");
UserService userService = context.getBean("proxyService", UserService.class);
userService.addUser();
System.out.println("----------------");
userService.updateUser();
System.out.println("----------------");
userService.deleteUser();
context.getClass().getMethod("close").invoke(context);
}
}
打印结果:

package cn.african.service;
public interface UserService {
public void addUser();
public void updateUser();
public void deleteUser();
}
package cn.african.service;
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("addUser...");
}
@Override
public void updateUser() {
System.out.println("updateUser...");
}
@Override
public void deleteUser() {
System.out.println("deleteUser...");
}
}
package cn.african.aspect;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// 前置通知
System.out.println("before");
// 手动执行目标方法,这是一个放行方法
Object object = invocation.proceed();
// 后置通知
System.out.println("after");
return object;
}
}
3. beans-aop.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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 1. 目标类 -->
<bean id="userService" class="cn.african.service.UserServiceImpl"></bean>
<!-- 2. 切面类 -->
<bean id="myAspect" class="cn.african.aspect.MyAspect"></bean>
<!-- 3. AOP配置 -->
<aop:config>
<aop:pointcut expression="execution(* cn.african.service.UserServiceImpl.*(..))" id="myPointcut"/>
<aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"/>
</aop:config>
</beans>
package cn.african.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.african.service.UserService;
public class TestAop {
@Test
public void demo01() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("beans-aop.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.addUser();
System.out.println("----------------");
userService.updateUser();
System.out.println("----------------");
userService.deleteUser();
context.getClass().getMethod("close").invoke(context);
}
}
打印结果:

Spring之AOP编程的更多相关文章
- Java基础-SSM之Spring的AOP编程
Java基础-SSM之Spring的AOP编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Spring的本质说白了就是动态代理,接下来我们会体验AOP的用法.它是对OOP的 ...
- Spring框架--AOP编程
2 手动实现AOP编程 AOP 面向切面的编程, AOP可以实现"业务代码"与"关注点代码"分离 // 保存一个用户 public void add(User ...
- Spring的AOP编程
1.手动实现AOP编程(代理模式) AOP是面向切面的编程,主要功能就是实现"业务代码"和辅助业务代码的"关注点代码"分离.在一个方法中,出了核心的业务代码,其 ...
- Java进阶知识21 Spring的AOP编程
1.概述 Aop:(Aspect Oriented Programming)面向切面编程 功能: 让关注点代码与业务代码分离! 关注点:重复代码就叫做关注点:切面: 关注点形成的类, ...
- spring的aop编程(半自动、全自动)
1.spring的半自动代理(从spring中获取代理对象) (1)spring中的通知类型 spring中aop的通知类型有五种: 前置:在目标方法执行前实施加强 后置:在目标方法执行后实施加强 环 ...
- 【AOP】spring 的AOP编程报错:[Xlint:invalidAbsoluteTypeName]error
AOP来发过程中,报错如下: warning no match for this type name: net.shopxx.wx.institution.controller [Xlint:inva ...
- spring相关—AOP编程—切入点、连接点
1 切入点表达式 1.1 作用 通过表达式的方式定位一个或多个具体的连接点. 1.2 语法细节 ①切入点表达式的语法格式 execution([权限修饰符] [返回值类型] [简单类名/全类名] [方 ...
- Spring基础篇——Spring的AOP切面编程
一 基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...
- Aop编程--注解与xml的实现
一.注解方式 1.首先引入spring对于aop编程的jar支持包,spring框架没有的包请自行在网上下载. aopalliance-alpha1.jar aspectjrt.jar aspectj ...
随机推荐
- Linux Centos 下安装软件 三种方式(转)
Linux学习的路还很远呢,各位码农,新年快乐哈! 1)一种是软件的源代码,您需要自己动手编译它.这种软件安装包通常是用gzip压缩过的tar包(后缀为.tar.gz). 2)另一种是软件的可执行程序 ...
- 兄弟连PHP培训教你提升效率的20个要点
兄弟连PHP培训教你提升效率的20个要点 用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字 ...
- 第八届蓝桥杯省赛17【java B组】第一题
1,标题: 购物单 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞. 这不,XX大促销又来了!老板夫人开出了长长的购 ...
- Intellij idea操作maven时控制台中文乱码
只留存记录 windows环境下,Intellij idea12中maven操作时,控制台中文乱码问题(编译报错或者clean install时出现的其他错误描述乱码) 在cmd中mvn中文正常显示, ...
- c++ --> #define中的三个特殊符号:#,##,#@
#define中的三个特殊符号:#,##,#@ 看下面三个define宏定义: #define Conn(x,y) x##y #define ToChar(x) #@x #define ToStrin ...
- 数据库 --> MySQL使用
MySQL使用 代码: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>#includ ...
- java枚举类型变通
原始用法 public enum Color { RED, GREEN, BLANK, YELLOW } 开发中用法 public enum ApiCodeEnum { SUCCESS(0," ...
- 关于使用Mybatis的使用说明(一)【未完善待更新】
(一)搭建Mybatis环境 (1)先导入常用的jar包:并且需要将lib文件夹下的包导入到项目中 (2)创建config文件夹,配置log4j.properties文件 # Global loggi ...
- 使用git将文件上传到Coding
1,首先在Coding上新建项目. 2,填写项目的相关内容. 3,建立项目后复制下面鼠标所选内容. 4,在自己的电脑中建立文件夹. 5,进入该文件夹后,点击鼠标右键,然后再点Git Clone. 6 ...
- C语言--第14.15周作业
一. 7-3 将数组中的数逆序存放 1.代码 #include 2<stdio.h> int main() { int a[10]; int i, n, s; scanf("%d ...