@Configuration和@bean給容器中註冊組件

spring容器中註冊組件的兩種方式:java

  • xml配置文件方式
  • 註解方式

一、xml配置文件方式

  • 編寫bean類
  • 使用xml文件配置bean注入到spring容器
  • 經過ClassPathXmlApplicationContext類獲取bean

1.一、編寫bean類spring

package com.jcon.entity;

/**
 * @author Jcon
 * @version V1.0
 * @Description: (用戶實體類)
 * @date 2019年01月28日
 */
public class Person {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

1.二、使用xml文件配置bean注入到spring容器app

<?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 id="person" class="com.jcon.entity.Person">
        <property name="age" value="20"/>
        <property name="name" value="張三"/>
    </bean>

</beans>

1.三、經過ClassPathXmlApplicationContext類獲取beanide

// 傳統xml方式注入bean並從容器中獲取
    @Test
    public void xmlGetBean(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person);
    }

二、註解方式

  • 編寫bean類
  • 編寫bean配置類,使用註解注入bean
  • 經過AnnotationConfigApplicationContext獲取bean對象

2.一、編寫bean類this

package com.jcon.entity;

/**
 * @author Jcon
 * @version V1.0
 * @Description: (用戶實體類)
 * @date 2019年01月28日
 */
public class Person {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

2.二、編寫bean配置類,使用註解注入beanspa

package com.jcon.entity;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Jcon
 * @version V1.0
 * @Description: (配置類)
 * @date 2019年01月28日
 */
@Configuration
public class Config {

    @Bean("person")
    public Person getPerson(){
        Person person = new Person();
        person.setAge(18);
        person.setName("李四");
        return person;
    }

}

2.三、經過AnnotationConfigApplicationContext獲取bean對象3d

// 註解方式注入bean並從容器中獲取
    @Test
    public void annotationGetBean(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
        Person person = (Person) applicationContext.getBean("person"); // 對應bean名字爲@bean方法的方法名,也能夠使用@bean("name")指定bean名字
        System.out.println(person);
    }

總結code

  • 使用註解類能夠代替xml配置類
  • 註解方式bean名字默認是方法名,能夠使用@bean("beanName")來指定bean名字
相關文章
相關標籤/搜索