Spring註解開發--@Bean註解介紹

@Bean註解的使用:

在以往我們利用springIOC容器獲取對象,都是使用配置文件,通過<bean>標籤模式來獲取的。先看一下以往的模式:

一、以往配置文件的方式:

1.在pom.xml中將spring的依賴導入(mvn的依賴庫:https://mvnrepository.com/)
找到 spring context 這個依賴包,我用的是4.3.18版本

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

2.創建一個實體類Person (set/get/toString/構造)

package cn.dxyzz.bean;

/**
 * @author Mr.dong
 * @creat 2019-01-28 10:04
 * Person實體類
 */
public class Person {
    private String name;//姓名
    private Integer age;//年齡

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

    public Person(){

    }

    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{" +
                "mame='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.在src/main./resources/目錄下創建applicationContext.xml配置文件,在配置文件中配置bean

<!--id:自定義的名稱,用於獲取對象-->
<!-- class:需要Spring來創建哪個類的對象(寫完整的包名) -->
<!--name:指定到某一個屬性,value:給屬性賦的值-->
<bean id="person" class="cn.dxyzz.bean.Person">
	<property name="age" value="18"></property>
	<property name="name" value="zhangsan"></property>
</bean>	
4.寫一個測試類cn.dxyzz.test.Test:
package cn.dxyzz.test;

import cn.dxyzz.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @author Mr.dong
 * @creat 2019-01-28 10:06
 * 測試
 */
public class Test {
    public static void main(String[] args) {
        //加載配置文件,獲取Spring容器  參數:指定到配置文件的名稱
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 從Spring容器中獲取對象,getBean()方法的參數是配置文件中的bean的 id
        Person person = (Person) applicationContext.getBean("person");
        //輸出
        System.out.println(person);
    }
}

4.輸出的結果:(已獲取到對象)
在這裏插入圖片描述

二、用註解@Bean的方式來獲取對象

1.導入依賴(跟上面的依賴是一樣的)

2.新建一個配置類:(cn.dxyzz.config.Config)

package cn.dxyzz.cn.dxyzz.config;

import cn.dxyzz.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @author Mr.dong
 * @creat 2019-01-28 10:44
 * 配置類
 */
@Configuration //@Configuration註解:告訴spring容器這是一個配置類
public class Config {
    @Bean //@Bean註解:給容器註冊一個Bean,類型爲返回值的類,id默認是用方法名作爲id
    public Person person(){
        return new Person("zhangsan",18);//給name屬性,age屬性賦值
    }
}

3.再新建一個測試類cn.dxyzz.test.ConfigTest:

import cn.dxyzz.bean.Person;
import cn.dxyzz.cn.dxyzz.config.Config;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 * @author Mr.dong
 * @creat 2019-01-28 10:49
 * 測試類ConfigTest
 */
public class ConfigTest {
    public static void main(String[] args) {
        //獲取spring容器,加載配置類
        AnnotationConfigApplicationContext aa = new AnnotationConfigApplicationContext(Config.class);
        //通過類名獲取對象
        Person person = aa.getBean(Person.class);
        //Person person = aa.getBean("person");這是通過id獲取對象
        //result是一樣的
        //當然裏面還有很多方法,這裏就不一一介紹啦,你可以自己研究研究
        //輸出
        System.out.println(person);
    }
}

4.輸出結果:
在這裏插入圖片描述

5.當然@Bean是可以自定義配置id的名稱:

//只需要 @Bean(value = "這裏寫自定義名稱")
//這樣就可以將這個bean的id值,換成自定義的啦。
/*將Config類變成這樣*/
@Configuration //@Configuration註解:告訴spring容器這是一個配置類
public class Config {
    @Bean(value = "aa") //@Bean註解:給容器註冊一個Bean,類型爲返回值的類,id默認是用方法名作爲id
    public Person person(){
        return new Person("zhangsan",18);//給兩個屬性賦值
    }
}
-----------------------------------------------------------
//測試
public class ConfigTest {
    public static void main(String[] args) {
        //獲取spring容器,加載配置類
        AnnotationConfigApplicationContext aa = new AnnotationConfigApplicationContext(Config.class);
        //通過類名獲取對象
        Object person = aa.getBean("aa");
        System.out.println(person);
    }
}
//結果是一樣的!
//Person{mame='zhangsan', age=18}

好了,@Bean註解介紹就到這裏,使用起來是不是比配置文件簡單的多!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章