Spring学习-01-Spring简单的例子引入Spring要点
前言
Spring 框架基本涵盖了企业级应用开发的各个方面,它包含了 20 多个不同的模块。这是其开源的GitHub链接,感兴趣的可以去翻一下新手可以先学习Spring框架的使用与原理,等到将来了解原理之后再深入理解。
那么我们直接深入主题,Spring框架应该如何应用,接下来将用一个简单的用例来引出Spring的核心要点,比如IoC和AOP等,之后再慢慢了解使用方法:如XML文件配置,注解等
现在把开发环境先介绍一下
- Java开发工具包(JDK):Java8
- IDEA2021.1.3
- Maven3.6.3
对这些工具只要求简单的使用,对Java的要求会高一些,并希望你能有一下基本的JavaWeb开发经验,有需求和要解决的问题会更方便理解Spring的简便之处。其他的两个工具都只是会用就行(我现在也不是太明白,希望能在开发学习中慢慢积累),没有安装的可以在网上找一下资料,如果有需要的话可以评论,有很多朋友需要的话以后我也会写安装和基础配置教程。
一、设计一个Spring的HelloWorld
结合上面的使用场景,设计一个查询用户的案例的两个需求,来看Spring框架帮我们简化了什么开发工作:
- 查询用户数据 - 来看DAO+POJO-> Service 的初始化和装载。
- 给所有Service的查询方法记录日志
创建一个Maven的Java项目
引入Spring框架的POM依赖,以及查看这些依赖之间的关系
<?xml version="1.0" encoding="UTF-8"?>
<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.hellospring</groupId>
<artifactId>HelloSpringDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.21</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.21</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.21</version>
</dependency>
</dependencies>
</project>
POJO-User
package com.hello.spring.entity;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
DAO 获取 POJO, UserDaoServiceImpl (mock 数据)
package com.hello.spring.dao.impl;
import com.hello.spring.entity.User;
import java.util.Collections;
import java.util.List;
public class UserDaoImpl {
public UserDaoImpl() {
}
public List<User> findUserList(){
return Collections.singletonList(new User("zhang",18));
}
}
业务层UserServiceImpl(调用DAO层)
package com.hello.spring.service.impl;
import com.hello.spring.dao.impl.UserDaoImpl;
import com.hello.spring.entity.User;
import java.util.List;
public class UserServiceImpl {
private UserDaoImpl userDao;
public UserServiceImpl() {
}
public UserServiceImpl(UserDaoImpl userDao) {
this.userDao = userDao;
}
public List<User> findUserList(){
return this.userDao.findUserList();
}
public void setUserDao(UserDaoImpl userDao){
this.userDao = userDao;
}
}
在applicationContext.xml中增加bean配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.hello.spring.dao.impl.UserDaoImpl">
</bean>
<bean id="userService" class="com.hello.spring.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
拦截所有service中的方法,并输出记录
package com.hello.spring.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.Method;
@Aspect
public class LogAspect {
@Around("execution(* com.hello.spring.service.*.*(..))")
public Object businessService(ProceedingJoinPoint pjp) throws Throwable {
Method method = ((MethodSignature)pjp.getSignature()).getMethod();
return pjp.proceed();
}
}
在applicationContext.xml中添加内容,最终的xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.hello.spring"/>
<aop:aspectj-autoproxy/>
<bean id="logAspect" class="com.hello.spring.aspect.LogAspect">
<!-- configure properties of aspect here as normal -->
</bean>
<bean id="userDao" class="com.hello.spring.dao.impl.UserDaoImpl">
</bean>
<bean id="userService" class="com.hello.spring.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
组装App
package com.hello.spring;
import com.hello.spring.entity.User;
import com.hello.spring.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServiceImpl userService = context.getBean("userService",UserServiceImpl.class);
List<User> userList = userService.findUserList();
for(User user : userList){
System.out.println(user);
}
}
}
整体结构和运行app
现在开始我们来分析这个例子体现了Spring的哪些核心要点
Spring框架帮助我们做了什么,它体现了什么要点呢?
1、控制反转IOC
来看第一个需求:查询用户(service通过调用dao查询pojo),本质上是如何创建User/Dao/Servcie等;
如果没有Spring框架,我们需要自己创建User/Dao/Service等,比如:
UserDaoImpl userDao = new UserDaoImpl();
UserSericeImpl userService = new UserServiceImpl();
userService.setUserDao(userDao);
List<User> userList = userService.findUserList();
有了Spring框架,可以将原有Bean的创建工作转给框架, 需要用时从Bean的容器中获取即可,这样便简化了开发工作
Bean的创建和使用分离了。
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("aspects.xml", "daos.xml", "services.xml");
// retrieve configured instance
UserServiceImpl service = context.getBean("userService", UserServiceImpl.class);
// use configured instance
List<User> userList = service.findUserList();
更进一步,我们来理解一下的知识点:
- Spring框架管理这些Bean的创建工作,即由用户管理Bean转变为框架管理Bean,这个就叫控制反转 - Inversion of Control (IoC)
- Spring 框架托管创建的Bean放在哪里呢? 这便是IoC Container(IOC容器);
- Spring 框架为了更好让用户配置Bean,必然会引入不同方式来配置Bean? 这便是xml配置,Java配置,注解配置等支持
- Spring 框架既然接管了Bean的生成,必然需要管理整个Bean的生命周期等;
- 应用程序代码从Ioc Container中获取依赖的Bean,注入到应用程序中,这个过程叫 依赖注入(Dependency Injection,DI) ; 所以说控制反转是通过依赖注入实现的,其实它们是同一个概念的不同角度描述。通俗来说就是IoC是设计思想,DI是实现方式
- 在依赖注入时,有哪些方式呢?这就是构造器方式,@Autowired, @Resource, @Qualifier… 同时Bean之间存在依赖(可能存在先后顺序问题,以及循环依赖问题等)
这边之后我们会继续学习相关的内容:Spring学习-Spring之控制反转(IOC)
2、面向切面 - AOP
来看第二个需求:**给Service所有方法调用添加日志**(调用方法时),本质上是解耦问题;
如果没有Spring框架,我们需要在每个service的方法中都添加记录日志的方法,比如:
public List<User> findUserList() {
System.out.println("execute method findUserList");
return this.userDao.findUserList();
}
有了Spring框架,通过@Aspect注解 定义了切面,这个切面中定义了拦截所有service中的方法,并记录日志; 可以明显看到,框架将日志记录和业务需求的代码解耦了,不再是侵入式的了
@Around("execution(* tech.pdai.springframework.service.*.*(..))")
public Object businessService(ProceedingJoinPoint pjp) throws Throwable {
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
System.out.println("execute method: " + method.getName());
return pjp.proceed();
}
更进一步,我们便能理解为何会有如下的知识点了:
- Spring 框架通过定义切面, 通过拦截切点实现了不同业务模块的解耦,这个就叫面向切面编程 - Aspect Oriented Programming (AOP)
- 为什么@Aspect注解使用的是aspectj的jar包呢?这就引出了Aspect4J和Spring AOP的历史渊源,只有理解了Aspect4J和Spring的渊源才能理解有些注解上的兼容设计
- 如何支持更多拦截方式来实现解耦, 以满足更多场景需求呢? 这就是@Around, @Pointcut… 等的设计
- 那么Spring框架又是如何实现AOP的呢? 这就引入代理技术,分静态代理和动态代理,动态代理又包含JDK代理和CGLIB代理等
这边引入我们后续的相关文章:Spring基础 - Spring之面向切面编程(AOP)
总结
面向切面编程 - Aspect Oriented Programming (AOP)*
2. 为什么@Aspect注解使用的是aspectj的jar包呢?这就引出了Aspect4J和Spring AOP的历史渊源,只有理解了Aspect4J和Spring的渊源才能理解有些注解上的兼容设计
3. 如何支持更多拦截方式来实现解耦, 以满足更多场景需求呢? 这就是@Around, @Pointcut… 等的设计
4. 那么Spring框架又是如何实现AOP的呢? 这就引入代理技术,分静态代理和动态代理,动态代理又包含JDK代理和CGLIB代理等
这边引入我们后续的相关文章:Spring基础 - Spring之面向切面编程(AOP)
总结
通过上述的框架介绍和例子,已经初步知道了Spring设计的两个大的要点:IOC和AOP;从框架的设计角度而言,更为重要的是简化开发,比如提供更为便捷的配置Bean的方式,直至0配置(即约定大于配置)。这里我将通过Spring历史版本的发展,和SpringBoot的推出等,来帮你理解Spring框架是如何逐步简化开发的。