作用:相当于xml配置文件里的<beans>、<bean>配置

public class Person {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "this.name=" + this.name + "," + "this.age=" + this.age;
    }
}
@Configuration
public class ConfigurationBean {
    @Bean("person")
    public Person getPerson() {
        Person person = new Person();
        person.setAge(25);
        person.setName("小东");
        return person;
    }
}
public class MainTest {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(
                ConfigurationBean.class);
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person);
        Person person2 = applicationContext.getBean(Person.class);
        System.out.println(person2);
        String[] types = applicationContext.getBeanNamesForType(Person.class);
        for(String type : types) {
            System.out.println("type:" + type);
        }
    }
}

打印:

this.name=小东,this.age=25
this.name=小东,this.age=25
type:person