Don't use Spring's XML configuration, leave it to Java for full permission!
JavaConfig is a subproject of Spring. After Spring 4, it is called the core feature of Spring!
Entity Class:
package ; import ; import ; //It means that this class is registered in the container by Spring@Component public class User { @Value("lixin") private String name; public String getName() { return name; } public void setName(String name) { = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
Configuration file:
package ; import ; import ; import ; import ; //This will also be hosted by the Spring container, because it is originally a @Component// @Configuration represents a class, just like what we were before@Configuration @ComponentScan("") public class LiConfig { //Register a bean, which 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 @Bean public User getUser(){ return new User(); //It is to inject the object into the bean } }
Test class:
import ; import ; import ; import ; 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! ApplicationContext context=new AnnotationConfigApplicationContext(); User getUser= (User) ("user"); (()); } }
This pure Java configuration method can be seen everywhere in Spring Boot!
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.