02、组件注册-@Configuration&@Bean给容器中注册组件

 

 

package com.lei.study_09_13.bean;

/**
 *
 *
 * @author LeiLei
 * @date 2019/9/13
 */
public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

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

 

package com.lei.study_09_13.config;

import com.lei.study_09_13.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 *
 * @author LeiLei
 * @date 2019/9/13
 */
@Configuration
public class BeanConfig {

    @Bean
    public Person person() {
        return new Person("lei",24);
    }

    @Bean
    public Person getPerson() {
        return new Person("lei",25);
    }

    @Bean("person2")
    public Person person2() {
        return new Person("lei2",26);
    }

}
package com.lei.study_09_13;

import com.lei.study_09_13.bean.Person;
import com.lei.study_09_13.config.BeanConfig;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Map;

/**
 * 1.xml测试中,bean中必须有无参构造方法,否则报错。原因,先实例化类,再进行属性注入。
 * 2.anno测试中,bean中无需无参构造方法,因为底层使用反射执行method.invoke执行实例化类。
 * 3.Person bb = context.getBean(Person.class);该方法根据组件类型获取实例,如果实例有2或多个时,使用该方法会报found 2错误。
 * 4.beanId默认为方法名,@Bean(id)有id声明,则beanID为id。
 * 5.beanId就是beanName,beanType就是Bean的Class
 *
 * @author LeiLei
 * @date 2019/9/13
 */
public class Test {

    public static void main(String[] args) {
        //1.xml注入测试
        /*ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person aa = (Person)context.getBean("person");
        System.out.println(aa);*/

        //2.anno注入测试
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);

        //根据ID获取
        Person aa = (Person) context.getBean("getPerson");
        System.out.println(aa);

        //该方法用于获取单个实例-获取多个实例的组件时会报错
        //Person bb = context.getBean(Person.class);
        //System.out.println(bb);

        //根据组件获取组件类型
        Map<String, Person> cc = context.getBeansOfType(Person.class);
        System.out.println(cc);

        BeanDefinition dd = context.getBeanDefinition("getPerson");
        System.out.println(dd);

        //根据组件类型获取所有组件名
        String[] ee = context.getBeanNamesForType(Person.class);
        for (String s : ee) {
            System.out.println(s);
        }

        //测试结果
        /*Person56{name='lei', age=25}
        {person=Person56{name='lei', age=24}, getPerson=Person56{name='lei', age=25}, person2=Person56{name='lei2', age=26}}
        Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=beanConfig; factoryMethodName=getPerson; initMethodName=null; destroyMethodName=(inferred); defined in com.lei.study_09_13.config.BeanConfig
        person
        getPerson
        person2*/

    }
} 

总结:

* 1.xml测试中,bean中必须有无参构造方法,否则报错。原因,先实例化类,再进行属性注入。
* 2.anno测试中,bean中无需无参构造方法,因为底层使用反射执行method.invoke执行实例化类。
* 3.Person bb = context.getBean(Person.class);该方法根据组件类型获取实例,如果实例有2或多个时,使用该方法会报found 2错误。
* 4.beanId默认为方法名,@Bean(id)有id声明,则beanID为id。
* 5.beanId就是beanName,beanType就是Bean的Class

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值