Spring深入了解常用配置應用
常用配置
現(xiàn)在這里簡單了解一下spring 配置文件中的一些常用配置,在后面我們還會遇到更多的配置,在后文繼續(xù)進行介紹了。
spring中的配置一共也就這幾個
- description描述不太重要,
- bean在之前已經(jīng)見識過了,
- alias給bean起別名,
- import在當前xml文件中導入其他xml文件
一、別名
在spring中別名主要是給bean的id起一個別名,同樣也有好幾種方式。
1、alias 配置
<alias name="user" alias="u"/>
alias是給bean的id起別名
- name 是bean的id
- alias 是bean的別名
(1)先定義普通實體類
package com.kuang.pojo; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Getter @Setter @ToString public class User { private int id; private String userName; private String password; }
(2)在配置文件中裝配bean,并定義bean的別名
<?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"> <alias name="user" alias="u"/> <bean id="user" class="com.kuang.pojo.User"> <property name="id" value="1"/> <property name="userName" value="root"/> <property name="password" value="123456"/> </bean> </beans>
(3)通過別名也能拿到裝配的bean
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); User user = context.getBean("u",User.class); System.out.println(user); }
(4)查看運行結果
二、bean 的配置
也可以通過bean來配置別名,而且可以給一個bean 配置多個別名
<bean id="user" class="com.kuang.pojo.User" name="u1,u2,u3,u4"> <property name="id" value="1"/> <property name="userName" value="root"/> <property name="password" value="123456"/> </bean>
name就是給當前bean配置別名,可以多個別名寫在一起,中間使用空格/逗號/分號進行分割,spring都能識別
三、import
在團隊開發(fā)使用中,還是非常常見的。它可以將多個配置文件,導入合成一個
假設一個團隊中有多個人進行開發(fā),這三個人負責不同類的開發(fā),不同的類需要注冊到不同的bean中
- 張三 beans1.xml
- 李四 beans2.xml
- 王五 beans3.xml
我們可以利用import 將所有人的beans.xml合并成一個總的ApplicationContext.xml ,最后使用的時候使用總的配置文件即可。
張三負責 User類 以及注冊到bean1.xml文件中
User類
package com.kuang.pojo; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Getter @Setter @ToString public class User { private int id; private String userName; private String password; }
bean1.xml
<?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="user" class="com.kuang.pojo.User"> <property name="id" value="1"/> <property name="userName" value="root"/> <property name="password" value="123456"/> </bean> </beans>
李四負責 Student類,bean2.xml
Stduent 類
package com.kuang.pojo; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Setter @Getter @ToString public class Student { private int id; private String name; private String sex; }
bean2.xml
<?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="student" class="com.kuang.pojo.Student"> <property name="id" value="1"/> <property name="name" value="張三"/> <property name="sex" value="男"/> </bean> </beans>
總的ApplicationContext.xml配置文件,導入了bean1.xml 和 bean2.xml
<?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"> <import resource="bean1.xml"/> <import resource="bean2.xml"/> </beans>
使用的時候,使用總的配置文件即可
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); User user = context.getBean("user",User.class); Student student = context.getBean("student",Student.class); System.out.println(user); System.out.println(student); }
存在問題
同時使用import還存在幾個問題 導入bean 的id沖突
如果導入的文件中有多個重名id相同的bean
如果總配置文件中有取這個bean
如果在導入的xml文件中,因為導入的時候id相同的bean會不斷覆蓋,同名的bean后面的xml會覆蓋前面的 xml,所以最后取的是最后導入這個id的xml文件中的bean
總結
與主配置中的id重名,調(diào)用主配置中的id;
多個import中配置中的id重名,調(diào)用最后import中配置中的id重名,即后面的覆蓋前面的;
到此這篇關于Spring深入了解常用配置應用的文章就介紹到這了,更多相關Spring常用配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實現(xiàn)word/pdf轉html并在線預覽
這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)word、pdf文件轉html并在線預覽的功能,文中的示例代碼講解詳細,需要的可以參考一下2023-05-05