Spring ---- JavaConfig class instead of XML configuration Bean

1. JavaConfig Implementation Bean object creation:

  AnnotationConfigurationApplicationContext another container by the implementation class Spring ApplicationContext

  ApplicationContext ac = new AnnotationConfigurationApplicationContext ( "package name | configuration class | ? The bean plants ") dependent on the plant, the scanning component injection property values.

    ac.getBean ();

    1.@Configuration    @Import("config.class") 

  Equivalent bean.xml configuration class  

@Configuration
@ComponentScan ( "com.xxx.beanObject") // scan package all the classes, the automatic assembly view annotations,  
@Import("javaconfig2.class")
public class  javaconfig{
        
         @Bean
        public beanObject  getUser(){ return  beanObject;}     
} 

  

@Component // Direct Registration User class for the bean
public class User {
    @Value ( "1") // initial value to the bean property
    private int id;
    @Value("chen")
    private String name;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

  

Guess you like

Origin www.cnblogs.com/chencn/p/12332538.html