詳解Spring配置文件中bean的相關屬性
前言
前面在使用spring容器的時候使用了xml文件進行配置,關于bean的屬性有以下幾種:
屬性名 | 作用 |
---|---|
class | 這個屬性是強制性的,并且指定用來創(chuàng)建 bean 的 bean 類 |
name/id | 這個屬性指定唯一的 bean 標識符 |
scope | 指定由特定的 bean 定義創(chuàng)建的對象的作用域 |
constructor-arg | 用來注入依賴關系 |
properties | 用來注入依賴關系 |
autowiring mode | 用來注入依賴關系 |
lazy-initialization mode | 延遲初始化的 bean 告訴 IoC 容器在它第一次被請求時,而不是在啟動時去創(chuàng)建一個 bean 實例 |
initialization 方法 | 在 bean 的所有必需的屬性被容器設置之后,調用回調方法 |
destruction 方法 | 當包含該 bean 的容器被銷毀時,使用回調方法 |
id/name屬性
id為bean的唯一標識,但在定義bean的時候也可以不需要寫id屬性,可以通過class屬性的值作為key獲取到bean對象:
<bean class="com.cc.service.UserService"></bean>
public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext a = new ClassPathXmlApplicationContext("test.xml"); UserService test = (UserService) a.getBean("com.cc.service.UserService"); test.a(); } }
如果有id,就不能通過這種方式獲取,會報NoSuchBeanDefinitionException的錯誤:
當<baen>標簽內沒有id屬性時,也可以根據name屬性獲取對象:
<bean name="text" class="com.cc.service.UserService"></bean>
public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext a = new ClassPathXmlApplicationContext("test.xml"); UserService test = (UserService) a.getBean("text"); test.a(); } }
如果有多個相同的name值,那么會報BeanDefinitionParsingException的錯,同樣,如果有重復的id也會報相同的錯誤:
<bean name="text" class="com.cc.service.UserService"></bean> <bean name="text" class="com.cc.entity.User"></bean>
scope屬性
可以通過scope來制定bean的作用域,取值如下:
作用域 | 描述 |
---|---|
singleton | 在spring IoC容器僅存在一個Bean實例,Bean以單例的形式存在、默認值 |
prototype | 每次從容器中調用Bean時,都會返回一個新的實例,每次調用getBean時,相當于執(zhí)行new |
request | 每次HTTP請求都會創(chuàng)建一個新的Bean,該作用域僅適用于WebApplicationcontext環(huán)境 |
session | 同一個HTTP Session共享一個Bean,不同Session使用不同的Bean,僅適用于WebApplicationcontext環(huán)境 |
global-session | 一般用于Portlet應用環(huán)境,該作用域僅適用于WebApplicationcontext環(huán)境 |
先將作用域設置為singleton:
<bean id="text" class="com.cc.service.UserService" scope="singleton"></bean>
public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext a = new ClassPathXmlApplicationContext("test.xml"); Object a1 = a.getBean("text"); Object a2 = a.getBean("text"); System.out.println(a1); System.out.println(a2); } }
可以看到a1和a2的地址相同,說明它們?yōu)橥粋€對象。
接下來將作用范圍改成prototype
初始化方法和銷毀方法
同樣,Bean也有關于生命周期的方法,分別是init-method與destroy-method:
<bean id="text" class="com.cc.service.UserService" scope="singleton" init-method="init" destroy-method="destroy"></bean>
public class UserService { public void a(){ System.out.println("........"); } public UserService() { System.out.println("類對象的創(chuàng)建"); } private void init() { System.out.println("初始化方法..."); } private void destroy() { System.out.println("銷毀方法..."); } }
public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext a = new ClassPathXmlApplicationContext("test.xml"); UserService text = (UserService) a.getBean("text"); a.close(); } }
注:銷毀方法只有在scope為singleton有效,多例模式下通常不會調用銷毀方法!
在對象創(chuàng)建完畢之后想做一些操作的方式除了寫init-method方法外,還可以實現InitializingBean接口,重寫afterPropertiesSet()方法。這里就不再贅述。
到此這篇關于詳解Spring配置文件中bean的相關屬性的文章就介紹到這了,更多相關Spring bean屬性內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!