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