All the code used in this blog has been released On GitHub.
1 Spring overview
Spring version history (Reference) wiki-Version history):
Version | Date | Notes |
---|---|---|
0.9 | 2003 | |
1.0 | March 24, 2004 | First production release. |
2.0 | 2006 | |
3.0 | 2009 | |
4.0 | 2013 | |
5.0 | 2017 |
At present (2021-10-5), the latest GA(general availability) version of spring framework is 5.3.10.
Phase 1: xml configuration
In the Spring 1.x era, beans were configured using xml
Stage 2: annotation configuration
In the Spring 2.x era, with JDK 1.5 supporting annotations, Spring provides annotations for declaring beans (such as @ Component, @Service), greatly reducing the workload of configuration
Phase 3: Java configuration
From Spring 3.x to now, spring provides the ability of Java configuration. Spring recommends using java configuration
Core concepts in Spring:
- Bean: every Java object managed by Spring is called a bean
- Dependency injection (DI): dependency injection can inject (combine) one object into another object
- Inversion of control (IOC): the Spring IoC container is responsible for initializing beans and managing dependencies between objects. The so-called inversion of control is to hand over Java objects to the Spring IoC container for management instead of traditional callers
- Aspect oriented programming (AOP): the main purpose is decoupling. AOP allows a group of classes to share the same behavior
2 dependency injection
Annotation for declaring Bean:
- @Component has no explicit role
- @Service is used in the business logic layer (service layer)
- @Repository is used in the data access layer (dao layer)
- @Controller, used in the control layer
Annotation injected into Bean:
- @Autowired: provided by spring, I translate it into automatic connection
- @Inject: provided by jsr-330
- @Resource: provided by jsr-250
3 Java configuration
Java configuration is recommended by Spring 4.x and can completely replace xml configuration
Java configuration is also recommended by Spring Boot
Java Configuration is implemented through @ Configuration and @ Bean
- @Configuration is a class annotation that declares that the current class is a configuration class. It is equivalent to the xml file of Spring configuration
- @Bean is a method annotation that declares the return value of the current method as a bean
stay Dependency configuration section Four dependency injection methods @ Component, @Service, @Repository and @ Controller are introduced. These four methods are annotation configuration
In general,
Java configuration is used for global configuration (such as database configuration and MVC related configuration);
Annotation configuration is used in business Bean configuration
3 Hello World example
Complete code On GitHub , only some main code fragments are shown here
Write a Bean of a function class (for business logic processing):
// Use the @ Service annotation to declare that the current class is a Bean managed by Spring // Here, @ Component, @Service, @Repository and Controller are equivalent, // It can be selected according to needs @Service public class FunctionService { public String sayHello(String name) { return "Hello " + name; } }
Write a Bean that calls the above function class:
// Annotation declares that the current class is a Bean managed by Spring @Service public class FunctionServiceUser { // Use @ Autowired to inject FunctionService into FunctionServiceUser, // Using @ Inject or @ Resource here is equivalent @Autowired FunctionService functionService; public String sayHello(String name) { return functionService.sayHello(name); } }
Configuration class:
// @Configuration declares that the current class is a configuration class @Configuration // @ComponentScan automatically scans all used @ services under the package, // @Component, @Repository, @Controller class, // And register them as beans @ComponentScan("cn.mitrecx.ch1.di") public class DiConfig { }
Note the package path cn.mitrecx.ch1.di, and the structure of my code directory is as follows:
Compile, execute:
✗ mvn clean package ✗ java -jar target/helloworld-1.0-SNAPSHOT.jar
Execution results:
Oct 05, 2021 6:40:39 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@312b1dae: startup date [Tue Oct 05 18:40:39 CST 2021]; root of context hierarchy Hello Rosie Oct 05, 2021 6:40:40 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@312b1dae: startup date [Tue Oct 05 18:40:39 CST 2021]; root of context hierarchy