美文网首页
@bean&@configuration

@bean&@configuration

作者: leven丶零度 | 来源:发表于2020-05-18 16:59 被阅读0次

    使用xml配置文件注入bean

    1、建立实体类

    package com.leven.bean;
    
    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() {
            super();
        }
    
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    

    2、建立xml文件,注入bean。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean class="com.leven.bean.Person" id="person">
            <property name="age" value="1"></property>
            <property name="name" value="leven"></property>
        </bean>
    </beans>
    

    3、建立主程序入口,获取容器中的person。

    package com.leven.main;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainTest {
        public static void main(String[] args) {
    
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            Object person = applicationContext.getBean("person");
            System.out.println(person);
        }
    }
    
    

    4、结果

    Person{name='leven', age=1}
    

    2.2、使用注解的方式注入bean

    1、创建配置类

    package com.leven.config;
    
    import com.leven.bean.Person;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    // 使用@Configuration标示为配置类
    @Configuration
    public class MainConfig {
    
        //如果不指定id,则默认为方法名。
        @Bean
        public Person person(){
            Person person = new Person();
            person.setAge(18);
            person.setName("leven");
            return new Person("leven",18);
        }
    
        //指定value后容器中的id为指定的值
        @Bean(value = "person_01")
        public Person person01(){
            Person person = new Person();
            person.setAge(18);
            person.setName("leven");
            return new Person("leven",18);
        }
    }
    
    

    2、主程序中获取配置类bean

    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
    Object  person = applicationContext.getBean("person");
    System.out.println(person);
    
    // 获取person在容器中的名称
    String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
    for (String name : namesForType) {
        System.out.println(name);
    }
    

    3、结果

    可以看到person01在容器中的id为自己指定的value值person_01

    Person{name='leven', age=18}
    person
    person_01
    

    相关文章

      网友评论

          本文标题:@bean&@configuration

          本文链接:https://www.haomeiwen.com/subject/zzqpohtx.html