Spring如何使用注解的方式創(chuàng)建bean
這篇文章主要介紹了Spring如何使用注解的方式創(chuàng)建bean,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
第一種使用配置類(lèi)的方式
1、創(chuàng)建一個(gè)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)建配置類(lèi):
import com.springbean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PersonConfig {
@Bean //@Bean("myperson") 這是設(shè)置bean的名字
public Person person(){ System.out.println("已經(jīng)創(chuàng)建實(shí)例");
return new Person("張三",20); } }
3、測(cè)試
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的類(lèi)型,默認(rèn)是方法名,需要修改就在配置類(lèi)中@Bean里面加上名字
String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
for (String beanType : beanNamesForType){
System.out.println(beanType);
}
}
}
和xml配置文件一樣,默認(rèn)的bean是單例的,如果需要改變?yōu)閜rototype,xml配置文件里是加上scope="prototype",這里PersonConfig配置類(lèi)中需要加上注解@Scope("prototype")。
介紹一下bean的幾種類(lèi)型的作用域。
- singleton:?jiǎn)螌?shí)例(默認(rèn)),ioc容器啟動(dòng)時(shí)就會(huì)創(chuàng)建對(duì)象放到ioc容器中,以后每次獲取都是直接從ioc容器中獲取,ioc容器可以簡(jiǎn)單理解為map
- prototype:多實(shí)例(原型),ioc容器啟動(dòng)并不會(huì)去調(diào)用方法創(chuàng)建對(duì)象,而是每次我們獲取對(duì)象的時(shí)候,才會(huì)調(diào)用方法去創(chuàng)建。
- requst:同一次請(qǐng)求創(chuàng)建一個(gè)實(shí)例
- session:同一個(gè)session創(chuàng)建一個(gè)實(shí)例
不加注解測(cè)試:
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")測(cè)試:
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
我們也可以改變單例時(shí)ioc加載的時(shí)候就創(chuàng)建實(shí)例,只要在我們的PersonConfig配置類(lèi)中加上@Lazy注解,使用懶加載。測(cè)試
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);
}*/
}
}
這是時(shí)打印欄將不會(huì)打印出“已經(jīng)創(chuàng)建實(shí)例”,就實(shí)現(xiàn)的單例情況下的懶加載。
第二種使用@import注解的方式
新建一個(gè)student類(lèi)
public class Student {
}
在配置類(lèi)PersonConfig上使用@Import注解,這里面可以傳入一個(gè)數(shù)組,用大括號(hào){}
@Configuration
@Import({Student.class})
public class PersonConfig {
測(cè)試:
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的實(shí)現(xiàn)類(lèi)來(lái)實(shí)現(xiàn)bean的注入
創(chuàng)建Parent和Teacher類(lèi)
public class Parent {
}
public class Teacher {
}
創(chuàng)建ImportSelector的實(shí)現(xiàn)類(lèi)MyImportSelector,返回需要注入的bean,這里是全類(lèi)名
public class myImportSelector implements ImportSelector{
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{"com.springbean.Parent","com.springbean.Teacher"};
}
}
修改PersonConfig,這里傳入實(shí)現(xiàn)類(lèi)MyImportSelector
@Configuration
@Import({Student.class, myImportSelector.class})
public class PersonConfig {
測(cè)試:
Parent parent = applicationContext.getBean(Parent.class);
Teacher teacher = applicationContext.getBean(Teacher.class);
System.out.println(parent);
System.out.println(teacher);
打印結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA如何判斷上傳文件后綴名是否符合規(guī)范MultipartFile
這篇文章主要介紹了JAVA判斷上傳文件后綴名是否符合規(guī)范MultipartFile,文中通過(guò)實(shí)例代碼介紹了java實(shí)現(xiàn)對(duì)上傳文件做安全性檢查,需要的朋友可以參考下2023-11-11
springMVC中基于token防止表單重復(fù)提交方法
本篇文章主要介紹了springMVC中基于token防止表單重復(fù)提交方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
Spring Security實(shí)現(xiàn)5次密碼錯(cuò)誤觸發(fā)賬號(hào)自動(dòng)鎖定功能
在現(xiàn)代互聯(lián)網(wǎng)應(yīng)用中,賬號(hào)安全是重中之重,然而,暴力 破解攻擊依然是最常見(jiàn)的安全威脅之一,攻擊者通過(guò)自動(dòng)化腳本嘗試大量的用戶(hù)名和密碼組合,試圖找到漏洞進(jìn)入系統(tǒng),所以為了解決這一問(wèn)題,賬號(hào)鎖定機(jī)制被廣泛應(yīng)用,本文介紹了Spring Security實(shí)現(xiàn)5次密碼錯(cuò)誤觸發(fā)賬號(hào)鎖定功能2024-12-12
SpringBoot全局異常與數(shù)據(jù)校驗(yàn)的方法
這篇文章主要介紹了SpringBoot全局異常與數(shù)據(jù)校驗(yàn)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
SpringBoot集成slf4j2日志配置的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot集成slf4j2日志配置的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
Java 實(shí)現(xiàn)完整功能的學(xué)生管理系統(tǒng)實(shí)例
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)完整版學(xué)生管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11

