Spring framework practice: Hello World

Posted by daltman1967 on Wed, 06 Oct 2021 01:44:53 +0200

All the code used in this blog has been released On GitHub.

1 Spring overview

Spring version history (Reference) wiki-Version history):

1.0March 24, 2004First production release.

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:

  1. Bean: every Java object managed by Spring is called a bean
  2. Dependency injection (DI): dependency injection can inject (combine) one object into another object
  3. 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
  4. 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:

  1. @Component has no explicit role
  2. @Service is used in the business logic layer (service layer)
  3. @Repository is used in the data access layer (dao layer)
  4. @Controller, used in the control layer

Annotation injected into Bean:

  1. @Autowired: provided by spring, I translate it into automatic connection
  2. @Inject: provided by jsr-330
  3. @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

  1. @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
  2. @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
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
public class FunctionServiceUser {
    // Use @ Autowired to inject FunctionService into FunctionServiceUser,
    // Using @ Inject or @ Resource here is equivalent
    FunctionService functionService;

    public String sayHello(String name) {
        return functionService.sayHello(name);

Configuration class:

// @Configuration declares that the current class is a configuration class
// @ComponentScan automatically scans all used @ services under the package,
// @Component, @Repository, @Controller class,
// And register them as beans
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

Topics: Java Spring Spring Boot