Spring uses javaconfig to implement configuration


Do not use spring‘s XML configuration, leave it to Java!

Javaconfig is a sub project of spring. After spring 4, it is called the core function of spring!

Entity class:

package com.lrx.poji;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//Indicates that this class is registered in the container by spring
public class User {
 private String name;

 public String getName() {
   return name;

 public void setName(String name) {
   this.name = name;

 public String toString() {
   return "User{" +
       "name='" + name + '\'' +

Configuration file:

package com.lrx.config;

import com.lrx.poji.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

//This will also be hosted by the spring container, because it is originally a @ component
//The @ configuration represents a class, just like the one before us ApplicationContext.xml It's the same
public class LiConfig {
  //Registering a bean is equivalent to a bean tag written in XML
  //The name of this method is equivalent to the ID attribute in the bean tag
  //The return value of the method is equivalent to the class attribute in the bean tag
  public User getUser(){
    Return new user(); // is the object to be injected into the bean

Test class:

import com.lrx.config.LiConfig;
import com.lrx.poji.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {
  public static void main(String[] args) {
    //If we use the configuration class method completely, we can only get the container through the annotationconfig context
    //Then load it by configuring the class object of the class!
    ApplicationContext context=new AnnotationConfigApplicationContext(LiConfig.class);
    User getUser= (User) context.getBean("user");

This pure Java configuration method can be seen everywhere in spring boot!

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.