1.使用JavaConfig实现Bean对象创建:

  通过Spring  ApplicationContext的另一个容器AnnotationConfigurationApplicationContext的实现类

  ApplicationContext   ac=  new AnnotationConfigurationApplicationContext(“包名|配置类|bean工厂?”) 依赖工厂,扫描组件注入属性值。

    ac.getBean();

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

  相当于bean.xml配置类  

@Configuration
@ComponentScan("com.xxx.beanObject")  //扫描包下所有的类,查看注解自动装配,  
@Import("javaconfig2.class")
public class  javaconfig{
        
         @Bean
        public beanObject  getUser(){ return  beanObject;}     
} 

  

@Component    //直接注册User类为bean
public class User {
    @Value("1")  //给bean 属性赋初值
    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;
    }
}

  

相关文章:

  • 2021-12-20
  • 2022-12-23
  • 2021-06-23
  • 2022-12-23
  • 2022-01-06
  • 2021-07-14
  • 2021-09-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
相关资源
相似解决方案