欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring如何使用注解的方式創(chuàng)建bean

 更新時間:2019年11月20日 11:19:51   作者:聞窗  
這篇文章主要介紹了Spring如何使用注解的方式創(chuàng)建bean,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了Spring如何使用注解的方式創(chuàng)建bean,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

第一種使用配置類的方式

1、創(chuàng)建一個bean

package com.springbean;

public class Person {

  private String name;
  private Integer age ;

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

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

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

  public String getName() {
    return name;
  }

  public Integer getAge() {
    return age;
  }

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

2、創(chuàng)建配置類:

import com.springbean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PersonConfig {

  @Bean  //@Bean("myperson") 這是設置bean的名字
  public Person person(){   System.out.println("已經(jīng)創(chuàng)建實例");
return new Person("張三",20); } }

3、測試

import com.spring.config.PersonConfig;
import com.springbean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ApplicationTest {
  public static void main(String[] args) {
    
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
    Person bean = applicationContext.getBean(Person.class);
    System.out.println(bean);

    //獲取bean的類型,默認是方法名,需要修改就在配置類中@Bean里面加上名字

    String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
    for (String beanType : beanNamesForType){
      System.out.println(beanType);
    }
  } 
}

和xml配置文件一樣,默認的bean是單例的,如果需要改變?yōu)閜rototype,xml配置文件里是加上scope="prototype",這里PersonConfig配置類中需要加上注解@Scope("prototype")。

介紹一下bean的幾種類型的作用域。

  • singleton:單實例(默認),ioc容器啟動時就會創(chuàng)建對象放到ioc容器中,以后每次獲取都是直接從ioc容器中獲取,ioc容器可以簡單理解為map
  • prototype:多實例(原型),ioc容器啟動并不會去調(diào)用方法創(chuàng)建對象,而是每次我們獲取對象的時候,才會調(diào)用方法去創(chuàng)建。
  • requst:同一次請求創(chuàng)建一個實例
  • session:同一個session創(chuàng)建一個實例

不加注解測試:

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
    Person bean = applicationContext.getBean(Person.class);
    Person bean2 = applicationContext.getBean(Person.class);
    System.out.println(bean==bean2);//打印結(jié)果為true

加上注解@Scope("prototype")測試:

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
    Person bean = applicationContext.getBean(Person.class);
    Person bean2 = applicationContext.getBean(Person.class);
    System.out.println(bean==bean2);
//打印結(jié)果為fale

我們也可以改變單例時ioc加載的時候就創(chuàng)建實例,只要在我們的PersonConfig配置類中加上@Lazy注解,使用懶加載。測試

public class ApplicationTest {
  public static void main(String[] args) {
 
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
    /* Person bean = applicationContext.getBean(Person.class);
    Person bean2 = applicationContext.getBean(Person.class);
    System.out.println(bean==bean2);*/
    /*
    String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
    for (String beanType : beanNamesForType){
      System.out.println(beanType);
    }*/
  }
}

這是時打印欄將不會打印出“已經(jīng)創(chuàng)建實例”,就實現(xiàn)的單例情況下的懶加載。

第二種使用@import注解的方式

新建一個student類

public class Student {
}

在配置類PersonConfig上使用@Import注解,這里面可以傳入一個數(shù)組,用大括號{}

@Configuration
@Import({Student.class})
public class PersonConfig {

測試:

public class DemoTest {

  ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);

  @Test
  public void test(){

    Student bean = applicationContext.getBean(Student.class);
    System.out.println(bean);
  }
}

打印結(jié)果:com.springbean.Student@2c34f934 ,注入成功

還可以在@Import中加入ImportSelector的實現(xiàn)類來實現(xiàn)bean的注入

創(chuàng)建Parent和Teacher類

public class Parent {
}

public class Teacher {
}

創(chuàng)建ImportSelector的實現(xiàn)類MyImportSelector,返回需要注入的bean,這里是全類名

public class myImportSelector implements ImportSelector{
  @Override
  public String[] selectImports(AnnotationMetadata annotationMetadata) {

    return new String[]{"com.springbean.Parent","com.springbean.Teacher"};
  }
}

修改PersonConfig,這里傳入實現(xiàn)類MyImportSelector

@Configuration
@Import({Student.class, myImportSelector.class})
public class PersonConfig {

測試:

Parent parent = applicationContext.getBean(Parent.class);
    Teacher teacher = applicationContext.getBean(Teacher.class);
    System.out.println(parent);
    System.out.println(teacher);

打印結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 淺析Java中關鍵詞volatile底層的實現(xiàn)原理

    淺析Java中關鍵詞volatile底層的實現(xiàn)原理

    在 Java 并發(fā)編程中,有 3 個最常用的關鍵字:synchronized、ReentrantLock 和 volatile,這篇文章主要來和大家聊聊volatile底層的實現(xiàn)原理,感興趣的可以了解下
    2024-02-02
  • thymeleaf中前后端數(shù)據(jù)交互方法匯總

    thymeleaf中前后端數(shù)據(jù)交互方法匯總

    這篇文章主要介紹了thymeleaf中前后端數(shù)據(jù)交互小結(jié),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2022-07-07
  • Spring Security單項目權限設計過程解析

    Spring Security單項目權限設計過程解析

    這篇文章主要介紹了Spring Security單項目權限設計過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • Java導出txt文件的方法

    Java導出txt文件的方法

    這篇文章主要介紹了Java導出txt文件的方法,實例分析了兩種java導出txt文本文件的使用技巧,需要的朋友可以參考下
    2015-05-05
  • SpringBoot 配置 okhttp3的操作

    SpringBoot 配置 okhttp3的操作

    這篇文章主要介紹了SpringBoot 配置 okhttp3的操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • java實現(xiàn)坦克大戰(zhàn)小游戲

    java實現(xiàn)坦克大戰(zhàn)小游戲

    這篇文章主要為大家詳細介紹了java實現(xiàn)坦克大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • SpringBoot使用 druid 連接池來優(yōu)化分頁語句

    SpringBoot使用 druid 連接池來優(yōu)化分頁語句

    這篇文章主要介紹了SpringBoot使用 druid 連接池來優(yōu)化分頁語句,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • 詳解Spring連接數(shù)據(jù)庫的幾種常用的方式

    詳解Spring連接數(shù)據(jù)庫的幾種常用的方式

    本篇文章主要介紹了Spring連接數(shù)據(jù)庫的幾種常用的方式,具有一定的參考價值,有需要的可以了解一下。
    2016-12-12
  • spring集成redis cluster詳解

    spring集成redis cluster詳解

    這篇文章主要介紹了spring集成redis cluster詳解,分享了maven依賴,Spring配置,增加connect-redis.properties 配置文件等相關內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Java實現(xiàn)雪花算法的示例代碼

    Java實現(xiàn)雪花算法的示例代碼

    SnowFlow算法是Twitter推出的分布式id生成算法,主要核心思想就是利用64bit的long類型的數(shù)字作為全局的id。本文將用Java語言實現(xiàn)雪花算法,感興趣的可以學習一下
    2022-03-03

最新評論