码迷,mamicode.com
首页 > 其他好文 > 详细

注解-组件注册01-@Configuration@Bean

时间:2020-06-09 18:25:23      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:组件   autowire   tty   code   rop   zhang   pre   play   author   

  实体类:

技术图片
 1 package spring_annotation.com.anno.bean;
 2 
 3 /**
 4  * @author Millet
 5  * @date 2020/6/8 20:12
 6  */
 7 public class Person {
 8     private String name;
 9     private Integer age;
10 
11     public String getName() {
12         return name;
13     }
14 
15     public void setName(String name) {
16         this.name = name;
17     }
18 
19     public Integer getAge() {
20         return age;
21     }
22 
23     public void setAge(Integer age) {
24         this.age = age;
25     }
26 
27     @Override
28     public String toString() {
29         return "Person{" +
30                 "name=‘" + name + ‘\‘‘ +
31                 ", age=" + age +
32                 ‘}‘;
33     }
34 
35     public Person() {
36     }
37 
38     public Person(String name, Integer age) {
39         this.name = name;
40         this.age = age;
41     }
42 }
Person

  向Spring容器中注册,可以在xml文件中进行配置:

<bean id="person" class="spring_annotation.com.anno.bean.Person">
        <property name="age" value="18"></property>
        <property name="name" value="zhangsan"></property>
</bean>
1 public static void main(String[] args) {
2   //1.xml文件配置
3   ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
4    Person person = (Person) applicationContext.getBean("person");
5   System.out.println(person);//Person{name=‘zhangsan‘, age=18}
6 }

  还可以通过配置类来进行装配:@Configuration和@Bean搭配使用

  @Configuration :告诉Spring这是一个配置类,类似于配置文件,类中可进行各种类装配;
  @Bean("person") :给容器注册一个bean;类型为返回值类型,id默认是方法名或者自定义;
//配置类 == 配置文件
@Configuration //告诉Spring这是一个配置类
public class MainConfig {
    @Bean("person") //给容器注册一个bean;类型为返回值类型,id默认是方法名或者自定义
    public Person getPerson(){
        return new Person("lisi",20);
    }
}
public static void main(String[] args) {
  //2.配置类
  ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
  Person person = applicationContext.getBean(Person.class);
  System.out.println(person);//Person{name=‘lisi‘, age=20}//Person{name=‘lisi‘, age=20}
}

 

  @Bean源码

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
  //1. name 和 value 两个属性是相同的含义的 @AliasFor(
"name") String[] value() default {};//默认情况下bean的名称和方法名称相同,你也可以使用name属性来指定 @AliasFor("value") String[] name() default {};   //2. 装配方式
  //Autowire.NO (默认设置)
  //Autowire.BY_NAME
  //Autowire.BY_TYPE
/** @deprecated */ @Deprecated Autowire autowire() default Autowire.NO; boolean autowireCandidate() default true; String initMethod() default "";//执行生命周期的初始化方法,类似@PostConstruct String destroyMethod() default "(inferred)";//执行生命周期的销毁方法(单实例才会调用),@PreDestroy的方法
 }

 

注解-组件注册01-@Configuration@Bean

标签:组件   autowire   tty   code   rop   zhang   pre   play   author   

原文地址:https://www.cnblogs.com/qmillet/p/13074177.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!