小言_互联网的博客

什么是IOC和什么是AOP

382人阅读  评论(0)

什么是IOC

控制反转,将对象的创建进行反转,常规情况下,对象都是开发者手动创建的,使用IOC开发者不再需要创建对象,而是由IOC容器根据需求自动创建项目所需要的对象。

相关依赖:


  
  1. <dependency>
  2. <groupId>org.springframework </groupId>
  3. <artifactId>spring-context </artifactId>
  4. <version>5.3.15 </version>
  5. </dependency>

案例:

随便创建一个实体类:


  
  1. package org.example.pojo;
  2. import lombok.Data;
  3. import lombok.experimental.Accessors;
  4. @Data
  5. @Accessors(chain = true)
  6. public class DataConfig {
  7. private String ip;
  8. private String dataBaseName;
  9. private String userName;
  10. private String pwd;
  11. }

不用IOC,所有对象开发者自己创建:

测试:


  
  1. package org.example.test;
  2. import org.example.pojo.DataConfig;
  3. public class Test {
  4. public static void main (String[] args) {
  5. DataConfig dataConfig = new DataConfig();
  6. dataConfig.setIp( "127.0.0.1")
  7. .setDataBaseName( "test")
  8. .setUserName( "root")
  9. .setPwd( "ok");
  10. System.out.println(dataConfig);
  11. }
  12. }

测试结果:

使用IOC,对象不用开发者创建,而是交给Spring框架来完成:

一、基于XML:

开发者把需要的对象在XML中进行配置,Spring框架读取这个配置文件,根据配置文件的内容来创建对象。

创建一个spring.xml:


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context= "http://www.springframework.org/schema/context"
  5. xmlns:p= "http://www.springframework.org/schema/p"
  6. xmlns:aop= "http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation= "http://www.springframework.org/schema/beans
  8. https://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. https://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. https://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
  13. <bean class="org.example.pojo.DataConfig" id="config">
  14. <property name="ip" value="127.0.0.1" />
  15. <property name="dataBaseName" value="test" />
  16. <property name="userName" value="root" />
  17. <property name="pwd" value="ok" />
  18. </bean>
  19. </beans>

测试:


  
  1. package org.example.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test2 {
  5. public static void main (String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext( "spring.xml");
  7. System.out.println(context.getBean( "config"));
  8. }
  9. }

测试结果:

二、基于注解:

1、配置类

用一个Java类来替代XML文件,把在XML中配置的内容放到配置类中。


  
  1. package org.example.configuration;
  2. import org.example.pojo.DataConfig;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. public class BeanConfiguration {
  7. @Bean(value = "config")
  8. public DataConfig dataConfig (){
  9. DataConfig dataConfig = new DataConfig();
  10. dataConfig.setIp( "127.0.0.1")
  11. .setDataBaseName( "test")
  12. .setUserName( "root")
  13. .setPwd( "ok");
  14. return dataConfig;
  15. }
  16. }

测试:


  
  1. package org.example.test;
  2. import org.example.configuration.BeanConfiguration;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. public class Test3 {
  6. public static void main (String[] args) {
  7. ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfiguration.class);
  8. System.out.println(context.getBean( "config"));
  9. }
  10. }

 测试结果:

2、扫包+注解

更简单的方式,不再需要依赖于XML或者配置类,而是直接将bean的创建交给目标类,在目标类添加注解来创建。


  
  1. package org.example.pojo;
  2. import lombok.Data;
  3. import lombok.experimental.Accessors;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. @Data
  7. @Component
  8. @Accessors(chain = true)
  9. public class DataConfig2 {
  10. @Value("127.0.0.1")
  11. private String ip;
  12. @Value("test")
  13. private String dataBaseName;
  14. @Value("root")
  15. private String userName;
  16. @Value("ok")
  17. private String pwd;
  18. }

测试:


  
  1. package org.example.test;
  2. import org.example.pojo.DataConfig2;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. public class Test4 {
  6. public static void main (String[] args) {
  7. ApplicationContext context = new AnnotationConfigApplicationContext( "org.example.pojo");
  8. System.out.println(context.getBean(DataConfig2.class));
  9. }
  10. }

测试结果:

自动创建对象,完成依赖注入。


  
  1. package org.example.pojo;
  2. import lombok.Data;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. @Data
  7. @Component
  8. public class GlobalConfig {
  9. @Value("8080")
  10. private String port;
  11. @Value("/")
  12. private String path;
  13. @Autowired
  14. private DataConfig2 dataConfig2;
  15. }

测试:


  
  1. package org.example.test;
  2. import org.example.pojo.GlobalConfig;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. public class Test5 {
  6. public static void main (String[] args) {
  7. ApplicationContext context = new AnnotationConfigApplicationContext( "org.example.pojo");
  8. System.out.println(context.getBean(GlobalConfig.class));
  9. }
  10. }

测试结果:

@Autowired 通过类型进行注入,如果需要通过名称取值,通过 @Qualifier 注解完成名称的映射。


  
  1. package org.example.pojo;
  2. import lombok.Data;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Component;
  7. @Data
  8. @Component
  9. public class GlobalConfig2 {
  10. @Value("8080")
  11. private String port;
  12. @Value("/")
  13. private String path;
  14. @Autowired
  15. @Qualifier("config")
  16. private DataConfig2 dataConfig2;
  17. }

测试:


  
  1. package org.example.test;
  2. import org.example.pojo.GlobalConfig2;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. public class Test6 {
  6. public static void main (String[] args) {
  7. ApplicationContext context = new AnnotationConfigApplicationContext( "org.example.pojo");
  8. System.out.println(context.getBean(GlobalConfig2.class));
  9. }
  10. }

测试结果:

什么是AOP

面向切面编程,是一种抽象化的面向对象编程,对面向对象编程的一种补充,底层使用动态代理机制来实现。

相关依赖:


  
  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-aspects</artifactId>
  4. <version> 5.3 .18</version>
  5. </dependency>

应用场景:

打印日志、事务、权限处理

案例:

1、创建切面类


  
  1. package org.example.aop;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.annotation.AfterReturning;
  4. import org.aspectj.lang.annotation.Aspect;
  5. import org.aspectj.lang.annotation.Before;
  6. import org.springframework.stereotype.Component;
  7. import java.util.Arrays;
  8. @Component
  9. @Aspect
  10. public class LoggerAspect {
  11. @Before("execution(public int org.example.aop.CalImpl.*(..))")
  12. public void before (JoinPoint joinPoint){
  13. String name = joinPoint.getSignature().getName();
  14. System.out.println(name+ "方法的参数是"+ Arrays.toString(joinPoint.getArgs()));
  15. }
  16. @AfterReturning(value = "execution(public int org.example.aop.CalImpl.*(..))",returning = "result")
  17. public void afterReturning (JoinPoint joinPoint,Object result){
  18. String name = joinPoint.getSignature().getName();
  19. System.out.println(name+ "方法的结果是"+result);
  20. }
  21. }

2、实现类添加 @Component 注解


  
  1. package org.example.aop;
  2. public interface Cal {
  3. public int add (int num1, int num2);
  4. public int sub (int num1, int num2);
  5. public int mul (int num1, int num2);
  6. public int div (int num1, int num2);
  7. }

  
  1. package org.example.aop;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class CalImpl implements Cal {
  5. @Override
  6. public int add (int num1, int num2) {
  7. int result = num1 + num2;
  8. return result;
  9. }
  10. @Override
  11. public int sub (int num1, int num2) {
  12. int result = num1 - num2;
  13. return result;
  14. }
  15. @Override
  16. public int mul (int num1, int num2) {
  17. int result = num1 * num2;
  18. return result;
  19. }
  20. @Override
  21. public int div (int num1, int num2) {
  22. int result = num1 / num2;
  23. return result;
  24. }
  25. }

3、配置自动扫包,开启自动生成代理对象


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context= "http://www.springframework.org/schema/context"
  5. xmlns:aop= "http://www.springframework.org/schema/aop"
  6. xmlns:p= "http://www.springframework.org/schema/p"
  7. xsi:schemaLocation= "http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
  13. <!-- 自动扫包 -->
  14. <context:component-scan base-package="org.example.aop"> </context:component-scan>
  15. <!-- 开启自动生成代理 -->
  16. <aop:aspectj-autoproxy> </aop:aspectj-autoproxy>
  17. </beans>

4、使用


  
  1. package org.example.test;
  2. import org.example.aop.Cal;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class Test7 {
  6. public static void main (String[] args) {
  7. ApplicationContext context = new ClassPathXmlApplicationContext( "spring2.xml");
  8. Cal bean = context.getBean(Cal.class);
  9. System.out.println(bean.add( 9, 8));
  10. System.out.println(bean.sub( 9, 8));
  11. System.out.println(bean.mul( 9, 8));
  12. System.out.println(bean.div( 9, 8));
  13. }
  14. }

测试结果:

相关视频:

【2022版】2小时学会Spring核心机制IoC和AOP_哔哩哔哩_bilibili


转载:https://blog.csdn.net/weixin_55076626/article/details/127523272
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场