Spring-IOC和AOP

Spring-IOC和AOP

参考:
Spring学习:https://how2j.cn/k/spring/spring-ioc-di/87.html?p=36286

JavaEE开发的颠覆者:SpringBoot实战(关注ElevenKeep公众号,回复springboot获得)

一、介绍:

spring是用来解决JavaEE开发中的所有问题。其中每一个被Spring管理的java对象都是一个bean,Spring容器提供了一个IOC容器用来初始化对象,解决对象之间的依赖关系。

二、新建一个web项目

idea创建maven项目

https://blog.csdn.net/lxw983520/article/details/85686285

idea创建java和resources目录

https://blog.csdn.net/qq_34377273/article/details/83183307

三、依赖注入

1.概念

所谓依赖注入,是指容器负责创建对象和维护对象之间的依赖关系,目的是:为了解耦,降低耦合度,过程:IOC(ApplicationContext)负责创建Bean,并把需要注入的Bean放到创建的容器中。

2.Bean的初始化和依赖注入
(1)编写功能类Bean
package com.eleven.bean03;

import org.springframework.stereotype.Service;

@Service // 声明ElevenService类是spring管理的一个bean
public class ElevenService {
	
	public String sayHello(String word) {
		return "hello" + word;
	}
}
(2)使用功能类的Bean
package com.eleven.bean03;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service // 使用@Service注解声明UseElevenService类是spring管理的一个bean。
public class UseElevenService {

	@Autowired // 使用@Autowired注解将ElevenService注入到UseElevenService中,让UseElevenService类具备ElevenService的功能。
	private ElevenService elvenService;

	public String sayHello(String word) {
		return elvenService.sayHello(word);
	}

}
(3)配置类
package com.eleven.bean03;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration // @Configuration声明当前DiConfig类是一个配置类。
@ComponentScan("com.eleven.bean03") // @ComponentScan会自动扫描包下面所有的注解和配置,并注册为bean。
public class DiConfig {

}
(4)运行
package com.eleven.bean03;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
	public static void main(String[] args) {
		// 使用AnnotationConfigApplicationContext作为Spring的容器,接受配置类DiConfig.class作为参数。
		AnnotationConfigApplicationContext context = 
				new AnnotationConfigApplicationContext(DiConfig.class);
		// 获得声明配置UseElevenService.class的类
		UseElevenService useElevenService = context.getBean(UseElevenService.class);
		// 通过给值word传入参数为输出useElevenService里面的方法
		System.out.println(useElevenService.sayHello(" Spring Di"));
		// 关闭,避免资源浪费。
		context.close();
	}

}
3.java配置
(1)编写功能类bean
package com.eleven.javabean03;

// 此处没有使用@Service声明bean
public class ElevenService {

	public String sayHello(String word) {
		return "Hello " + word + "!";
	}

}
(2)使用功能类bean
package com.eleven.javabean03;

// 此处没有使用@Service声明bean
public class UseElevenService {

	// 此处没有使用@Autowired注解声明bean
	ElevenService eleveService;

	public void setElevenService(ElevenService eleveService) {
		this.eleveService = eleveService;
	}

	public String sayHello(String word) {
		return eleveService.sayHello(word);
	}
}
(3)配置类
package com.eleven.javabean03;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration // 声明JavaConfig类是一个配置类
// JavaConfig没有使用@Com注解是因为,所有的bean都在JavaConfig里面定义了
public class JavaConfig {

	@Bean // 声明当前ElevenService类的返回值是一个bean,bean的名称就是方法名。
	public ElevenService elevenService() {
		return new ElevenService();
	}

	@Bean
	public UseElevenService useElevenService() {
		UseElevenService useElevenService = new UseElevenService();
		useElevenService.setElevenService(elevenService());	// 注入UseElevenService的时候,直接调用elevenService()
		return useElevenService;
	}

}
(4)运行
package com.eleven.javabean03;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
		UseElevenService useElevenService = context.getBean(UseElevenService.class);
		System.out.println(useElevenService.sayHello("Java Config"));
		context.close();
	}

}
4.AOP

演示模拟记录操作日志系统的实现

(1)添加pom文件
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId> org.aspectj</groupId>
			<artifactId> aspectjweaver</artifactId>
			<version> 1.9.0</version>
		</dependency>

整个pom.xml贴出来

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.eleven</groupId>
	<artifactId>SpringTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<!-- 定义变量 -->
	<properties>
		<java.version>1.7</java.version>
	</properties>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId> org.aspectj</groupId>
			<artifactId> aspectjweaver</artifactId>
			<version> 1.9.0</version>
		</dependency>
		<!-- 将file转化成字符串 -->
		<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.3</version>
		</dependency>
		<!-- Java规范提案 -->
		<!-- https://mvnrepository.com/artifact/javax.annotation/jsr250-api -->
		<dependency>
			<groupId>javax.annotation</groupId>
			<artifactId>jsr250-api</artifactId>
			<version>1.0</version>
		</dependency>
		<!-- Spring Test 支持 -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>


	</dependencies>


</project>
(2)编写拦截规则的注解
package com.eleven.aop03;

import java.lang.annotation.Documented;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Retention;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.METHOD) // 依用来描述注解的使用范围
@Retention(RetentionPolicy.RUNTIME) // 修饰注解
@Documented // 表示这个注解是由javadoc记录的
public @interface Action { // 定义一个注解@Action
	
	String name();
}
(3)编写使用注解的被拦截类
package com.eleven.aop03;

import org.springframework.stereotype.Service;

@Service // 声明DemoAnnotationService是spring管理的一个bean
public class DemoAnnotationService {
	@Action(name = "注解式拦截的add操作")
	public void add() {}
}
(4)编写使用方法规则被拦截类
package com.eleven.aop03;

import org.springframework.stereotype.Service;

@Service	// 声明DemoMethodService时spring管理的一个bean
public class DemoMethodService {
	public void add() {}
	
}
(5)编写切面
package com.eleven.aop03;

import java.lang.reflect.Method;

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.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect // 通过该注解声明一个切面
@Component // 通过该注解让此切面成为spring容器管理的一个bean
public class LogAspect {

	@Pointcut("@annotation(com.eleven.aop03.Action)") // 通过@Pointcut这个注解声明切点
	public void annotationPointCut() {
	};

	@After("annotationPointCut()") // 通过@After注解声明一个建言,并使用@PointCut定义的切点
	public void after(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		Action action = method.getAnnotation(Action.class);
		System.out.println("注解式拦截" + action.name()); // 通过反射可以获得注解上的属性,然后做日志记录相关的操作
	}

	@Before("execution(* com.eleven.aop03.DemoMethodService.*(..))") // 通过@Before声明一个建言,此建言直接使用拦截规则作为参数
	public void before(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		System.out.println("方法规则式拦截" + method.getName());// 通过反射可以获得注解上的属性,然后做日志记录相关的操作
	}

}
(6)配置类
package com.eleven.aop03;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("com.eleven.aop03")
@EnableAspectJAutoProxy // 使用该注解开启Spring对AspectJ代理的支持
public class AopConfig {

}

(7)运行
package com.eleven.aop03;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
		DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
		DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
		demoAnnotationService.add();
		demoMethodService.add();
		context.close();
	}

}
(8)输出
注解式拦截注解式拦截的add操作
方法规则式拦截add
回答: Spring的DI(依赖注入)和IOC(控制反转)是Spring框架中的两个核心概念。DI是指通过外部容器来注入对象的依赖关系,而不是在对象内部创建或查找依赖对象。IOC是指将对象的创建和依赖关系的管理交给容器来完成,而不是由对象自己来管理。\[1\] 在Spring中,IOC容器的初始化过程包括加载配置文件、解析配置文件、实例化Bean对象并将其存储在IOC容器中。可以通过注解来实现基于注解的容器初始化,通过在类上添加注解来标识其作为Bean,并通过注解来指定依赖关系。\[2\] AOP(面向切面编程)是Spring框架中的另一个重要概念,它允许在程序运行期间动态地将额外的行为织入到代码中,而不需要修改原始代码。AOP的实现方式包括基于代理的方式和基于字节码增强的方式。AOP的原理是通过在目标方法的前后插入切面逻辑来实现。\[2\] 在Spring中,可以通过注解来标识切面,并通过注解来指定切入点和通知类型。常用的AOP注解包括@Aspect、@Pointcut、@Before、@After等。\[3\] 总结来说,DI和IOCSpring框架中用于管理对象依赖关系的机制,而AOP是用于实现横切关注点的机制。它们都是Spring框架中重要的特性,可以帮助开发者更好地组织和管理代码。 #### 引用[.reference_title] - *1* [Spring IoCAOP的通俗理解](https://blog.csdn.net/qq_39144436/article/details/123394242)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [关于SpringIoCAOP的面试题,快看看你都能答上来哪些](https://blog.csdn.net/Gaowumao/article/details/124919483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [面试汇总-Spring-IOCAOP](https://blog.csdn.net/weixin_37672801/article/details/126415598)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值