Java?Spring框架創(chuàng)建項(xiàng)目與Bean的存儲(chǔ)與讀取詳解
本文思維導(dǎo)圖:

1.Spring項(xiàng)目的創(chuàng)建
1.1創(chuàng)建Maven項(xiàng)目
第一步,創(chuàng)建Maven項(xiàng)目,Spring也是基于Maven的。


1.2添加spring依賴
第二步,在Maven項(xiàng)目中添加Spring的支持(spring-context, spring-beans)
在pom.xml文件添加依賴項(xiàng)。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
刷新等待加載完成。

1.3創(chuàng)建啟動(dòng)類(lèi)
第三步,創(chuàng)建啟動(dòng)類(lèi)與main,用來(lái)做簡(jiǎn)單的測(cè)試
在java目錄創(chuàng)建類(lèi),寫(xiě)代碼即可,因?yàn)檫@里只演示怎么創(chuàng)建Spring項(xiàng)目和介紹Spring的簡(jiǎn)單使用,就不依賴那些Tomcat什么的了,直接寫(xiě)一個(gè)Main類(lèi)更直觀。

1.4配置國(guó)內(nèi)源
由于國(guó)外源不穩(wěn)定,可能第二步引入spring依賴會(huì)失敗,所以下面介紹如何配置國(guó)內(nèi)鏡像源。


現(xiàn)成的settings.xml文件鏈接:
地址2:提取碼: 9thv
如果你已經(jīng)有了settings文件,但沒(méi)有配置mirror,配置內(nèi)容如下:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
2.儲(chǔ)存或讀取Bean對(duì)象
2.1添加spring配置文件
添加spring配置文件(首次才需要,非首次可忽略此步驟)
右鍵resources目錄,新建一個(gè).xml配置文件,文件名推薦spring.xml或者spring-config.xml。

創(chuàng)建一個(gè)spring.xml配置文件,配置內(nèi)容:
<?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">
</beans>2.2創(chuàng)建Bean對(duì)象
第一步,創(chuàng)建Bean對(duì)象。
比如我們要注入一個(gè)User對(duì)象,就先的創(chuàng)建一個(gè)User類(lèi)。
package com.bean;
public class User {
public void sayHi(String name) {
System.out.println("你好!" + name);
}
}將Bean通過(guò)配置文件,注入到spring中,即在spring配置文件中通過(guò)以下語(yǔ)句注入。
<bean id="user" class="com.bean.User"></bean>
spring中對(duì)象的儲(chǔ)存是通過(guò)鍵值對(duì)來(lái)存儲(chǔ)的,其中key為id,value為class。
命名規(guī)范:id使用小駝峰命名,如userid,class使用大駝峰命名,如userId。
2.3讀取Bean對(duì)象
想要從spring中將Bean對(duì)象讀取出來(lái),先要得到spring上下文對(duì)象,相當(dāng)于得到了spring。再通過(guò)spring上下文對(duì)象提供的方法獲取需要使用的Bean對(duì)象。最后就能使用Bean對(duì)象了。
import com.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1.得到上下文對(duì)象
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//2.獲取bean對(duì)象,此處是根據(jù)id獲取
User user = (User) context.getBean("user");
//3.使用bean
user.sayHi("zhangsan");
}
}運(yùn)行結(jié)果:
你好!zhangsan
Process finished with exit code 0
還可以使用Bean工廠(舊)來(lái)獲取Bean。
import com.bean.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main2 {
public static void main(String[] args) {
//1.得到Bean工廠
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring.xml"));
//2.獲取Bean
User user = (User) factory.getBean("user");
//3.使用
user.sayHi("李四");
}
}
雖然Bean工廠XmlBeanFactory類(lèi)現(xiàn)在已經(jīng)廢棄了,但是目還能使用的,當(dāng)然創(chuàng)建Bean工廠有新的方式,但老的方式比較直觀,因此演示采用老的方式創(chuàng)建。
運(yùn)行結(jié)果:
你好!李四
Process finished with exit code 0
發(fā)現(xiàn)ApplicationContext與BeanFactory都可以從容器中獲取Bean,都提供了getBean方法,那問(wèn)題來(lái)了,ApplicationContext與BeanFactory有什么區(qū)別?
相同點(diǎn):都可以從容器中獲取Bean,都提供了getBean方法。
不同點(diǎn):
BeanFactory是ApplicationContext的父類(lèi),BeanFactory只提供了基礎(chǔ)訪問(wèn)Bean對(duì)象的功能,而ApplicationContext除了擁有BeanFactory的全部功能,還有其他額外功能的實(shí)現(xiàn),如國(guó)際化,資源訪問(wèn)等功能實(shí)現(xiàn)。- 從性能方面來(lái)說(shuō)是不同的,
BeanFactory按需加載Bean,屬于懶漢方式,ApplicationContext是餓漢方式,在創(chuàng)建時(shí)會(huì)將所有的Bean都加載,以備使用。
證明:
我們?cè)赽ean目錄下添加一個(gè)Blog類(lèi),并完善Blog與User類(lèi)的構(gòu)造方法,當(dāng)類(lèi)被構(gòu)造時(shí)會(huì)發(fā)出一些信號(hào),在獲取上下文或工廠時(shí)根據(jù)這些信號(hào)讓我們感知到它是否會(huì)被構(gòu)造。

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main3 {
public static void main(String[] args) {
//1.得到上下文對(duì)象
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
}
}運(yùn)行結(jié)果:

ApplicationContext創(chuàng)建時(shí),會(huì)將所有的對(duì)象都構(gòu)造,餓漢的方式。
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main4 {
public static void main(String[] args) {
//1.得到Bean工廠
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring.xml"));
}
}
BeanFactory創(chuàng)建時(shí),什么都沒(méi)有,說(shuō)明是懶漢的方式。
ApplicationContext中的多種getBean方法:
方法1:根據(jù) bean name獲取bean。
User user = (User) context.getBean("user");方法2:根據(jù)bean type獲取bean。
User user = (User) context.getBean(User.class);
只有beans中只有一個(gè)類(lèi)的實(shí)例沒(méi)有問(wèn)題,但是個(gè)有多個(gè)同類(lèi)的實(shí)例,會(huì)有問(wèn)題,即在spring中注入多個(gè)同一個(gè)類(lèi)的對(duì)象,就會(huì)報(bào)錯(cuò)。
我們來(lái)試一試,首先在Spring配置文件,注入多個(gè)User對(duì)象:

然后我們?cè)偻ㄟ^(guò)這種方式來(lái)獲取對(duì)象,我們發(fā)現(xiàn)報(bào)錯(cuò)了,報(bào)錯(cuò)信息如下:
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.bean.User' available: expected single matching bean but found 3: user,user1,user2
拋出了一個(gè)NoUniqueBeanDefinitionException異常,表示注入的對(duì)象不是唯一的。
方法3:綜合上述兩種,可以根據(jù)bean name與bean type來(lái)獲取bean
相比方法1,更加健壯。
User user = context.getBean("user", User.class);小結(jié):

到此這篇關(guān)于Java Spring框架創(chuàng)建項(xiàng)目與Bean的存儲(chǔ)與讀取詳解的文章就介紹到這了,更多相關(guān)Java Spring框架內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot如何對(duì)LocalDateTime進(jìn)行格式化并解析
這篇文章主要介紹了SpringBoot如何對(duì)LocalDateTime進(jìn)行格式化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Java中invokedynamic字節(jié)碼指令問(wèn)題
這篇文章主要介紹了Java中invokedynamic字節(jié)碼指令問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
Spring @value和@PropertySource注解使用方法解析
這篇文章主要介紹了Spring @value和@PropertySource注解使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Springboot整合Druid實(shí)現(xiàn)對(duì)訪問(wèn)的監(jiān)控方式
這篇文章主要介紹了Springboot整合Druid實(shí)現(xiàn)對(duì)訪問(wèn)的監(jiān)控方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
java實(shí)現(xiàn)的密碼強(qiáng)度檢測(cè)功能完整示例
這篇文章主要介紹了java實(shí)現(xiàn)的密碼強(qiáng)度檢測(cè)功能,結(jié)合完整實(shí)例形式分析了java針對(duì)密碼強(qiáng)度檢測(cè)相關(guān)的字符串遍歷、判斷,以及輸出密碼強(qiáng)度等級(jí)相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
基于SpringBoot和Vue3的博客平臺(tái)文章列表與分頁(yè)功能實(shí)現(xiàn)
在前面的教程中,我們已經(jīng)實(shí)現(xiàn)了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能。本教程將繼續(xù)引導(dǎo)您實(shí)現(xiàn)博客平臺(tái)的文章列表與分頁(yè)功能,需要的朋友可以參考閱讀2023-04-04

