Spring和Spring Framework的组件, 这里对于开发者来说有几个问题:
如下是官方在解释Spring框架的常用场景的图

我加上一些注释后,是比较好理解的;引入这个图,重要的原因是为后面设计一个案例帮助你构建认知。
结合上面的使用场景, 设计一个查询用户的案例的两个需求 ,来看Spring框架帮我们简化了什么开发工作:

<?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>tech.pdai</groupId>
<artifactId>001-spring-framework-demo-helloworld-xml</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>5.3.9</spring.version>
<aspectjweaver.version>1.9.6</aspectjweaver.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
</dependencies>
</project>

package tech.pdai.springframework.entity;
/**
* @author pdai
*/
public class User {
/**
* user's name.
*/
private String name;
/**
* user's age.
*/
private int age;
/**
* init.
*
* @param name name
* @param age age
*/
public User(String name, int age) {
this.name = name;
this.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;
}
}
package tech.pdai.springframework.dao;
import java.util.Collections;
import java.util.List;
import tech.pdai.springframework.entity.User;
/**
* @author pdai
*/
public class UserDaoImpl {
/**
* init.
*/
public UserDaoImpl() {
}
/**
* mocked to find user list.
*
* @return user list
*/
public List<User> findUserList() {
return Collections.singletonList(new User("pdai", 18));
}
}
并增加daos.xml
<?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="tech.pdai.springframework.dao.UserDaoImpl">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>
package tech.pdai.springframework.service;
import java.util.List;
import tech.pdai.springframework.dao.UserDaoImpl;
import tech.pdai.springframework.entity.User;
/**
* @author pdai
*/
public class UserServiceImpl {
/**
* user dao impl.
*/
private UserDaoImpl userDao;
/**
* init.
*/
public UserServiceImpl() {
}
/**
* find user list.
*
* @return user list
*/
public List<User> findUserList() {
return this.userDao.findUserList();
}
/**
* set dao.
*
* @param userDao user dao
*/
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
}
并增加services.xml
<?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">
<!-- services -->
<bean id="userService" class="tech.pdai.springframework.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
</beans>
package tech.pdai.springframework.aspect;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* @author pdai
*/
@Aspect
public class LogAspect {
/**
* aspect for every methods under service package.
*/
@Around("execution(* tech.pdai.springframework.service.*.*(..))")
public Object businessService(ProceedingJoinPoint pjp) throws Throwable {
// get attribute through annotation
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
System.out.println("execute method: " + method.getName());
// continue to process
return pjp.proceed();
}
}
并增加aspects.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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="tech.pdai.springframework" />
<aop:aspectj-autoproxy/>
<bean id="logAspect" class="tech.pdai.springframework.aspect.LogAspect">
<!-- configure properties of aspect here as normal -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>
package tech.pdai.springframework;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tech.pdai.springframework.entity.User;
import tech.pdai.springframework.service.UserServiceImpl;
/**
* @author pdai
*/
public class App {
/**
* main interfaces.
*
* @param args args
*/
public static void main(String[] args) {
// 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();
// print info from beans
userList.forEach(a -> System.out.println(a.getName() + "," + a.getAge()));
}
}

那么Spring框架帮助我们做什么,它体现了什么哪些要点呢?
来看第一个需求: 查询用户 (service通过调用dao查询pojo),本质上如何创建User/Dao/Service等;
UserDaoImpl userDao = new UserDaoImpl(); UserSericeImpl userService = new UserServiceImpl(); userService.setUserDao(userDao); List<User> userList = userService.findUserList();
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基础 - Spring之控制反转(IOC)
来看第二个需求: 给Service所有方法调用添加日志 (调用方法时),本质上是解耦问题;
/**
* find user list.
*
* @return user list
*/
public List<User> findUserList() {
System.out.println("execute method findUserList");
return this.userDao.findUserList();
}
/**
* aspect for every methods under service package.
*/
@Around("execution(* tech.pdai.springframework.service.*.*(..))")
public Object businessService(ProceedingJoinPoint pjp) throws Throwable {
// get attribute through annotation
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
System.out.println("execute method: " + method.getName());
// continue to process
return pjp.proceed();
}
更进一步, 你便能理解为何会有如下的知识点了 :
这边引入我们后续的相关文章: Spring基础 - Spring之面向切面编程(AOP)
通过上述的框架介绍和例子,已经初步知道了Spring设计的两个大的要点:IOC和AOP;从框架的设计角度而言,更为重要的是简化开发,比如提供更为便捷的配置Bean的方式,直至0配置(即约定大于配置)。这里我将通过Spring历史版本的发展,和SpringBoot的推出等,来帮你理解Spring框架是如何逐步简化开发的。
在前文的例子中, 通过xml配置方式实现的,这种方式实际上比较麻烦; 我通过Java配置进行改造:
package tech.pdai.springframework.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import tech.pdai.springframework.aspect.LogAspect;
import tech.pdai.springframework.dao.UserDaoImpl;
import tech.pdai.springframework.service.UserServiceImpl;
/**
* @author pdai
*/
@EnableAspectJAutoProxy
@Configuration
public class BeansConfig {
/**
* @return user dao
*/
@Bean("userDao")
public UserDaoImpl userDao() {
return new UserDaoImpl();
}
/**
* @return user service
*/
@Bean("userService")
public UserServiceImpl userService() {
UserServiceImpl userService = new UserServiceImpl();
userService.setUserDao(userDao());
return userService;
}
/**
* @return log aspect
*/
@Bean("logAspect")
public LogAspect logAspect() {
return new LogAspect();
}
}
package tech.pdai.springframework;
import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import tech.pdai.springframework.config.BeansConfig;
import tech.pdai.springframework.entity.User;
import tech.pdai.springframework.service.UserServiceImpl;
/**
* @author pdai
*/
public class App {
/**
* main interfaces.
*
* @param args args
*/
public static void main(String[] args) {
// create and configure beans
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
// retrieve configured instance
UserServiceImpl service = context.getBean("userService", UserServiceImpl.class);
// use configured instance
List<User> userList = service.findUserList();
// print info from beans
userList.forEach(a -> System.out.println(a.getName() + "," + a.getAge()));
}
}

更进一步,Java 5开始提供注解支持,Spring 2.5 开始完全支持基于注解的配置并且也支持JSR250 注解。在Spring后续的版本发展倾向于通过注解和Java配置结合使用.
package tech.pdai.springframework.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* @author pdai
*/
@Configuration
@EnableAspectJAutoProxy
public class BeansConfig {
}
/**
* @author pdai
*/
@Repository
public class UserDaoImpl {
/**
* mocked to find user list.
*
* @return user list
*/
public List<User> findUserList() {
return Collections.singletonList(new User("pdai", 18));
}
}
/**
* @author pdai
*/
@Service
public class UserServiceImpl {
/**
* user dao impl.
*/
@Autowired
private UserDaoImpl userDao;
/**
* find user list.
*
* @return user list
*/
public List<User> findUserList() {
return userDao.findUserList();
}
}
package tech.pdai.springframework;
import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import tech.pdai.springframework.entity.User;
import tech.pdai.springframework.service.UserServiceImpl;
/**
* @author pdai
*/
public class App {
/**
* main interfaces.
*
* @param args args
*/
public static void main(String[] args) {
// create and configure beans
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
"tech.pdai.springframework");
// retrieve configured instance
UserServiceImpl service = context.getBean(UserServiceImpl.class);
// use configured instance
List<User> userList = service.findUserList();
// print info from beans
userList.forEach(a -> System.out.println(a.getName() + "," + a.getAge()));
}
}

Springboot实际上通过约定大于配置的方式,使用xx-starter统一的对Bean进行默认初始化,用户只需要很少的配置就可以进行开发了。
这个因为很多开发者都是从SpringBoot开始着手开发的,所以这个比较好理解。我们需要的是将知识点都串联起来,构筑认知体系。
最后结合Spring历史版本总结下它的发展:
(这样是不是能够帮助你在整体上构建了知识体系的认知了呢?)

PS:相关代码,可以通过 这里 直接查看