当前位置: 首页 > news >正文

做甜品的网站网站整站优化方案

做甜品的网站,网站整站优化方案,品牌策划公司经营范围包括哪些,广告设计公司vi一、IOC容器 1.1 基础 1.1.1 容器 1、Spring框架的主要功能是通过其核心容器来实现的。2、Spring容器是生成Bean的工厂#xff0c;它负责创建Bean的实例#xff0c;并管理其生命周期。所有的组件都被当成Bean处理#xff0c;例如数据源、Hibernate的SessionFactory、事务管…一、IOC容器 1.1 基础 1.1.1 容器 1、Spring框架的主要功能是通过其核心容器来实现的。2、Spring容器是生成Bean的工厂它负责创建Bean的实例并管理其生命周期。所有的组件都被当成Bean处理例如数据源、Hibernate的SessionFactory、事务管理器等。应用中的所有组件都处于Spring的管理之下都被Spring以Bean的方式管理。Bean是Spring容器的基本单位。3、Spring核心容器是由什么组成的Spring的核心容器由Beans、Core、Context、SpEL等模块组成。所有Spring的其他模块都是建立在Core基础模块上的。该模块规定了创建和维护Bean的方式提供了控制反转IoC和依赖注入DI等特性。4、Spring有两个核心接口分别为BeanFactory和ApplicationContext。其中ApplicationContext是BeanFactory的子接口它们都可代表Spring容器。也就是说Spring框架提供了两种核心容器BeanFactory和ApplicationContext。 1.1.2 容器的实现 ​ 1、Spring 框架带有两个 IOC 容器 —— BeanFactory和ApplicationContext。2、BeanFactory是Spring容器最基本的接口。ApplicationContext是BeanFactory的子接口。​ 1pring容器最基本的接口就是BeanFactory。Spring Ioc容器的实现从根源上是Beanfactory。2BeanFactory是 IOC 容器的最基本版本ApplicationContext扩展了BeanFactory的特性。 1、BeanFactory1BeanFactory是Spring容器最基本的接口。他是 IOC 容器的最基本版本。​BeanFactory是Spring中最基本的接口它是Spring IoC容器中最底层的接口提供了IoC容器最基本的形式它具有最基本的 IoC 功能负责管理 Spring Bean 的生命周期通过 BeanFactory 可以获取指定 Bean 的实例。 2BeanFactory就是一个管理Bean的工厂它主要负责初始化各种Bean并调用它们的生命周期方法。3BeanFactory接口提供了几个实现类其中最常用的是org.springframework.beans. factory.xml.XmlBeanFactory该类会根据XML配置文件中的定义来装配Bean。4创建BeanFactory beanFactory new XmlBeanFactory(new FileSystemResource(D:/applicationContext.xml));2、ApplicationContext1ApplicationContext是BeanFactory的子接口也被称为应用上下文是另一种常用的Spring核心容器。ApplicationContext是BeanFactory的子接口ApplicationContext继承了BeanFactory接口的全部功能同时还提供了其他的一些功能。ApplicationContext是Spring中最重要的接口之一它是Spring容器的具体实现.在BeanFactory的基础上添加了一些更加实用的功能比如资源加载、事件发布、AOP和事务等。 因此可以认为ApplicationContext继承了BeanFactory拥有了更多的功能和扩展性。​ 2BeanFactory是 IOC 容器的最基本版本ApplicationContext扩展了BeanFactory的特性。 1.1.2.1 BeanFactory创建 Testpublic void testIOC() {//加载配置文件,创建Bean工程Resource rs(Resource) new ClassPathResource(org/jsoft/a_hello/applicationContext.xml);BeanFactory factorynew XmlBeanFactory(rs);//获取对象User u(User) factory.getBean(user);System.out.println(u.getAge());}1.1.2.2 ApplicationContext创建 基于ClassPathXmlApplicationContext ClassPathXmlApplicationContext作用加载classpath根目录下的配置文件创建IOC容器。1、加载一个配置文件。然后创建IOC容器。加载classpeth根类路径下ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);加载指定类路径下ApplicationContext acnew ClassPathXmlApplicationContext(org/jsoft/a_hello/applicationContext.xml);2、加载多个配置文件。然后创建IOC容器。ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml,app2.xml); public class AppStart {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);Car car ac.getBean(Car.class);System.out.println(car);}} 基于AnnotationConfigApplicationContext AnnotationConfigApplicationContext作用加载配置类创建IOC容器。a、指定一个或多个配置类,创建IOC容器ApplicationContext acnew AnnotationConfigApplicationContext(MyConfig.class);ApplicationContext acnew AnnotationConfigApplicationContext(MyConfig.class,MyConfig2.class);c、指定一个或多个扫描路径,创建IOC容器ApplicationContext acnew AnnotationConfigApplicationContext(com.hlp.spring.ioc);ApplicationContext acnew AnnotationConfigApplicationContext(com.hlp.spring.ioc,com.hlp.spring.ioc2);c、不指定配置类创建IOC容器。【手动注册配置类】//创建无配置类的IOC容器AnnotationConfigApplicationContext ac new AnnotationConfigApplicationContext();//手动注册配置类【注册后必须刷新】ac.register(MyConfig.class);ac.refresh(); 1.1.2.3 BeanFactory和ApplicationContext的区别 1、BeanFactory与ApplicationContext的区别1ApplicationContext、BeanFactory都是IOC容器。 2ApplicationContext是BeanFactory的子接口。BeanFactory是 IOC 容器的最基本版本。ApplicationContext扩展了BeanFactory的特性ApplicationContext功能肯定比BeanFactory的功能更强大。3ApplicationContext容器在初始化的时候就会初始化Bean。而BeanFactory容器中的Bean是懒加载在初始化的时候不会初始化Bean只有在调用getBean时才会创建对象。 1.1.2 类图详解 类图1 1、BeanFactory是 IOC 容器的最基本版本。它是最基础的容器接口。2、XmlBeanFactory是BeanFactory的直接实现。 类图2 1.2 BeanFatory 1.2.1 概念 1. 什么是BeanFactoryBeanFactory是Spring框架中的一个接口它是一个工厂类用来创建和管理Spring中的Bean对象。BeanFactory接口定义了Spring容器的基本规范和行为它提供了一种机制来将配置文件中定义的Bean实例化、配置和管理起来。2. BeanFactory的作用BeanFactory的主要作用是提供Bean的创建、配置、初始化和销毁等基本操作它可以根据配置文件或注解来创建并管理Bean实例并提供了各种方法来获取和操作Bean实例。3. BeanFactory的实现类BeanFactory接口有多个实现类其中最常用的是XmlBeanFactory和DefaultListableBeanFactory。XmlBeanFactory是通过XML文件来配置Bean的实例化、配置和管理的而DefaultListableBeanFactory则是通过Java代码来配置Bean的实例化、配置和管理的。下面我们将详细介绍BeanFactory的使用。 1.2.2 基于BeanFatory创建IOC容器 创建IOC容器BeanFactory ac new XmlBeanFactory(new ClassPathResource(applicationContext.xml)); User user (User) ac.getBean(user);System.out.println(user); 案例 User.java public class User {public User() {System.out.println(构造方法调用);} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean classcom.demo.aop.User iduser/bean/beans测试 其中ClassPathResource是一个用于从classpath中加载资源文件的类。XmlBeanFactory是Spring提供的一个实现了BeanFactory接口的IoC容器实例。在实例化XmlBeanFactory时需要提供一个Resource对象这里传入的是spring-config.xml文件。 public class App {public static void main(String[] args) {BeanFactory ac new XmlBeanFactory(new ClassPathResource(applicationContext.xml));System.out.println();User user (User) ac.getBean(user);System.out.println(user);} } 1.2.3 BeanFactory的配置 在BeanFactory的配置中主要包括Bean的定义、依赖和属性等方面。 1Bean的定义 在Bean的定义中主要包括Bean的类型、ID和作用域等方面。下面是一个简单的Bean定义示例 bean iduserService classcom.example.UserService scopesingleton/2Bean的依赖 在Bean的依赖中主要包括Bean之间的依赖关系和依赖注入方式等方面。下面是一个简单的Bean依赖示例 bean iduserService classcom.example.UserServiceproperty nameuserDao refuserDao/ /beanbean iduserDao classcom.example.UserDaoImplproperty namedataSource refdataSource/ /beanbean iddataSource classorg.apache.commons.dbcp2.BasicDataSourceproperty namedriverClassName valuecom.mysql.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/test/property nameusername valueroot/property namepassword value123456/ /bean3Bean的属性 在Bean的属性中主要包括Bean的各种属性信息如普通属性、集合属性和引用属性等。下面是一个简单的Bean属性示例 bean iduserService classcom.example.UserServiceproperty namename valueJohn/property nameage value30/property namehobbieslistvaluereading/valuevaluewriting/valuevaluetraveling/value/list/propertyproperty nameuserDao refuserDao/ /bean1.2.4 BeanFactory的初始化 在BeanFactory的初始化中主要包括BeanFactoryAware接口、InitializingBean接口和init-method属性等方面。 1BeanFactoryAware接口 如果一个Bean实现了BeanFactoryAware接口那么它将能够获取到当前Bean所在的BeanFactory实例。下面是一个简单的BeanFactoryAware接口示例 public class MyBean implements BeanFactoryAware {private BeanFactory beanFactory;Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.beanFactory beanFactory;}} 2InitializingBean接口 如果一个Bean实现了InitializingBean接口那么它将能够在Bean实例化后、依赖注入后、属性设置后进行一些初始化操作。下面是一个简单的InitializingBean接口示例 public class MyBean implements InitializingBean {Overridepublic void afterPropertiesSet() throws Exception {// do something after bean initialization}}在上面的示例中MyBean实现了InitializingBean接口并重写了afterPropertiesSet()方法。该方法将在Bean实例化、依赖注入和属性设置后被调用可以在该方法中进行一些初始化操作。 3init-method属性 如果一个Bean配置了init-method属性那么在Bean实例化、依赖注入和属性设置后该方法将被调用来进行一些初始化操作。下面是一个简单的init-method属性示例 bean idmyBean classcom.example.MyBean init-methodinit在上面的示例中定义了一个id为myBean的Bean并配置了init-method属性为init。在Bean实例化、依赖注入和属性设置后Spring容器将调用MyBean类中的init()方法进行初始化操作。 4BeanFactory的销毁 1DisposableBean接口 如果一个Bean实现了DisposableBean接口那么它将能够在Bean销毁前进行一些操作。下面是一个简单的DisposableBean接口示例 public class MyBean implements DisposableBean {Overridepublic void destroy() throws Exception {// do something before bean destruction}}在上面的示例中MyBean实现了DisposableBean接口并重写了destroy()方法。该方法将在Bean销毁前被调用可以在该方法中进行一些清理操作。 2destroy-method属性 如果一个Bean配置了destroy-method属性那么在Bean销毁前该方法将被调用来进行一些清理操作。下面是一个简单的destroy-method属性示例 bean idmyBean classcom.example.MyBean destroy-methodcleanup 在上面的示例中定义了一个id为myBean的Bean并配置了destroy-method属性为cleanup。在Bean销毁前Spring容器将调用MyBean类中的cleanup()方法进行清理操作。 1.3 ApplicationContext 1.3.1 概念 1、BeanFactory是Spring中最基本的接口它是Spring IoC容器中最底层的接口提供了IoC容器最基本的形式它具有最基本的 IoC 功能负责管理 Spring Bean 的生命周期通过 BeanFactory 可以获取指定 Bean 的实例。 2、ApplicationContext是BeanFactory的子接口ApplicationContext继承了BeanFactory接口的全部功能同时还提供了其他的一些功能。ApplicationContext是Spring中最重要的接口之一它是Spring容器的具体实现。在BeanFactory的基础上添加了一些更加实用的功能比如资源加载、事件发布、AOP和事务等。 因此可以认为ApplicationContext继承了BeanFactory拥有了更多的功能和扩展性。 1.3.2 ApplicationContex源码分析 ApplicationContext继承的接口与功能 ResourceLoader加载资源文件MessageSource国际化消息的源头ApplicationEventPublisher应用事件发布EnvironmentCapable获取当前应用的环境信息ListableBeanFactory提供了批量操作Bean的方法HierarchicalBeanFactory层次性的BeanFactory支持子容器AutowireCapableBeanFactoryBean自动装配的BeanFactoryBeanDefinitionRegistry定义和注册BeanDefinition的接口ConfigurableApplicationContextApplicationContext可配置的接口对外提供了修改bean定义、激活环境、注册shut-down hook等能力。 1、ResourceLoader 接口 这段示例代码通过ApplicationContext实例化了一个ResourceLoader对象然后通过getResource方法加载了classpath:test.txt文件。这个文件在classpath路径下所以可以使用 classpath: 前缀来定位文件。 public class ResourceLoaderExample { public static void main(String[] args) throws IOException { ApplicationContext context new ClassPathXmlApplicationContext(spring-config.xml); ResourceLoader loader context; Resource resource loader.getResource(classpath:test.txt); InputStream inputStream resource.getInputStream(); BufferedReader reader new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line reader.readLine()) ! null) { System.out.println(line); } reader.close(); } } 2、ApplicationEventPublisher 接口 我们可以在启动时发布一个事件 public class MyApplicationEvent extends ApplicationEvent { public MyApplicationEvent(Object source) { super(source); } } public class PublishEventExample { public static void main(String[] args) { ConfigurableApplicationContext context new ClassPathXmlApplicationContext(spring-config.xml); context.addApplicationListener(new ApplicationListenerMyApplicationEvent() {Override public void onApplicationEvent(MyApplicationEvent event) {System.out.println(MyApplicationEvent received!); } }); context.publishEvent(new MyApplicationEvent(Hello World!)); context.close(); } } 当程序成功运行时我们可以看到输出了 MyApplicationEvent received! 的信息。 3、EnvironmentCapable 接口 这个接口用于获取当前运行的环境信息 public class EnvironmentExample { public static void main(String[] args) { ApplicationContext context new ClassPathXmlApplicationContext(spring-config.xml); Environment environment context.getEnvironment(); System.out.println(The environment is : environment.getProperty(os.name)); } } 1.3.3 基于ApplicationContext创建IOC容器 该接口具有三个常用的实现类 1、ClassPathXmlApplicationContext可以加载类路径下的配置文件要求配置文件必须在类路径之下。 2、FileSystemXmlApplicationContext可以加载磁盘中任意路径下的配置文件要求具有访问权限。 3、AnnotationConfigApplicationContext用于读取注解创建容器。 方式一通过ClassPathXmlApplicationContext创建//初始化spring容器加载配置文件ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);方式二通过FileSystemXmlApplicationContext创建//初始化spring容器加载配置文件ApplicationContext applicationContext new FileSystemXmlApplicationContext(applicationContext.xml););方式三通过ClassPathXmlApplicationContext创建//初始化spring容器加载配置文件ApplicationContext applicationContext new AnnotationConfigApplicationContext(); 二、Spring Aop AOP:面相切面编程OOP面相对象编程AOP是OOP的一个有效补充。 2.1 代理模式 AOP本身是基于动态代理模式实现的所以掌握代理模式是我们学好AOP的一个重要的前提条件 2.1.1 静态代理 若代理类在程序运行前就已经存在那么这种代理方式被成为**静态代理** 这种情况下的代理类通常都是我们在]ava代码中定议的。通常情况下静态代理中的代理类和目标类会实现同一接口或是派生自相同的父类。 1创建公共接口 ISomeService.java public interface ISomeService {public String doSomeThing(String msg); } 2创建目标对象 SomeServiceImpl .java public class SomeServiceImpl implements ISomeService{Overridepublic String doSomeThing(String msg) {System.out.println(目标对象执行了...msg);return 目标对象执行了...msg;} }3创建代理对象 SomeServiceProxy .java public class SomeServiceProxy implements ISomeService{private ISomeService someService;public SomeServiceProxy(ISomeService someService) {this.someService someService;}Overridepublic String doSomeThing(String msg) {System.out.println(目标对象执行前);//目前对象执行String result someService.doSomeThing(msg);System.out.println(目标对象执行后);return result;} } 4测试 App .java public class App {public static void main(String[] args) {//创建目标对象ISomeService someServicenew SomeServiceImpl();//创建代理类SomeServiceProxy proxynew SomeServiceProxy(someService);//执行String s proxy.doSomeThing(123);} } 2.1.2 动态代理 代理类在程序运行时创建的代理方式被成为动态代理。也就是说这种情况下代理类并不是在java代码中定义的而是在运行时根据我们在]ava代码中的“指示”动态生成的。 2.1.2.1 JDK动态代理 1 核心 对于实现接口的目标对象如果想对进行增强可以使用JDK动态代理。 Object o Proxy.newProxyInstance(target.getClass().getClassLoader() //获取目标对象的类加载器, target.getClass().getInterfaces() //获取目标对象的所有实现的接口, new InvocationHandler() {/*** 代理对象执行的方法* 会在该方法中执行目标对象的方法** return* throws Throwable*/Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println(目标对象执行方法前...);//执行目标对象的方法Object result method.invoke(target, args);System.out.println(目标对象执行方法后...);return result;}});o.doSomeThing(123);} }2 案例 1创建公共接口 ISomeService.java public interface ISomeService {public String doSomeThing(String msg); } 2创建目标对象 SomeServiceImpl .java public class SomeServiceImpl implements ISomeService{Overridepublic String doSomeThing(String msg) {System.out.println(目标对象执行了...msg);return 目标对象执行了...msg;} }3JDK动态代理 public class App {public static void main(String[] args) {//目标对象ISomeService targetnew SomeServiceImpl();ISomeService o (ISomeService) Proxy.newProxyInstance(target.getClass().getClassLoader() //获取目标对象的类加载器, target.getClass().getInterfaces() //获取目标对象的所有实现的接口, new InvocationHandler() {/*** 代理对象执行的方法* 会在该方法中执行目标对象的方法** return* throws Throwable*/Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println(目标对象执行方法前...);//执行目标对象的方法Object result method.invoke(target, args);System.out.println(目标对象执行方法后...);return result;}});o.doSomeThing(123);} }2.1.2.2 CGLIB动态代理 如果目标对象没有实现任何的接口那么我们是使用不了JDK代理模式的这时我们只能通过CGLIB代理来实现。CGLIB的实现本质是继承. 1引入cglib包 1创建目标对象 /*** 目标对象* 目标对象没有实现任何的接口*/ public class SomeServiceImpl {public String doSomeThing(String msg) {System.out.println(目标对象执行了...msg);return 目标对象执行了...msg;} }2创建代理对象工厂 /*** Cglib代理工厂*/ public class CgligProxy implements MethodInterceptor {/*** 传入目标对象*/private SomeServiceImpl target;public CgligProxy(SomeServiceImpl someService) {this.target someService;}/*** 对外提供创建代理对象的方法** return*/public SomeServiceImpl createProxy(){//创建增强器Enhancer enhancernew Enhancer();//指定父类enhancer.setSuperclass(SomeServiceImpl.class);//指定回调接口对象。enhancer.setCallback(this);//返回创建的代理对象return (SomeServiceImpl) enhancer.create();}/*** 和JDK动态代理中的Invoke方法类似* param o* param method* param objects* param methodProxy* return* throws Throwable*/Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println(目标对象执行前...);//目标对象执行Object result method.invoke(target, objects);System.out.println(目标对象执行后...);return result;} } 3测试 public class App {public static void main(String[] args) {SomeServiceImpl targetnew SomeServiceImpl();CgligProxy cgligProxynew CgligProxy(target);SomeServiceImpl proxy cgligProxy.createProxy();proxy.doSomeThing(123);} } 2.2 AOP概念 1、定义:AOP (Aspect Oriented Programming),即面向切面编程可以说是OOP(Obiect Oriented Programming面向对象编程)的补充和完善。面向切面是面向对象中的一种方式而已。在代码执行过程中动态嵌入其他代码叫做面向切面编程。常见的使用场景:日志。事务数据库操作....2、实质:面向切面编程就是将交又业务逻辑封装成切面利用AOP的功能将切面织入到主业务逻辑中所谓交叉业务罗辑是指诵用的、与主业务逻辑无关的代码如安全检查、事务、日志等。若不使用AOP则会出现代码纠缠即交叉业务逻辑与主业务逻辑混合在一起。这样会使主业务逻辑变的混杂不清 1AOP术语 切面将交叉业务逻辑从主逻辑中抽取的代码即是切面。织入将交叉业务逻辑代码插入到目标对象中执行。目标对象需要被增强的对象代理对象Spring代理目标对象也就是AOP代理对象连接点可以切入的目标对象的所有的方法。切入点需要切入的目标对象的方法。通知通知可以说是切面的具体实现即被抽取的交叉业务逻辑代码 2面相切面编程核心概念 3AOP的具体实现 AOP面向切面编程这个概念并不是spring发明的这种思想一直存在。所以aop实现有很多种。今天我们讲两种。1、基于纯代理的AOP该方式是由spring提供的ProxyFactoryBean实现的。2、基于aspectJ的AOP对于AOP这种编程思想很多框架都进行了实现。Spring就是其中之一可以完成面向切面编程。然而Aspect也实现了AOP的功能且其实现方式更为简捷使用更为方便而且还支持注解式开发。所以spring又将Aspect的对于AOP的实现也引入到了自己的框架中。在Spring中使用AOP开发时一般使用Aspectl的实现方式 2.3 AOP的具体实现 – 基于纯代理的AOP 2.3.1 基础 1、基于纯代理的AOP实质上是使用了spring提供的ProxyFactoryBean来实现的。2、ProxyFactoryBean是FactoryBean内部使用了JDK动态代理来实现代理对象。本质上还是一个创建Bean的工厂。 1环境准备 基于纯代理的AOP需导入cglib-nodep包 2 通知类型 2.3.2 实现 1前置通知 前置通知在目标方法执行之前执行的通知不能修改代理类的方法返回的结果 ISomeService.java public interface ISomeService {public void doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService{Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyMethodBeforeAdvice.java /*** 前置通知切面类*/ public class MyMethodBeforeAdvice implements MethodBeforeAdvice {Overridepublic void before(Method method, Object[] objects, Object o) throws Throwable {System.out.println(前置通知执行了);} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--注册目标类--bean idsomeService classcom.demo.service.SomeServiceImpl/bean!--注册前置通知--bean idmyMethodBeforeAdvice classcom.demo.MyMethodBeforeAdvice/bean!--创建代理类--bean classorg.springframework.aop.framework.ProxyFactoryBean idmyProxyFactory!--指定目标对象--property nametarget refsomeService/property!--指定目标对象实现的接口--property nameinterfaces valuecom.demo.service.ISomeService/property!--注入前置通知--property nameinterceptorNameslist!--前置通知--valuemyMethodBeforeAdvice/value/list/property/bean/beans测试 public class App {public static void main(String[] args) {ApplicationContext ac new ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(myProxyFactory);someService.doSomething(!23);} } 2后置通知 后置通知在目标方法执行之后执行的通知不可以修改代理类的方法返回的结果 ISomeService.java public interface ISomeService {public void doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService{Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyMethodAfterAdvice .java /*** 后置通知切面*/ public class MyMethodAfterAdvice implements AfterReturningAdvice {Overridepublic void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {System.out.println(后置通知执行了);} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--注册目标类--bean idsomeService classcom.demo.service.SomeServiceImpl/bean!--注册后置通知--bean idmyMethodAfterAdvice classcom.demo.MyMethodAfterAdvice/bean!--创建代理类--bean classorg.springframework.aop.framework.ProxyFactoryBean idmyProxyFactory!--指定目标对象--property nametarget refsomeService/property!--指定目标对象实现的接口--property nameinterfaces valuecom.demo.service.ISomeService/property!--注入前置通知--property nameinterceptorNameslist!--后置通知--valuemyMethodAfterAdvice/value/list/property/bean/beans测试 public class App {public static void main(String[] args) {ApplicationContext ac new ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(myProxyFactory);someService.doSomething(!23);} } 3环绕通知 环绕通知就是在切入点方法之前前后都会织入的方式而且环绕通知相比于前置通知和后置通知来说可以修改返回结果 ISomeService.java public interface ISomeService {public void doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService{Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyMethodAroundAdvice .java /*** 环绕通知切面*/ public class MyMethodAroundAdvice implements MethodInterceptor {Overridepublic Object invoke(MethodInvocation methodInvocation) throws Throwable {System.out.println(环绕通知1);Object result methodInvocation.proceed();System.out.println(环绕通知2);return result;} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--注册目标类--bean idsomeService classcom.demo.service.SomeServiceImpl/bean!--注册环绕通知--bean idmyMethodAroundAdvice classcom.demo.MyMethodAroundAdvice/bean!--创建代理类--bean classorg.springframework.aop.framework.ProxyFactoryBean idmyProxyFactory!--指定目标对象--property nametarget refsomeService/property!--指定目标对象实现的接口--property nameinterfaces valuecom.demo.service.ISomeService/property!--注入前置通知--property nameinterceptorNameslist!--环绕通知--valuemyMethodAroundAdvice/value/list/property/bean/beans测试 public class App {public static void main(String[] args) {ApplicationContext ac new ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(myProxyFactory);someService.doSomething(!23);} } 4异常通知 异常通知在目标方法执行抛出异常后执行的通知异常通知实现的ThrowsAdvice接口该接口为一个标志接口没有任何方法。需手动写一个afterThrowing方法。 ISomeService.java public interface ISomeService {public void doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService{Overridepublic void doSomething(String msg) {int i1;System.out.println(i/0); //抛出异常触发异常通知System.out.println(SomeServiceImpl:msg);} } MyMethodThrowAdvice .java /*** 异常通知切面类*/ public class MyMethodThrowAdvice implements ThrowsAdvice {/*** 异常通知** param ex 异常通知类型*/public void afterThrowing(Exception ex){System.out.println(异常通知执行了);}} applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--注册目标类--bean idsomeService classcom.demo.service.SomeServiceImpl/bean!--注册异常通知--bean idmyMethodThrowAdvice classcom.demo.MyMethodThrowAdvice/bean!--创建代理类--bean classorg.springframework.aop.framework.ProxyFactoryBean idmyProxyFactory!--指定目标对象--property nametarget refsomeService/property!--指定目标对象实现的接口--property nameinterfaces valuecom.demo.service.ISomeService/property!--注入前置通知--property nameinterceptorNameslistvaluemyMethodThrowAdvice/value/list/property/bean/beans测试 public class App {public static void main(String[] args) {ApplicationContext ac new ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(myProxyFactory);someService.doSomething(!23);} } 5基于纯代理的AOP的案例 ISomeService.java public interface ISomeService {public void doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService{Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyMethodBeforeAdvice .java /*** 前置通知切面类*/ public class MyMethodBeforeAdvice implements MethodBeforeAdvice {Overridepublic void before(Method method, Object[] objects, Object o) throws Throwable {System.out.println(前置通知执行了);} } MyMethodAfterAdvice .java /*** 后置通知切面*/ public class MyMethodAfterAdvice implements AfterReturningAdvice {Overridepublic void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {System.out.println(后置通知执行了);} } MyMethodAroundAdvice .java /*** 环绕通知切面*/ public class MyMethodAroundAdvice implements MethodInterceptor {Overridepublic Object invoke(MethodInvocation methodInvocation) throws Throwable {System.out.println(环绕通知1);Object result methodInvocation.proceed();System.out.println(环绕通知2);return result;} } MyMethodThrowAdvice .java /*** 异常通知切面类*/ public class MyMethodThrowAdvice implements ThrowsAdvice {/*** 异常通知** param ex 异常通知类型*/public void afterThrowing(Exception ex){System.out.println(异常通知执行了);}} applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--注册目标类--bean idsomeService classcom.demo.service.SomeServiceImpl/bean!--注册前置通知--bean idmyMethodBeforeAdvice classcom.demo.MyMethodBeforeAdvice/bean!--注册后置通知--bean idmyMethodAfterAdvice classcom.demo.MyMethodAfterAdvice/bean!--注册环绕通知--bean idmyMethodAroundAdvice classcom.demo.MyMethodAroundAdvice/bean!--注册异常通知--bean idmyMethodThrowAdvice classcom.demo.MyMethodThrowAdvice/bean!--创建代理类--bean classorg.springframework.aop.framework.ProxyFactoryBean idmyProxyFactory!--指定目标对象--property nametarget refsomeService/property!--指定目标对象实现的接口--property nameinterfaces valuecom.demo.service.ISomeService/property!--注入前置通知--property nameinterceptorNameslist!--前置通知--valuemyMethodBeforeAdvice/value!--后置通知 --valuemyMethodAfterAdvice/value!--环绕通知 --valuemyMethodAroundAdvice/value!--异常通知 --valuemyMethodThrowAdvice/value/list/property/bean/beans测试 public class App {public static void main(String[] args) {ApplicationContext ac new ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(myProxyFactory);someService.doSomething(!23);} } 2.4 AOP的具体实现 – 基于AspectJ的AOP 2.4.1 基础 1环境准备 引入依赖 applicationContext.xml中引入aop约束 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aop xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd/beans2AspectJ中的通知类型 2.4.2 JoinPoint 、ProceedingJoinPoint JoinPoint 1、作用1在 Spring AOP 中JoinPoint 接口代表了一个程序执行的点比如方法执行或异常处理。2JoinPoint 提供了一种方式来访问当前被通知方法的详细信息如方法签名、参数等。当使用 AOP 通知Advice时你可以将 JoinPoint 作为参数传递到通知方法中以便获取有关当前执行点的详细信息。3JoinPoint 可用于所有类型的通知Before、After、AfterReturning、AfterThrowing但不包括环绕通知。2、JoinPoint 常用方法getArgs()返回一个对象数组包含了被通知方法的参数。 getThis()返回代理对象。getTarget()返回目标对象。 getSignature()返回被通知方法的签名信息。 toString()打印出正在执行的被通知方法的详细信息。 toShortString()提供正在执行的被通知方法的简短描述。toLongString()提供正在执行的被通知方法的完整描述3、其他关联类 MethodSignatureMethodSignature 是 Signature 接口的子接口专门用于方法调用。它提供了访问被拦截方法的详细信息如方法名称、返回类型和参数类型。在通知方法中通常通过将 JoinPoint.getSignature() 的返回值强制转换为 MethodSignature 来获取更多关于方法的信息。 ProceedingJoinPointProceedingJoinPoint 是 JoinPoint 的子接口仅能用于环绕通知Around。它添加了 proceed() 方法允许控制何时继续执行拦截的方法。 1JoinPoint applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdbean classcom.hlp.demo01.service.impl.SomeServiceImpl idsomeService/beanbean classcom.hlp.demo01.aop.LogAspect idlogAspect/beanaop:configaop:pointcut idpt expressionexecution(* com.hlp.demo01.service..*(..))/aop:aspect reflogAspectaop:before methodbefore pointcut-refpt/aop:before/aop:aspect/aop:config/beansISomeService.java public interface ISomeService {String say(); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {public String say() {System.out.println(SomeServiceImpl);return SomeServiceImpl;} } LogAspect.java public class LogAspect {public void before(JoinPoint jp){System.out.println(############);System.out.println(获取参数: Arrays.toString(jp.getArgs()));System.out.println(############);System.out.println(获取代理对象: jp.getThis());System.out.println(获取目标对象: jp.getTarget());System.out.println(############);System.out.println(获取通知方法的签名信息: jp.getSignature());System.out.println(############);System.out.println(获取通知方法的详细信息: jp.toString());System.out.println(获取通知方法的简短描述: jp.toShortString());System.out.println(获取通知方法的完整描述: jp.toLongString());System.out.println(before 前置通知);} } App.java public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.say();} }2ProceedingJoinPoint applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdbean classcom.hlp.demo01.service.impl.SomeServiceImpl idsomeService/beanbean classcom.hlp.demo01.aop.LogAspect idlogAspect/beanaop:configaop:pointcut idpt expressionexecution(* com.hlp.demo01.service..*(..))/aop:aspect reflogAspectaop:around methodaround pointcut-refpt/aop:around/aop:aspect/aop:config/beansISomeService.java public interface ISomeService {String say(); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {public String say() {System.out.println(SomeServiceImpl);return SomeServiceImpl;} } LogAspect.java public class LogAspect {public Object around(ProceedingJoinPoint jp) throws Throwable {System.out.println(############);System.out.println(获取参数: Arrays.toString(jp.getArgs()));System.out.println(############);System.out.println(获取代理对象: jp.getThis());System.out.println(获取目标对象: jp.getTarget());System.out.println(############);System.out.println(获取通知方法的签名信息: jp.getSignature());System.out.println(############);System.out.println(获取通知方法的详细信息: jp.toString());System.out.println(获取通知方法的简短描述: jp.toShortString());System.out.println(获取通知方法的完整描述: jp.toLongString());System.out.println(around 环绕通知前);Object result jp.proceed();System.out.println(around 环绕通知后);return result;}} App.java public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.say();} }2.4.3 切入点表达式 SpringAop只支持标准的AspectJ Aop的pointcut的表达式类型其中的10种外加Spring Aop自己扩充的一种一共是11(101)种类型的表达式分别如下。1.execution一般用于指定方法的执行用的最多。2.within指定某些类型的全部方法执行也可用来指定一个包。3.thisSpring Aop是基于动态代理的生成的bean也是一个代理对象this就是这个代理对象当这个对象可以转换为指定的类型时对应的切入点就是它了Spring Aop将生效。target当被代理的对象可以转换为指定的类型时对应的切入点就是它了Spring Aop将生效。4.reference pointcut(经常使用)表示引用其他命名切入点只有ApectJ风格支持Schema风格不支持5.args当执行的方法的参数是指定类型时生效。6.args当执行的方法参数类型上拥有指定的注解时生效。7.within与target类似看官方文档和网上的说法都是within只需要目标对象的类或者8.父类上有指定的注解则within会生效而target则是必须是目标对象的类上有指定的注解。而根据笔者的测试这两者都是只要目标类或父类上有指定的注解即可。9.annotation当执行的方法上拥有指定的注解时生效。10.target当代理的目标对象上拥有指定的注解时生效。11.bean当调用的方法是指定的bean的方法时生效。(Spring AOP自己扩展支持的)注意Pointcut定义时还可以使用、||、! 这三个运算。进行逻辑运算。可以把各种条件组合起来使用 1execution 切入点表达式要匹配的对象就是目标方法的方法名。所以execution表达式中明显就是方法的签名。注意表达式中加[]的部分表示可省略部分各部分间用空格分开。 切入点表达式 切入点表达式符号 举例1 举例2 execution(void com.guanzhi.dao.BookDao.update()) √ 匹配接口能匹配到execution(void com.guanzhi.dao.impl.BookDaoImpl.update()) √ 匹配实现类能匹配到execution(* com.guanzhi.dao.impl.BookDaoImpl.update()) √ 返回值任意能匹配到execution(* com.guanzhi.dao.impl.BookDaoImpl.update(*)) × 返回值任意但是update方法必须要有一个参数无法匹配要想匹配需要在update接口和实现类添加参数execution(void com.*.*.*.*.update()) √ 返回值为void,com包下的任意包三层包下的任意类的update方法匹配到的是实现类能匹配execution(void com.*.*.*.update()) √ 返回值为void,com包下的任意两层包下的任意类的update方法匹配到的是接口能匹配execution(void *..update()) √ 返回值为void方法名是update的任意包下的任意类能匹配execution(* *..*(..)) √ 匹配项目中任意类的任意方法能匹配但是不建议使用这种方式影响范围广execution(* *..u*(..)) √ 匹配项目中任意包任意类下只要以u开头的方法update方法能满足能匹配execution(* *..*e(..)) √ 匹配项目中任意包任意类下只要以e结尾的方法update和save方法能满足能匹配execution(void com..*()) √ 返回值为voidcom包下的任意包任意类任意方法能匹配*代表的是方法execution(* com.guanzhi.*.*Service.find*(..)) √ 将项目中所有业务层方法的以find开头的方法匹配execution(* com.guanzhi.*.*Service.save*(..)) √ 将项目中所有业务层方法的以save开头的方法匹配 举例3 2within 使用“within(类型表达式)”匹配指定类型内的方法执行 与execution相比可以不写参数 例子 Aspect public class LogAspect {// Before(execution(* com.hlp..*(..))) // public void before(){ // System.out.println(#######); // System.out.println(before 前置通知); // }Before(within(com.hlp..*))public void before(){System.out.println(#######);System.out.println(before 前置通知);}}3this 使用“this(类型全限定名)”匹配当前AOP代理对象类型的执行方法注意是AOP代理对象的类型匹配这样就可能包括引入接口方法也可以匹配注意this中使用的表达式必须是类型全限定名不支持通配符 4target 使用“target(类型全限定名)”匹配当前目标对象类型的执行方法注意是目标对象的类型匹配这样就不包括引入接口也类型匹配注意target中使用的表达式必须是类型全限定名不支持通配符 例子 Aspect public class LogAspect {// Before(execution(* com.hlp..*(..))) // public void before(){ // System.out.println(#######); // System.out.println(before 前置通知); // }Before(target(com.hlp.demo01.service.impl.SomeServiceImpl))public void before(){System.out.println(#######);System.out.println(before 前置通知);}}5within 使用“within(注解类型)”匹配所以持有指定注解类型内的方法注解类型也必须是全限定类型名 例子 MyAnno.java 自定义注解 Retention(RetentionPolicy.RUNTIME) Target({ElementType.TYPE}) public interface MyAnno { }SomeServiceImpl .java MyAnno public class SomeServiceImpl implements ISomeService {public String say() {System.out.println(SomeServiceImpl);return SomeServiceImpl;} } Aspect public class LogAspect {// Before(execution(* com.hlp..*(..))) // public void before(){ // System.out.println(#######); // System.out.println(before 前置通知); // }Before(within(com.hlp.demo01.MyAnno))public void before(){System.out.println(#######);System.out.println(before 前置通知);}}6 target 使用“target(注解类型)”匹配当前目标对象类型的执行方法其中目标对象持有指定的注解注解类型也必须是全限定类型名 7 args 使用“args(注解列表)”匹配当前执行的方法传入的参数持有指定注解的执行注解类型也必须是全限定类型名 8 annotation 使用“annotation(注解类型)”匹配当前执行方法持有指定注解的方法注解类型也必须是全限定类型名 9bean 使用“bean(Bean id或名字通配符)”匹配特定名称的Bean对象的执行方法Spring ASP扩展的在AspectJ中无相应概念 10reference pointcut 表示引用其他命名切入点只有ApectJ风格支持Schema风格不支持如下所示 2.4.3 实现 2.4.3.1 IOC容器基于配置aspectJ基于配置方式 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--注册someService类--bean classcom.hlp.service.impl.SomeServiceImpl idsomeService/bean!--注册切面类--bean classcom.hlp.MyAspect idmyAspect/bean!--方式一:切面配置-- !-- aop:config-- !-- aop:aspect refmyAspect-- !-- lt;!ndash;前置通知ndash;gt;-- !-- aop:before methodbeforeAdvice pointcutexecution(* com.hlp.service..*.*(..))/aop:before-- !-- lt;!ndash;后置通知ndash;gt;-- !-- aop:after-returning methodafterAdvice returningmsg pointcutexecution(* com.hlp.service..*.*(..))/aop:after-returning-- !-- lt;!ndash;环绕通知ndash;gt;-- !-- aop:around methodaroundAdvice pointcutexecution(* com.hlp.service..*.*(..))/aop:around-- !-- lt;!ndash;异常通知ndash;gt;-- !-- aop:after-throwing methodthrowsAdvice throwingex pointcutexecution(* com.hlp.service..*.*(..))/aop:after-throwing-- !-- lt;!ndash;最终通知ndash;gt;-- !-- aop:after methodfinalAdvice pointcutexecution(* com.hlp.service..*.*(..))/aop:after-- !-- /aop:aspect-- !-- /aop:config--!--方式二:切面配置--aop:configaop:pointcut idmyAspext expressionexecution(* com.hlp..*.*(..))/aop:aspect refmyAspect!--前置通知--aop:before methodbeforeAdvice pointcut-refmyAspext/aop:before!--后置通知--aop:after-returning methodafterAdvice returningmsg pointcut-refmyAspext/aop:after-returning!--环绕通知--aop:around methodaroundAdvice pointcut-refmyAspext/aop:around!--异常通知--aop:after-throwing methodthrowsAdvice throwingex pointcut-refmyAspext/aop:after-throwing!--最终通知--aop:after methodfinalAdvice pointcut-refmyAspext/aop:after/aop:aspect/aop:config/beans 1前置通知 ISomeService.java public interface ISomeService {public String doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic String doSomething(String msg) {System.out.println(SomeServiceImpl:msg);return 123;} } MyAspect .java /*** 自定义切面类*/ public class MyAspect {/*** 前置通知* 切入点表达式将通知与切入点关联*/public void before(){System.out.println(前置通知执行了);} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--AspectJ的AOP配置方式一-- !-- aop:config-- !-- aop:pointcut idmyAspect expressionexecution(* com.demoaspect.service..*.*(..))/-- !-- aop:aspect refmyAspect-- !-- aop:after-returning methodafter returningmsg pointcut-refmyAspext /aop:after-returning-- !-- /aop:aspect-- !-- /aop:config--!--AspectJ的AOP配置方式二--aop:config!--配置切面类--aop:aspect refmyAspect!-- 配置后置通知将通知与切入点表达式关联--aop:after-returning methodafter returningmsg pointcutexecution(* com.demoaspect.service..*.*(..))/aop:after-returning/aop:aspect/aop:config /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} }2后置通知 ISomeService.java public interface ISomeService {public void doSomething(String msg); }SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyAspect .java /*** 自定义切面类*/ public class MyAspect {/*** 后置通知* 切入点表达式将通知与切入点关联*/public void after(Object msg){System.out.println(后通知执行了msg);} }applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--AspectJ的AOP配置--aop:config!--配置切面类--aop:aspect refmyAspect!-- 配置后置通知将通知与切入点表达式关联--aop:after-returning methodafter returningmsg pointcutexecution(* com.demoaspect.service..*.*(..))/aop:after-returning/aop:aspect/aop:config /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} } 3环绕通知 ISomeService.java public interface ISomeService {public void doSomething(String msg); }SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyAspect .java /*** 自定义切面类*/ public class MyAspect {/*** 环绕通知* 切入点表达式将通知与切入点关联*/public Object round(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println(目标方法执行前打印);//执行目标对象的方法Object result proceedingJoinPoint.proceed();//目标方法执行后打印System.out.println(目标方法执行前打印);return result;} }applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--AspectJ的AOP配置--aop:config!--配置切面类--aop:aspect refmyAspect!-- 配置环绕通知将通知与切入点表达式关联--aop:around methodround pointcutexecution(* com.demoaspect.service..*.*(..))/aop:around/aop:aspect/aop:config /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} } 4异常通知 ISomeService.java public interface ISomeService {public void doSomething(String msg); }SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyAspect .java /*** 自定义切面类*/ public class MyAspect {/*** 异常通知* 切入点表达式将通知与切入点关联*/public void throws123(Exception ex) throws Throwable {System.out.println(出现异常了ex);} }applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--AspectJ的AOP配置--aop:config!-- aop:pointcut id expression/--!--配置切面类--aop:aspect refmyAspect!-- 配置异常通知将通知与切入点表达式关联--aop:after-throwing methodthrows123 throwingex pointcutexecution(* com.demoaspect.service..*.*(..))/aop:after-throwing/aop:aspect/aop:config /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} } 5最终通知 ISomeService.java public interface ISomeService {public void doSomething(String msg); }SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyAspect .java /*** 自定义切面类*/ public class MyAspect {/*** 最终通知* 切入点表达式将通知与切入点关联*/public void final1(){System.out.println(最终通知执行了);} }applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--AspectJ的AOP配置--aop:config!-- aop:pointcut id expression/--!--配置切面类--aop:aspect refmyAspect!-- 配置最终通知将通知与切入点表达式关联--aop:after methodfinal1 pointcutexecution(* com.demoaspect.service..*.*(..))/aop:after/aop:aspect/aop:config /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} } 5 完整案例 ISomeService.java public interface ISomeService {public void doSomething(String msg); }SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyAspect .java package com.demoaspect;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** 自定义切面类*/ public class MyAspect {/*** 前置通知* 切入点表达式将通知与切入点关联*/public void before(){System.out.println(前置通知执行了);}/*** 后置通知* 切入点表达式将通知与切入点关联*/public void after(Object msg){System.out.println(后通知执行了msg);}/*** 环绕通知* 切入点表达式将通知与切入点关联*/public Object round(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println(目标方法执行前打印);//执行目标对象的方法Object result proceedingJoinPoint.proceed();//目标方法执行后打印System.out.println(目标方法执行前打印);return result;}/*** 异常通知* 切入点表达式将通知与切入点关联*/public void throws123(Exception ex) throws Throwable {System.out.println(出现异常了ex);}/*** 最终通知* 切入点表达式将通知与切入点关联*/public void final1(){System.out.println(最终通知执行了);} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--AspectJ的AOP配置方式一-- !-- aop:config-- !-- aop:pointcut idmyAspect expressionexecution(* com.demoaspect.service..*.*(..))/-- !-- aop:aspect refmyAspect-- !-- aop:before methodbefore pointcut-refmyAspect/aop:before-- !-- aop:after-returning methodafter returningmsg pointcut-refmyAspect/aop:after-returning-- !-- aop:around methodround pointcut-refmyAspect/aop:around-- !-- aop:after-throwing methodthrows123 throwingex pointcut-refmyAspect/aop:after-throwing-- !-- aop:after methodfinal1 pointcut-refmyAspect/aop:after-- !-- /aop:aspect-- !-- /aop:config--!--AspectJ的AOP配置方式二--aop:config!-- aop:pointcut id expression/--!--配置切面类--aop:aspect refmyAspect!-- 配置前置通知将通知与切入点表达式关联--aop:before methodbefore pointcutexecution(* com.demoaspect.service..*.*(..))/aop:before!-- 配置后置通知将通知与切入点表达式关联--aop:after-returning methodafter returningmsg pointcutexecution(* com.demoaspect.service..*.*(..))/aop:after-returning!-- 配置环绕通知将通知与切入点表达式关联--aop:around methodround pointcutexecution(* com.demoaspect.service..*.*(..))/aop:around!-- 配置异常通知将通知与切入点表达式关联--aop:after-throwing methodthrows123 throwingex pointcutexecution(* com.demoaspect.service..*.*(..))/aop:after-throwing!-- 配置最终通知将通知与切入点表达式关联--aop:after methodfinal1 pointcutexecution(* com.demoaspect.service..*.*(..))/aop:after/aop:aspect/aop:config /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} } 2.3.2.2 IOC容器基于配置aspectJ基于注解方式实现 【基于aspectJ注解方式但是spring还是基于配置文件实现】 方式一 每个切入点单独设置表达式 Aspect public class LogAspect {//前置通知Before(value execution(* com.hlp.demo01.service..*(..)))public void before(){System.out.println(前置通知执行了);}//后置通知AfterReturning(value execution(* com.hlp.demo01.service..*(..)),returning msg)public void after(Object msg){System.out.println(后通知执行了msg);}//环绕通知Around(value execution(* com.hlp.demo01.service..*(..)))public Object after(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println(目标方法执行前打印);//执行目标对象的方法Object result proceedingJoinPoint.proceed();//目标方法执行后打印System.out.println(目标方法执行前打印);return result;}//异常通知//目标方法抛异常后才会执行。AfterThrowing(value execution(* com.hlp.demo01.service..*(..)) ,throwingex)public void throws123(Exception ex) throws Throwable {System.out.println(出现异常了ex);}//最终通知//不管目标方法是否抛出异常都会执行。After(value execution(* com.hlp.demo01.service..*(..)))public void after11() {System.out.println(最终通知执行了);} }方式二 切入点统一设置表达式 Aspect public class LogAspect {Pointcut(value execution(* com.hlp.demo01.service..*(..)))public void pt1(){}//前置通知Before(pt1())public void before(){System.out.println(前置通知执行了);}//后置通知AfterReturning(value pt1(),returning msg)public void after(Object msg){System.out.println(后通知执行了msg);}//环绕通知Around(value pt1())public Object after(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println(目标方法执行前打印);//执行目标对象的方法Object result proceedingJoinPoint.proceed();//目标方法执行后打印System.out.println(目标方法执行前打印);return result;}//异常通知//目标方法抛异常后才会执行。AfterThrowing(value pt1() ,throwingex)public void throws123(Exception ex) throws Throwable {System.out.println(出现异常了ex);}//最终通知//不管目标方法是否抛出异常都会执行。After(value pt1())public void after11() {System.out.println(最终通知执行了);} } 1前置通知 ISomeService.java public interface ISomeService {public String doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic String doSomething(String msg) {System.out.println(SomeServiceImpl:msg);return 123;} } MyAspect .java /*** 自定义切面类*/ Aspect public class MyAspect {/*** 前置通知* 切入点表达式将通知与切入点关联*/Before(value execution(* com.demoaspect.service.*.*(..)))public void before(){System.out.println(前置通知执行了);} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--注册自动代理--aop:aspectj-autoproxy/aop:aspectj-autoproxy /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} }2后置通知 ISomeService.java public interface ISomeService {public void doSomething(String msg); }SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic void doSomething(String msg) {System.out.println(SomeServiceImpl:msg);} } MyAspect .java /*** 自定义切面类*/ Aspect public class MyAspect {/*** 后置通知* 切入点表达式将通知与切入点关联*/AfterReturning(value execution(* com.demoaspect.service.*.*(..)),returning msg)public void after(Object msg){System.out.println(后通知执行了msg);} }applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--注册自动代理--aop:aspectj-autoproxy/aop:aspectj-autoproxy /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} } 3环绕通知 ISomeService.java public interface ISomeService {public String doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic String doSomething(String msg) {System.out.println(SomeServiceImpl:msg);return 123;} } MyAspect .java /*** 自定义切面类*/ Aspect public class MyAspect {/*** 环绕通知* 切入点表达式将通知与切入点关联*/Around(value execution(* com.demoaspect.service.*.*(..)))public Object after(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println(目标方法执行前打印);//执行目标对象的方法Object result proceedingJoinPoint.proceed();//目标方法执行后打印System.out.println(目标方法执行前打印);return result;} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--注册自动代理--aop:aspectj-autoproxy/aop:aspectj-autoproxy /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} }4异常通知 ISomeService.java public interface ISomeService {public String doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic String doSomething(String msg) {if(11){return (1/0);}System.out.println(SomeServiceImpl:msg);return 123;} } MyAspect .java /*** 自定义切面类*/ Aspect public class MyAspect {/*** 异常通知* 切入点表达式将通知与切入点关联*/AfterThrowing(value execution(* com.demoaspect.service.*.*(..)) ,throwingex)public void throws123(Exception ex) throws Throwable {System.out.println(出现异常了ex);} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--注册自动代理--aop:aspectj-autoproxy/aop:aspectj-autoproxy /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} }5最终通知 ISomeService.java public interface ISomeService {public String doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic String doSomething(String msg) {if(11){return (1/0);}System.out.println(SomeServiceImpl:msg);return 123;} } MyAspect .java /*** 自定义切面类*/ Aspect public class MyAspect {/*** 最终通知* 切入点表达式将通知与切入点关联*/After(value execution(* com.demoaspect.service.*.*(..)))public void after(){System.out.println(最终通知执行了);} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--注册自动代理--aop:aspectj-autoproxy/aop:aspectj-autoproxy /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} }5 完整案例 ISomeService.java public interface ISomeService {public String doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic String doSomething(String msg) {System.out.println(SomeServiceImpl:msg);return 123;} } MyAspect .java /*** 自定义切面类*/ Aspect public class MyAspect {/*** 前置通知* 切入点表达式将通知与切入点关联*/Before(value execution(* com.demoaspect.service.*.*(..)))public void before(){System.out.println(前置通知执行了);}/*** 后置通知* 切入点表达式将通知与切入点关联*/AfterReturning(value execution(* com.demoaspect.service.*.*(..)),returning msg)public void after(Object msg){System.out.println(后通知执行了msg);}/*** 环绕通知* 切入点表达式将通知与切入点关联*/Around(value execution(* com.demoaspect.service.*.*(..)))public Object after(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println(目标方法执行前打印);//执行目标对象的方法Object result proceedingJoinPoint.proceed();//目标方法执行后打印System.out.println(目标方法执行前打印);return result;}/*** 异常通知* 切入点表达式将通知与切入点关联*/AfterThrowing(value execution(* com.demoaspect.service.*.*(..)) ,throwingex)public void throws123(Exception ex) throws Throwable {System.out.println(出现异常了ex);}/*** 最终通知* 切入点表达式将通知与切入点关联*/After(value execution(* com.demoaspect.service.*.*(..)))public void after(){System.out.println(最终通知执行了);} } applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 注册目标对象--bean classcom.demoaspect.service.SomeServiceImpl idsomeService/bean!-- 注册切面类对象--bean classcom.demoaspect.MyAspect idmyAspect/bean!--注册自动代理--aop:aspectj-autoproxy/aop:aspectj-autoproxy /beans测试 public class App {public static void main(String[] args) {ApplicationContext acnew ClassPathXmlApplicationContext(applicationContext.xml);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} }2.3.2.3 IOC容器基于注解aspectJ基于注解方式实现 1完整示例 ISomeService.java public interface ISomeService {public String doSomething(String msg); } SomeServiceImpl .java public class SomeServiceImpl implements ISomeService {Overridepublic String doSomething(String msg) {System.out.println(SomeServiceImpl:msg);return 123;} } MyAspect .java package com.demoaspect;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** 自定义切面类*/ Aspect public class MyAspect {/*** 前置通知* 切入点表达式将通知与切入点关联*/Before(value execution(* com.demoaspect.service..*.*(..)))public void before(){System.out.println(前置通知执行了);}/*** 后置通知* 切入点表达式将通知与切入点关联*/AfterReturning(value execution(* com.demoaspect.service..*.*(..)) ,returning msg)public void after(Object msg){System.out.println(后通知执行了msg);}/*** 环绕通知* 切入点表达式将通知与切入点关联*/Around(value execution(* com.demoaspect.service..*.*(..)) )public Object round(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println(目标方法执行前打印);//执行目标对象的方法Object result proceedingJoinPoint.proceed();//目标方法执行后打印System.out.println(目标方法执行前打印);return result;}/*** 异常通知* 切入点表达式将通知与切入点关联*/AfterThrowing(value execution(* com.demoaspect.service..*.*(..)) ,throwing ex)public void throws123(Exception ex) throws Throwable {System.out.println(出现异常了ex);}/*** 最终通知* 切入点表达式将通知与切入点关联*/After(value execution(* com.demoaspect.service..*.*(..)) )public void final1(){System.out.println(最终通知执行了);} } MyConfig.java Configuration EnableAspectJAutoProxy public class MyConfig {Beanpublic ISomeService someService(){return new SomeServiceImpl();}Beanpublic MyAspect myAspect(){return new MyAspect();} }测试 public class App {public static void main(String[] args) {ApplicationContext acnew AnnotationConfigApplicationContext(MyConfig.class);ISomeService someService (ISomeService) ac.getBean(someService);someService.doSomething(123);} }2分析 aspectJ基于注解方式实现【IOC容器基于配置】中的applicationContext.xml配置文件》等价于“aspectJ基于注解方式实现【IOC容器基于注解】”中的MyConfig.java 三、阶段总结 3.1 applicationContext.xml 1applicationContext.xml 基础 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd/beans2引入c标签简化构造注入 c标签用于简化构造注入使用c标签必须引入xmlns:c“http://www.springframework.org/schema/c” ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:chttp://www.springframework.org/schema/cxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!-- bean classcom.hlp.spring.ioc.beans.Car idmyCar -- !-- constructor-arg nameid value1111/constructor-arg-- !-- constructor-arg namename value宝马xxx/constructor-arg-- !-- constructor-arg nameage value1/constructor-arg-- !-- /bean--!-- bean classcom.hlp.spring.ioc.beans.Car idmyCar -- !-- constructor-arg index0 value1111/constructor-arg-- !-- constructor-arg index1 value宝马xxx/constructor-arg-- !-- constructor-arg value1/constructor-arg-- !-- /bean--bean classcom.hlp.spring.ioc.beans.Car c:id11 c:name宝马xxx c:age12 idmyCar /bean/beans3引入p标签简化属性注入 p标签用于简化属性注入。若使用p标签则必须引入xmlns:p“http://www.springframework.org/schema/p” ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!-- 设值注入方式一 -- !-- bean classcom.hlp.spring.ioc.beans.Car idcar-- !-- property nameid value12xxxx/property-- !-- property nameage value12/property-- !-- property namename value12/property-- !-- /bean--!-- 设值注入方式二 --bean classcom.hlp.spring.ioc.beans.Car idcar2 p:idxxx p:age12 p:name宝马xxx/bean/beans4使用context:component-scan开启注解扫描 使用context:component-scan开启注解扫描必须引入xml约束 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd!-- 添加扫描路径--!-- 作用ioc容器初始化时会扫描该标签指定的路径路径下是否有通过注解标识的java类--context:component-scan base-packagecom.hlp.spring.ioc.beans/context:component-scan /beans5使用aop必须引入aop约束 使用aop必须引入aop约束 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--注册目标类--bean idsomeService classcom.demo.service.SomeServiceImpl/bean!--注册前置通知--bean idmyMethodBeforeAdvice classcom.demo.MyMethodBeforeAdvice/bean!--创建代理类--bean classorg.springframework.aop.framework.ProxyFactoryBean idmyProxyFactory!--指定目标对象--property nametarget refsomeService/property!--指定目标对象实现的接口--property nameinterfaces valuecom.demo.service.ISomeService/property!--注入前置通知--property nameinterceptorNameslist!--前置通知--valuemyMethodBeforeAdvice/value/list/property/bean/beans6完整xml配置 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:chttp://www.springframework.org/schema/cxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--属性注入--bean classcom.hlp.demo01.Person idpersonproperty namename value张三/propertyproperty nameage value123/property/beanbean classcom.hlp.demo01.Person p:name张安 p:age12 idperson2/bean!--构造注入--bean classcom.hlp.demo01.Person idperson3constructor-arg namename value张三/constructor-argconstructor-arg nameage value123/constructor-arg/beanbean classcom.hlp.demo01.Person c:name123 c:age12 idperson4/bean/beans3.2 核心模块导入jar 1导入jar core核心 aop dao 2引入pom pom文件 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIddemo033/artifactIdversion1.0-SNAPSHOT/versionpackagingjar/packagingnamedemo033/namedependencies!-- ########################### core 核心模块 ########################### --dependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion3.2.7.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion3.2.7.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion3.2.7.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context-support/artifactIdversion3.2.7.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-expression/artifactIdversion3.2.7.RELEASE/version/dependency!-- ########################### aop模块 ########################### --dependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion3.2.7.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion3.2.7.RELEASE/version/dependencydependencygroupIdaopalliance/groupIdartifactIdaopalliance/artifactIdversion1.0/version/dependency!-- ########################### dao模块 ########################### --dependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.3.23/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-tx/artifactIdversion5.3.23/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.31/version/dependency/dependencies /project
http://www.tj-hxxt.cn/news/139037.html

相关文章:

  • 怎么做套版网站互联网创业项目的效果
  • 19楼网站模板自己做企业网站用哪个软件
  • 我的网站怎么做快速刷排名的软件最好
  • 网站建设维保合同微信公众号运营推广方案
  • 海南澄迈住房和城乡建设厅网站个人是否可以申请网址
  • 怎么优化网站关键字app对接wordpress
  • 建站群赚钱有前途吗网站建设tlmh
  • 能利用双股铜芯电话线做网站吗福州鼓楼区网站建设
  • iis提示网站建设中app介绍网站模板
  • 网站内怎么做搜索建设京东物流网站的目标是什么
  • 建网站 开发app洛阳网站建设首选洛阳铭信科技
  • 网站 医院信息化建设潍坊专业技术人员继续教育
  • 公司建设网站有什么好处计算机网站开发书籍
  • 如何建设专题网站冶金工业建设工程定额总站网站
  • 北京网站改版哪家好广州企业管理咨询公司排名
  • 网站备案拍照是什么郑州网站制作服务
  • 郑州网站制作报价企业网站源码搜一品资源
  • 长春做企业网站多少钱微博优惠券网站怎么做
  • iis7架设网站桂林两江四湖夜游
  • 做yield网站多少钱wordpress+用户中心插件
  • 嘉兴做网站的公司企业上云解决方案
  • 做网站合肥西宁网站seo价格
  • 公司在百度怎么推广电商seo推广
  • 什么是网站标题单业网站建设
  • 网站建设策划优点企业文化手册
  • 图片展示型网站模板wordpress 秀米
  • 苏州相城区做网站论坛类网站开发
  • 购物建设网站网站空间费1年1200
  • 那里建设网站大兴做网站的公司
  • asp网站代码 部分封装移动网站模板下载