国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

13、spring的bean基礎(5)

2019-11-08 01:55:32
字體:
來源:轉載
供稿:網友

13、sPRing的bean基礎(5)

在本文中主要介紹以下兩個知識點,在博主做備忘之時,也可以分享給大家一起學習。

spring依賴檢查的兩種實現方式spring bean初始化和銷毀的函數調用

1、spring依賴檢查的兩種實現方式

說明:進行依賴檢查的目的就在于確保bean的屬性被注入一個目標值


在開始演示之前先創建兩個實體類: Customer.java

package com.main.autowrite.required;public class Customer { private Person person; private String state; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public String getState() { return state; } public void setState(String state) { this.state = state; } @Override public String toString() { return "Customer [person=" + person + ", state=" + state + "]"; }}

Person.java

package com.main.autowrite.required;public class Person { private String name; private int type; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getType() { return type; } public void setType(int type) { this.type = type; } @Override public String toString() { return "Person [name=" + name + ", type=" + type + "]"; }}

第一種:基于xml方式實現的依賴檢查(四種檢查形式)

依賴檢查有以下四種檢查規則

none 規則,即不進行任何依賴檢查,bean的屬性可以為nullsimple 規則,對于bean中已經聲明的簡單類型(int,string等等)屬性,必須要注入值objects 規則,對于bean中已經聲明的對象屬性(其他類,Integer等等),必須要為bean注入一個值all 規則,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-2.5.xsd"> <!-- none規則 --> <bean id="CustomerNone" class="com.main.autowrite.required.Customer" dependency-check="all"> <property name="person"> <ref bean="Person"/> </property> <property name="state" value="1"></property> </bean> <!-- simple規則 --> <bean id="CustomerSimple" class="com.main.autowrite.required.Customer" dependency-check="simple"> <property name="person"> <ref bean="Person"/> </property> <property name="state" value="1"></property> </bean> <!-- objects規則 --> <bean id="CustomerObjects" class="com.main.autowrite.required.Customer" dependency-check="objects"> <property name="person"> <ref bean="Person"/> </property> <property name="state" value="1"></property> </bean> <!-- all規則 --> <bean id="CustomerAll" class="com.main.autowrite.required.Customer" dependency-check="all"> <property name="person"> <ref bean="Person"/> </property> <property name="state" value="1"></property> </bean> <bean id="Person" class="com.main.autowrite.required.Person"> <property name="name" value="yiibai" /> <property name="type" value="1" /> </bean></beans>

上述的四種例子中,均為Customer的四個bean的所有屬性注入了值,作為測試,現在把simple規則的state屬性刪除掉,然后運用下面的測試方法查看結果。

@Test public void test(){ applicationContext context = new ClassPathXmlApplicationContext("com/main/autowrite/required/bean.xml"); Customer customer = (Customer)context.getBean("CustomerSimple"); System.out.println(customer.toString()); }

結果如下:提示state屬性要注入值,或者停用simple規則 的dependency checking

Error creating bean with name 'CustomerSimple' defined in class path resource [com/main/autowrite/required/bean.xml]: Unsatisfied dependency expressed through bean property 'state': Set this property value or disable dependency checking for this bean.

第二種:基于注解的依賴檢查

說明:基于注解的依賴檢查實現方式為:在實體類的屬性對應的setter方法中加入@Required注解 例如:

@Required public void setState(String state) { this.state = state; }

但是簡單做完上述步驟是不夠的,還需要在bean配置文件中注冊一個RequiredAnnotationBeanPostProcessor。 注冊的方式有以下兩種: 第一種:添加context:annotation-config

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <!-- 基于注解的依賴檢查 --> <bean id="CustomerRequired" class="com.main.autowrite.required.Customer"> <property name="person"> <ref bean="Person"/> </property> <property name="state" value="1"></property> </bean> <bean id="Person" class="com.main.autowrite.required.Person"> <property name="name" value="yiibai" /> <property name="type" value="1" /> </bean></beans>

第二種:包含 RequiredAnnotationBeanPostProcessor 在beans標簽里添加:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

以上工作做完就可以使用注解形式的依賴檢查了。

自定義依賴檢查的注解:@Required-style

第一步:創建@MyRequired接口

package com.main;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface MyRequired{}

第二步:注冊自定義注解關鍵字@MyRequired

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"> <property name="requiredAnnotationType" value="com.main.MyRequired"/></bean>

第三步:應用到bean屬性的setter方法

@MyRequired public void setState(String state) { this.state = state; }

這樣就完成了自定義依賴檢查的注解,當然還可以使用這種方式定義其他類型的注解。


spring bean初始化和銷毀的函數調用

第一種方式:

說明:直接實現InitializingBean和DisposableBean的兩個標記接口,并且重寫響應的方法,然后在bean配置文件中聲明bean,進一步提取(初始化)bean,最后釋放spring容器。以上就是大概思路,下面我們來一步步分析。 第一步: 創建實體類和聲明bean:

package com.main.bean.initAndDispose;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;public class InitAndDisposeDemo implements InitializingBean,DisposableBean{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "InitAndDisposeDemo [name=" + name + "]"; } /** * destroy methods */ public void destroy() throws Exception { System.out.println("我是銷毀函數,當spring容器被銷毀時,或者bean被回收資源時被調用。"); } /** * Initializing methods */ public void afterPropertiesSet() throws Exception { System.out.println("我是初始化函數,當bean被裝進spring容器時調用"); }}<bean id="InitAndDisposeDemo" class="com.main.bean.initAndDispose.InitAndDisposeDemo"> <property name="name" value="大家好,我是InitAndDisposeDemo"/> </bean>

第二步:測試:

@Test public void test(){ ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("com/main/bean/initAndDispose/bean.xml"); InitAndDisposeDemo initAndDisposeDemo = (InitAndDisposeDemo)context.getBean("InitAndDisposeDemo"); System.out.println(initAndDisposeDemo.toString()); context.close(); }

運行結果如下: 這里寫圖片描述

但是上述方法被不被推薦使用,因為這使得你的項目將會和spring框架耦合度變的更緊密,這是一個不好的處理方式。

第二種方式:

在bean配置文件中使用init-method和destroy-method 第一步,修改你的InitAndDisposeDemo類如下(直接刪除實現的兩個標標記接口):

package com.main.bean.initAndDispose;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;public class InitAndDisposeDemo{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "InitAndDisposeDemo [name=" + name + "]"; } public void destroy() throws Exception { System.out.println("destroy:我是銷毀函數,當spring容器被銷毀時,或者bean被回收資源時被調用。"); } public void init() throws Exception { System.out.println("afterPropertiesSet:我是初始化函數,當bean被裝進spring容器時調用"); }}

第二步:在bean配置文件中聲明init-method和destroy-method

<bean id="InitAndDisposeDemo" class="com.main.bean.initAndDispose.InitAndDisposeDemo" init-method="init" destroy-method="destroy"> <property name="name" value="大家好,我是InitAndDisposeDemo"/> </bean>

測試代碼和運行結果:依然和第一種的一樣;切記要把方法名寫對,不然會拋出方法找不到的異常。

第三種方式:

第一步:修改你的InitAndDisposeDemo如下:

@PreDestroy public void destroy() throws Exception { System.out.println("destroy:我是銷毀函數,當spring容器被銷毀時,或者bean被回收資源時被調用。"); } @PostConstruct public void init() throws Exception { System.out.println("afterPropertiesSet:我是初始化函數,當bean被裝進spring容器時調用"); }

第二步: 在bean配置文件中注冊CommonAnnotationBeanPostProcessor,有兩種方法: 1:在bean配置文件中添加如下語句

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

2:在bean配置文件中添加如下語句

<context:annotation-config />

第三步:執行 說明:執行方法和結果均和上面的例子一樣


文檔結束,謝謝


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鹤山市| 陆河县| 土默特右旗| 富民县| 胶州市| 蒲江县| 会同县| 紫云| 定安县| 隆昌县| 龙口市| 南和县| 车致| 当阳市| 个旧市| 黔东| 高清| 上蔡县| 科尔| 凉山| 鹤岗市| 渭源县| 新龙县| 永城市| 祁门县| 阜平县| 新乡县| 左云县| 安国市| 陆丰市| 思茅市| 东宁县| 新邵县| 文山县| 尼木县| 罗甸县| 运城市| 肥城市| 东阳市| 岑巩县| 三明市|