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

建设网站需要虚拟空间口碑好的做pc端网站

建设网站需要虚拟空间,口碑好的做pc端网站,乐陵人力资源中心,十大网络营销经典案例目录 创建项目 Spring 核心基础 Spring 容器 Spring 容器的作用 Spring 容器的工作流程 Bean Bean 的生命周期 IOC#xff08;控制反转#xff09;与依赖注入#xff08;DI#xff09; 控制反转的概念 依赖注入的几种方式#xff08;构造器注入、Setter 注入、接…目录 创建项目 Spring 核心基础 Spring 容器 Spring 容器的作用 Spring 容器的工作流程 Bean Bean 的生命周期 IOC控制反转与依赖注入DI 控制反转的概念 依赖注入的几种方式构造器注入、Setter 注入、接口注入 构造器注入依赖对象通过类的构造器传入。 基于 XML 的构造器注入 基于注解的构造器注入 Setter 注入通过 Setter 方法将依赖注入到对象中。 Setter 注入的基本原理 Setter 注入的优缺点 Spring 容器工作原理 案例实现 Spring 配置文件基于 XML 使用 Java 配置类 接口注入这种方式不常见是通过接口方法将依赖注入一般不推荐。 Spring Bean 的生命周期 Spring Bean 的配置 基于 XML 的配置 XML 配置文件的基础结构 定义 Bean 基本的 Bean 配置 Bean 的作用域 注入依赖 构造器注入 属性注入Setter 注入 ​​​​​​​集合注入 ​​​​​​​​​​​​​​注入 Map 注入 Properties 自动装配Autowiring ​​​​​​​​​​​​​​加载 Bean 配置文件 基于 Java 配置类的配置 Spring AOP面向切面编程 AOP 概念与用途 AOP 切入点和通知类型前置通知、后置通知、环绕通知等 使用 Aspect 注解配置 AOP 创建项目 创建一个基本的maven项目 dependencies!-- Spring Context --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.10/version/dependency!-- Spring Beans --dependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion5.3.10/version/dependency/dependencies package com.lirui.example;public class UserRepository {public void save() {System.out.println(1111111111);} }package com.lirui.example;public class UserService {private final UserRepository userRepository;// 构造器注入public UserService(UserRepository userRepository) {this.userRepository userRepository;}public void saveUser() {userRepository.save();} }package com.lirui;import com.lirui.example.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {// 加载 Spring 配置文件ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);// 获取 userService BeanUserService userService context.getBean(userService, UserService.class);// 调用方法userService.saveUser();} } ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!-- applicationContext.xml 是 Spring 框架中的配置文件通常用于定义 Spring IoC控制反转容器中的 Bean 配置和管理。这个文件主要用于将 Spring 的配置项和组件描述清楚告知 Spring 容器如何实例化、配置和管理应用程序中的各种对象Bean。它是 Spring 配置的传统方式之一除了注解和 Java 配置类。--!-- 定义 UserRepository Bean --bean iduserRepository classcom.lirui.example.UserRepository/!-- 定义 UserService Bean注入 userRepository --bean iduserService classcom.lirui.example.UserServiceconstructor-arg refuserRepository//bean/beansSpring 核心基础 Spring 容器 Spring 容器是 Spring 框架的核心组件之一。 Spring 容器的作用 创建和管理 BeanSpring 容器根据配置文件如 XML 配置、注解或 Java 配置类创建 Java 对象并通过依赖注入DI管理这些对象的生命周期和依赖关系。 控制反转IoCSpring 容器负责对象的创建、配置和管理应用程序中的对象不再直接创建和管理依赖项容器完成这些任务。这种方式称为控制反转Inversion of Control, IoC。 提供依赖注入DI容器自动将所需的依赖注入到对象中使得对象之间的耦合度降低从而提高代码的灵活性和可维护性。 Spring 容器的工作流程 加载配置文件Spring 容器首先加载配置文件或注解配置解析其中的 Bean 定义。 创建 Bean 实例根据配置Spring 容器创建 Bean 实例并进行初始化。 依赖注入容器自动将其他 Bean依赖注入到当前 Bean 中通常通过构造器注入、Setter 注入或字段注入。 Bean 的使用应用程序获取 Bean 并调用其方法。 销毁 Bean当容器关闭时容器销毁 Bean 实例执行清理操作。 Bean Bean 是指由 Spring 容器管理的对象 Bean 的生命周期 实例化Spring 容器根据配置或注解定义的 Bean 类创建对象实例。 依赖注入容器将所需的依赖项注入到 Bean 的属性中。依赖可以是通过构造器注入、Setter 注入或字段注入。 初始化在 Bean 完成依赖注入后容器会调用 Bean 的初始化方法如果定义了。 使用Bean 可以被应用程序使用通过 Spring 容器获取。 销毁当容器关闭时容器会销毁 Bean执行销毁回调方法如果定义了。 IOC控制反转与依赖注入DI 控制反转的概念 控制反转Inversion of Control简称 IOC是 Spring 的核心概念指的是将对象的创建和依赖关系的管理交由容器如 Spring 容器来处理而不是由对象自己来完成。这种反转是通过依赖注入DI实现的能减少代码耦合提高可测试性和灵活性。 在传统的 Java 编程中对象依赖于其他对象通常是通过 new 关键字来创建这会导致类之间紧密耦合。而通过 IOC容器负责管理这些依赖关系类之间的耦合度更低。 依赖注入的几种方式构造器注入、Setter 注入、接口注入 构造器注入依赖对象通过类的构造器传入。 基于 XML 的构造器注入 在 Spring 中可以通过 bean 的 constructor-arg 元素来实现构造器注入。这里的 constructor-arg 元素用于传递构造函数的参数Spring 会根据配置的顺序自动匹配构造器的参数。 constructor-arg 标签可以通过 value 和 ref 属性来传递构造函数的参数。它们的作用如下 value: 用于传递简单的常量值如字符串、数字、布尔值等。这通常用于基本类型或字符串类型的构造参数。 ref: 用于传递引用类型的参数通常用于传递其他 bean 的引用。如果构造函数参数是另一个 bean 的引用使用 ref 属性。 package com.lirui.car;public class Car {private Engine engine;public Car(Engine engine) {this.engine engine;}public void drive() {System.out.println(车使用 engine.getType()engine.getHorsepower());} }package com.lirui.car;public class Engine {private String type;private int horsepower;public Engine(String type,int horsepower) {this.type type;this.horsepower horsepower;}public String getType() {return type;}public int getHorsepower() {return horsepower;} }package com.lirui.car;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);Car car context.getBean(ca, Car.class);car.drive();} }?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!-- applicationContext.xml 是 Spring 框架中的配置文件通常用于定义 Spring IoC控制反转容器中的 Bean 配置和管理。这个文件主要用于将 Spring 的配置项和组件描述清楚告知 Spring 容器如何实例化、配置和管理应用程序中的各种对象Bean。它是 Spring 配置的传统方式之一除了注解和 Java 配置类。--!-- 定义 UserRepository Bean --bean iduserRepository classcom.lirui.example.UserRepository/!-- 定义 UserService Bean注入 userRepository --bean iduserService classcom.lirui.example.UserServiceconstructor-arg refuserRepository//beanbean idV8en classcom.lirui.car.Engineconstructor-arg valueV8/constructor-arg value500//beanbean idV6en classcom.lirui.car.Engineconstructor-arg valueV6/constructor-arg value200//beanbean idca classcom.lirui.car.Carconstructor-arg refV6en//bean/beans基于注解的构造器注入 在 Spring 3.0 及以后的版本中可以通过 Autowired 注解来实现构造器注入Spring 会自动根据构造函数参数类型来选择合适的 bean。 package com.lirui.car;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource;Configuration ComponentScan(basePackages com.lirui.car) PropertySource(classpath:application.properties) public class AppConfig { }package com.lirui.car;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;Component public class Car {private Engine engine;Autowiredpublic Car(Engine engine) {this.engine engine;}public void drive() {System.out.println(车使用 engine.getType()engine.getHorsepower());} }package com.lirui.car;import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;Component public class Engine {private String type;private int horsepower;public Engine(Value(${car.engine.type})String type,Value(${car.engine.horsepower}) int horsepower) {this.type type;this.horsepower horsepower;}public String getType() {return type;}public int getHorsepower() {return horsepower;} }package com.lirui.car;import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext context new AnnotationConfigApplicationContext(AppConfig.class);Car car context.getBean(Car.class);car.drive();} }#application.properties car.engine.typeV8 car.engine.horsepower200 AppConfig.java配置类 Configuration 作用标识该类是一个 Spring 配置类相当于传统的 XML 配置文件。解释Spring 会通过这个类来定义和配置 Spring 容器中的 Bean。它指定了类中的方法可以用于生成 Bean 配置。ComponentScan(basePackages com.lirui.car) 作用告诉 Spring 扫描 com.lirui.car 包及其子包中的所有类并自动将这些类注册为 Spring Bean。解释任何标注了 Component、Service、Repository、Controller 等注解的类都会被 Spring 容器管理。通过此注解Spring 会自动扫描并注册这些类。PropertySource(classpath:application.properties) 作用指定一个外部的属性文件在这个例子中是 application.properties用于加载应用的配置。解释通过 PropertySource 注解Spring 会读取 application.properties 文件中的内容并将其中的配置值注入到 Spring 容器中的 Bean 类。Car.java汽车类 Component 作用标识 Car 类是一个 Spring 管理的组件Bean。解释Spring 容器会自动扫描并将标注了 Component 注解的类注册为 Bean。可以通过 Autowired 注解来注入其他 Bean。 Autowired 作用自动注入依赖。Spring 会根据类型自动查找并注入匹配的 Bean。解释在构造函数上标注 AutowiredSpring 会自动将 Engine 类型的 Bean 注入到 Car 类的 engine 属性中。 drive() 作用此方法用于打印 Car 类使用的引擎类型和马力。解释通过 engine.getType() 和 engine.getHorsepower() 获取并输出引擎类型和马力。Engine.java引擎类 Component 作用标识 Engine 类是一个 Spring 管理的 Bean。解释Spring 容器会自动扫描并注册该类为一个 Bean以便可以注入到其他组件中。 Value(${car.engine.type}) 和 Value(${car.engine.horsepower}) 作用通过 Value 注解将外部配置文件中的属性值注入到类的字段中。解释Value 注解用来注入配置文件中的值。在这个例子中car.engine.type 和 car.engine.horsepower 分别来自 application.properties 配置文件中的值。 构造函数 作用Engine 类的构造函数接收两个参数type 和 horsepower并将它们初始化到对应的实例变量中。Main.java主程序 ApplicationContext 作用ApplicationContext 是 Spring 的核心接口提供了 Bean 管理和依赖注入等功能。解释使用 AnnotationConfigApplicationContext 创建 Spring 容器加载 AppConfig 配置类并初始化相关 Bean。 context.getBean(Car.class) 作用从 Spring 容器中获取 Car 类的实例。解释通过 getBean() 方法从容器中获取已经定义的 Car Bean。Spring 会根据类类型来返回匹配的 Bean 实例。 car.drive() 作用调用 Car 类中的 drive() 方法输出关于汽车和引擎的信息。解释通过 Car 对象调用 drive() 方法最终打印出汽车的引擎类型和马力。application.properties配置文件 car.engine.typeV8 和 car.engine.horsepower200作用定义 car.engine.type 和 car.engine.horsepower 的配置项。解释这些配置项会被加载到 Spring 环境中并通过 Value 注解注入到 Engine 类的 type 和 horsepower 字段中。注解总结 Configuration用来标识配置类替代传统的 XML 配置。ComponentScan用来指定 Spring 容器扫描的包自动注册符合条件的组件为 Bean。PropertySource加载外部配置文件注入其中的属性值。Component标识一个类为 Spring 管理的 Bean。Autowired自动注入依赖的 Bean。Value将外部配置文件中的属性值注入到 Bean 的字段中。 Setter 注入通过 Setter 方法将依赖注入到对象中。 Setter 注入的基本原理 Setter 注入依赖项的方式是通过 Spring 容器为 Bean 自动调用带有 Autowired 注解的 Setter 方法来注入依赖对象。这种方式相比构造器注入更灵活因为它允许在对象创建后进行依赖注入而且依赖项也可以在 Bean 生命周期的不同阶段进行注入。 Setter 注入的优缺点 优点 灵活性高你可以选择是否为对象提供所有依赖因为某些依赖项是通过 Setter 方法来注入的可以选择不注入某些依赖允许在不影响整个对象创建的情况下部分注入。 更容易管理可选依赖如果依赖项是可选的Setter 注入允许不必在构造函数中强制要求所有依赖项都传入。 避免构造函数过多如果类的构造函数依赖项非常多使用 Setter 注入可以避免构造函数的过度拥挤使得类的接口保持简洁。 缺点 不够安全与构造器注入相比Setter 注入依赖项是可选的这可能导致对象在使用时缺少必需的依赖造成运行时错误。 依赖关系不明确通过构造器注入类的依赖项是不可变的一目了然。而通过 Setter 注入依赖关系在对象创建后才被设置依赖的明确性较低。 不支持必须的依赖项Setter 注入无法保证在对象创建后依赖项已经完全注入这可能导致空指针异常或不一致的状态。 Spring 容器工作原理 Spring 会在 Car 类的 Setter 方法上查找 Autowired 注解并将对应的依赖项注入进来例如 Engine、GPS、Radio。依赖项可以通过 Spring 容器中的配置自动提供或者通过 XML 配置类手动声明并注入。 案例实现 // Engine.java public class Engine {private String type;public Engine(String type) {this.type type;}public String getType() {return type;} }// GPS.java public interface GPS {void navigate(); }// GPSImpl.java public class GPSImpl implements GPS {Overridepublic void navigate() {System.out.println(正在用gps导航);} }// Radio.java public interface Radio {void playMusic(); }// RadioImpl.java public class RadioImpl implements Radio {Overridepublic void playMusic() {System.out.println(正在听音乐广播);} }package com.lirui.car;import org.springframework.beans.factory.annotation.Autowired;public class Car {private Engine engine;private GPS gps;private Radio radio;// Setter 注入方式Autowiredpublic void setEngine(Engine engine) {this.engine engine;}Autowiredpublic void setGps(GPS gps) {this.gps gps;}Autowiredpublic void setRadio(Radio radio) {this.radio radio;}public void drive() {System.out.println(引擎engine.getType() );gps.navigate();radio.playMusic();} }Spring 配置文件基于 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!-- applicationContext.xml 是 Spring 框架中的配置文件通常用于定义 Spring IoC控制反转容器中的 Bean 配置和管理。这个文件主要用于将 Spring 的配置项和组件描述清楚告知 Spring 容器如何实例化、配置和管理应用程序中的各种对象Bean。它是 Spring 配置的传统方式之一除了注解和 Java 配置类。--!-- 定义 UserRepository Bean --bean iduserRepository classcom.lirui.example.UserRepository/!-- 定义 UserService Bean注入 userRepository --bean iduserService classcom.lirui.example.UserServiceconstructor-arg refuserRepository//beanbean idengine classcom.lirui.car.Engineconstructor-arg valueV8//beanbean idgps classcom.lirui.car.GPSImpl/bean idradio classcom.lirui.car.RadioImpl/bean idcar classcom.lirui.car.Carproperty nameengine refengine/property namegps refgps/property nameradio refradio//bean!-- bean idV8en classcom.lirui.car.Engine-- !-- constructor-arg valueV8/-- !-- constructor-arg value500/-- !-- /bean--!-- bean idV6en classcom.lirui.car.Engine-- !-- constructor-arg valueV6/-- !-- constructor-arg value200/-- !-- /bean--!-- bean idca classcom.lirui.car.Car-- !-- constructor-arg refV6en/-- !-- /bean--/beans主程序 package com.lirui.car;import com.lirui.example.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {// 加载 Spring 配置文件ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);// 获取 userService BeanCar car context.getBean(car, Car.class);// 调用方法car.drive();} }使用 Java 配置类 package com.lirui.car;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource;Configuration ComponentScan(basePackages com.lirui.car) public class AppConfig {Beanpublic Engine engine() {return new Engine(V8);}Beanpublic GPS gps() {return new GPSImpl();}Beanpublic Radio radio() {return new RadioImpl();}Beanpublic Car car() {Car car new Car();car.setEngine(engine());car.setGps(gps());car.setRadio(radio());return car;} } 主程序 package com.lirui.car;import com.lirui.example.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {// 使用 Java 配置类来初始化 Spring 容器AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext(AppConfig.class);// 获取 Car 实例依赖项会自动注入Car car context.getBean(Car.class);car.drive();context.close();} }接口注入这种方式不常见是通过接口方法将依赖注入一般不推荐。 Spring Bean 的生命周期 ​​​​实例化容器通过反射实例化 Bean。 属性注入依赖关系被注入到 Bean 中。 初始化调用 PostConstruct 方法或者调用自定义的初始化方法可以通过 init-method 属性指定。 使用Bean 可以被应用程序使用。 销毁容器关闭时Bean 会被销毁调用 PreDestroy 方法或调用自定义的销毁方法可以通过 destroy-method 属性指定。 Spring Bean 的配置 基于 XML 的配置 XML 配置是最早期的 Spring 配置方式对项目结构有完整掌控的需求。 XML 配置文件的基础结构 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!-- Bean 配置 -- /beans​ xmlns 和 xsi:schemaLocation 是定义 XML 命名空间和 XML Schema 的标准写法用于使 Spring 能够理解配置文件。 beans 元素是 Spring 配置文件的根元素所有 Bean 的配置都包含在 beans 元素中。 定义 Bean 每个 Spring Bean 都需要在 XML 配置文件中声明。使用 bean 元素来定义一个 Beanid 属性用于指定 Bean 的名称class 属性指定 Bean 的类。 基本的 Bean 配置 bean idengine classcom.lirui.car.Engine!-- 通过构造器传递参数 --constructor-arg valueV8/ /bean id 属性指定了 Bean 的标识符在容器中唯一。 class 属性指定了 Bean 对应的 Java 类。 Bean 的作用域 Spring Bean 的作用域scope决定了 Bean 实例的生命周期和它在容器中的共享方式。常用的作用域包括 Singleton默认作用域Spring 容器中默认是单例模式容器创建一个 Bean 实例所有引用该 Bean 的地方都使用这个单例实例。 Prototype每次请求时都会创建一个新的 Bean 实例。 Request仅在 web 应用中有效表示每个 HTTP 请求都会生成一个新的 Bean 实例。 Session仅在 web 应用中有效表示每个 HTTP 会话Session都会生成一个新的 Bean 实例。 bean idcar classcom.lirui.car.Car scopeprototypeconstructor-arg refengine/ /bean​​​​​​​注入依赖  构造器注入 ​​​​​​​​​​​​​​构造器注入是通过 constructor-arg 元素来为 Bean 的构造函数传递参数。如果 Bean 有多个构造函数可以通过 index 或 name 属性来指定使用哪个构造函数。 value 用于传递简单类型的值如字符串、数字等。 ref 用于注入其他 Bean。  属性注入Setter 注入 ​​​​​​​属性注入是通过 property 元素为 Bean 的属性注入值或引用其他 Bean。 name 属性指定目标属性的名称。 ref 属性指定要注入的其他 Bean。 bean idcar classcom.lirui.car.Carproperty namefeaturesmapentry keyengine value-refengine/entry keygps value-refgps//map/property /bean ​​​​​​​集合注入 注入一个集合类型如 List、Set、Map、Properties 等Spring 提供了对集合类型的支持。 bean idcar classcom.lirui.car.Carproperty namefeatureslistvalueGPS/valuevalueRadio/value/list/property /bean ​​​​​​​​​​​​​​注入 Map bean idcar classcom.lirui.car.Carproperty namefeaturesmapentry keyengine value-refengine/entry keygps value-refgps//map/property /bean注入 Properties bean idcar classcom.lirui.car.Carproperty namepropertiespropsprop keycolorred/propprop keytypesedan/prop/props/property /bean 自动装配Autowiring 自动装配允许 Spring 自动为 Bean 注入依赖Spring 提供了几种自动装配的方式autowirebyName、autowirebyType 和 autowireconstructor。 byName 根据 Bean 名称来进行自动装配 byType 根据 Bean 的类型来进行自动装配 构造器自动装配 根据构造器参数的类型来自动装配相应的 Bean。 ​​​​​​​​​​​​​​加载 Bean 配置文件 Spring 提供了两种方式来加载 XML 配置文件通过 ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext。 ApplicationContext context new ClassPathXmlApplicationContext(beans.xml); Car car (Car) context.getBean(car); car.drive(); ApplicationContext context new FileSystemXmlApplicationContext(path/to/beans.xml); Car car (Car) context.getBean(car); car.drive(); 基于注解的配置Component、Service、Repository、Controller ​​​​​​​​​​​​​​注解配置在代码中直接标注 Bean 的定义使得配置更加简洁和集中常用的注解包括 Component、Service、Repository、Controller 等。 在 XML 中启用注解扫描 context:component-scan base-packagecom.example /基于 Java 配置类的配置 ​​​​​​​java 配置类基于 Configuration 和 Bean 注解可以完全替代 XML提供更强的类型检查和代码提示。 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;Configuration public class AppConfig {Beanpublic UserRepository userRepository() {return new UserRepository();}Beanpublic UserService userService() {return new UserService(userRepository());} }Spring AOP面向切面编程 AOP 概念与用途 ​​​​​​​​​​​​​​AOP 的核心是切面Aspect通过定义切面可以在应用的各个切入点Join Point执行特定的行为。常见的横切关注点包括 日志记录可以在方法执行前后记录日志。 事务管理确保在方法执行期间的数据库操作符合 ACID 特性。 权限检查在方法执行前检查用户权限。 AOP 切入点和通知类型前置通知、后置通知、环绕通知等 ​​​​​​​​​​​​​​在 Spring AOP 中通知Advice是指在指定时间执行的动作。主要有以下几种类型 前置通知Before在目标方法执行前执行。 后置通知After在目标方法执行后执行。 返回通知AfterReturning在目标方法正常返回后执行。 异常通知AfterThrowing在目标方法抛出异常后执行。 环绕通知Around可以在目标方法执行前后进行操作。 使用 Aspect 注解配置 AOP ​​​​​​​开切pom.xml ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns: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/modelVersiongroupIdcom.lirui/groupIdartifactIddemo05/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!-- Spring Context --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.10/version/dependency!-- Spring aop --dependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion5.3.10/version/dependency!-- Spring aop --dependencygroupIdorg.aspectj/groupIdartifactIdaspectjrt/artifactIdversion1.9.8/version/dependency!-- Spring aop --dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.8/version/dependency!-- Spring Beans --dependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion5.3.10/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.10/version/dependency/dependencies/project package com.lirui.car;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;Aspect Component public class CarAspect {// 在 Car 类的 drive 方法执行之前执行此方法Before(execution(void com.lirui.car.Car.drive()))public void logBefore(JoinPoint joinPoint) {System.out.println(Car drive() 方法执行之前的日志);}// 在 Car 类的 drive 方法执行之后执行此方法After(execution(void com.lirui.car.Car.drive()))public void logAfter(JoinPoint joinPoint) {System.out.println(Car drive() 方法执行之后的日志);} }配置类XML 配置 配置类Java 配置 EnableAspectJAutoProxy // 启用 AOP 支持 还能自定义注解 然后实现日志操作  后边再说吧
http://www.hkea.cn/news/14450148/

相关文章:

  • 用手机看网站源代码企业网站设计经典案例
  • 仙桃网站设计公司房产获客软件
  • 51我们一起做网站app软件定制开发平台
  • 阿里云虚拟主机多个网站吗在什么网站做调查问卷
  • 免费永久网站制作专业的营销型网站
  • 广东h5网站建设贵阳网站设计
  • 网站二级页面做哪些东西163公司企业邮箱
  • 做亚马逊常用的网站做直发网站
  • 中山网站制作工具wordpress php5.4支持
  • 公司网站谁负责做我国空间站建造
  • 万网做网站花多少钱网站开发环境windows7的优点
  • 美工培训网站wordpress python脚本
  • 百度做一个网站多少钱联想官网网上商城
  • 东莞最新网站建设软件wordpress百度影音
  • 一站式媒体发稿平台在线制作logo设计
  • 网站服务器去哪买的商业信息
  • 东莞网站设计百年wordpress canvas 粒子跟随特效
  • 临夏州住房和城乡建设局网站官网做得好的公司
  • 开远市新农村数字建设网站淘宝关键词排名是怎么做的
  • 徐汇网站制作设计电商网站开发代码
  • 如何给网站做seo优化孩子学编程一年要多少钱
  • 宁波网站营销推广策划方案seo网络营销推广排名
  • 网页制作与网站开发用的软件论坛网站需要多大的空间
  • 传媒公司制作网站如何用网站模板做网站
  • 网站外包建设公司网站关键词优化怎么做
  • 网站开发应如何入账html网页导航栏模板
  • 大学生作业代做网站天峨县建设局网站
  • 做游戏用什么电脑系统下载网站好呼市网站建设
  • 深圳宝安网站设计公司上海南建设培训执业中心网站
  • 做合成照片的国外网站网站建设 seojsc