1. 程式人生 > >Spring入門學習——指定Bean引用

Spring入門學習——指定Bean引用

組成應用程式的Bean往往需要互相協作完成應用功能,為了Bean之間的相互訪問,必須在Bean配置檔案中指定Bean引用。在之前的生成序列號的案例場景中,生成字首是指定一個字串來作為字首的,現在增強一下,以系統日期按照某種規則來產生字首。

package com.cgy.springrecipes.sequence;
/**
* 用於定義字首生成操作
*/
public interface PrefixGenerator {
public String getPrefix();
}

package com.cgy.springrecipes.sequence;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* 使用特殊模式格式化當前系統日期以生成字首
*/
public class DatePrefixGenerator implements PrefixGenerator{

private DateFormat formatter;

public void setPattern(String pattern) {
this.formatter = new SimpleDateFormat(pattern);
}

public String getPrefix() {
return formatter.format(new Date());
}

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<property name="prefixGenerator">
<ref bean="datePrefixGenerator"/>
</property>
</bean>

<bean id="datePrefixGenerator"

class="com.cgy.springrecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>

</beans>

此時執行出來的序列為2017033010000A和2017033010001A

<ref>元素的bean屬性名稱可以使對IoC容器中的任何Bean的引用(即使這個Bean不在同一個XML配置檔案中定義)。如果想引用相同XML檔案中的一個Bean,應該使用local屬性。使用local屬性的好處是,xml編輯器將幫助校驗BeanID是否存在於相同的XML中,因為local的值是一個XML ID引用。

簡寫形式

<property name="prefixGenerator" ref="datePrefixGenerator"/>

但是注意:該簡寫相當於使用bean屬性,因此無法利用xml編輯器的驗證。

Spring2.X之後還有一個便利的簡寫來指定Bean引用,利用pschema將bean引用作為<bean>元素的一個屬性。同時注意為了

區分Bean引用與簡單的屬性值,必須在屬性名後加上-ref字尾。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- <bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<property name="prefixGenerator">
<ref local="datePrefixGenerator"/>
</property>
</bean> -->
<bean id="sequenceGenerator"
class="com.cgy.springrecipes.sequence.SequenceGenerator"
p:suffix="A"
p:initial="10000"
p:prefixGenerator-ref="datePrefixGenerator"/>

<bean id="datePrefixGenerator" class="com.cgy.springrecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</beans>

********************************************************************************************************************************************【使用建構函式來注入Bean引用】

package com.cgy.springrecipes.sequence;

public class SequenceGenerator {
private PrefixGenerator prefixGenerator;
private String suffix;
private int initial;
private int counter;

public SequenceGenerator() {}

public SequenceGenerator(PrefixGenerator prefixGenerator) {
this.prefixGenerator = prefixGenerator;
}


......省略

}

<bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<constructor-arg>
<ref local="datePrefixGenerator"/>
</constructor-arg>

</bean>

*****************************************************************************************************************************************【宣告內部Bean】如果Bean例項只用於一個特殊的屬性,可以宣告為內部Bean,內部Bean宣告直接包含在<property>或<constructor-arg>中,不設定任何id或者name屬性,這個Bean將是匿名的,無法在別處使用。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<property name="prefixGenerator">
<bean class="com.cgy.springrecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>

</property>

</bean>

</beans>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<constructor-arg>
<bean class="com.cgy.springrecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>

</constructor-arg>

</bean>

</beans>


相關推薦

Spring入門學習——指定Bean引用

組成應用程式的Bean往往需要互相協作完成應用功能,為了Bean之間的相互訪問,必須在Bean配置檔案中指定Bean引用。在之前的生成序列號的案例場景中,生成字首是指定一個字串來作為字首的,現在增強一下,以系統日期按照某種規則來產生字首。package com.cgy.springrecipes.sequen

Spring入門學習Bean引用) 第二節

Spring學習 第二節 Bean的引用 使用`property`的`ref`屬性建立bean之間的引用關係 另一種方式是建立一個部的Bean使用 為Bean的引用的屬性賦值 小結 Bean的引用

Spring入門學習Bean的生命週期) 第九節

Spring入門學習 第九節 Bean的生命週期 Bean的生命週期 建立需要的類Car.javapackage com.fafa.spring.beans.cycle; public class Car { public Car

Spring入門學習Bean的注入) 第一節

Spring學習 第一節 開始使用 Maven新增Spring依賴 使用XML配置檔案通過屬性注入Bean 通過構造方法來配置Bean的屬性 Bean的引用 小結 開始使用 Maven新增

Spring入門學習Bean的作用域) 第六節

Spring入門學習 第六節 作用域 作用域 使用bean的scope屬性來指定建立的bean的作用域,該屬性值預設是單例的singleton 建立一個car的bean,使用的是第四節中建立的Car類<bean id="ca

Spring入門學習Bean之間的關係) 第五節

Spring入門學習 第五節 Bean之間的關係(繼承,依賴) 繼承 依賴 Bean之間的關係(繼承,依賴) 繼承 繼續使用上節中建立的類。 建立一個Spring配置檔案beans-relati

Spring入門學習(基於註解的方式配置Bean) 第十二節

Spring入門學習(基於註解的方式配置Bean) 指定SpringIOC容器掃描包 分別建立`repository,service,controller`層 指定SpringIOC容器掃描包 分別建立reposi

Spring入門學習(通過FactoryBean配置Bean) 第十一節

Spring入門學習(通過FactoryBean配置Bean) Spring中有兩種型別的Bean Spring中有兩種型別的Bean 普通的Bean和工廠Bean,工廠Bean返回的物件不是指類的一個例項,它返回的是該工廠bean的g

Spring入門學習(工廠方法配置Bean) 第十節

Spring入門學習(工廠方法配置Bean) 配置Bean的形式 Bean的配置方式 靜態工廠方法 例項工廠方法 配置Bean的形式 基於XML檔案的方式 基於註解的方式 Bean的

Spring入門學習——為集合元素指定資料型別

預設情況下,Spring將集合中所有元素作為字串對待,如果不打算將集合元素作為字串使用,就必須為它們指定資料型別。解決方案:(1)使用<value>標記的type屬性指定每個集合元素的資料型別(2)用集合標記的value-type屬性指定所有元素的資料型別實驗場景:在之前的序列號生成基礎上,將字尾

Spring入門學習筆記(1)

eth cast path jpa 組件 註解 lob 調用bean 應用 目錄 Spring好處 依賴註入 面向面編程(AOP) Spring Framework Core Container Web Miscellaneous 編寫第一個程序 IoC容器 Sprin

Spring入門學習筆記(3)——事件處理類

aware super 不能 href his 應用 odi eap app 目錄 Spring中的事件處理 Spring內建事件 監聽Context事件 Example 自定義Spring事件 Spring中的事件處理 ApplicationContext 是Spr

Spring入門學習筆記(4)——JDBC的使用

目錄 Spring JDBC框架概覽 JdbcTemplate類 配置資料來源 資料訪問物件(Data Access Object,DAO) 執行SQL命令 Spring JDBC框架概覽 使用傳統的JDBC連線資料庫,需要編寫不必要的程式碼來處理

spring入門學習筆記第二課--為什麼使用spring以及spring的使用

在我的上篇部落格中,留下了一個伏筆,作為這篇部落格的開篇: 為什麼使用spring? 這裡摘取我在網上看到的內容作為這個問題的答案: Spring的IOC和AOP兩大核心功能可以大大降低應用系統的耦合性、簡化開發流程。 Spring框架技術可在不同層次上起作用,比如IOC

Spring入門學習(使用XML配置檔案方式來配置AOP) 第十七節

Spring入門學習(使用XML配置檔案方式來配置AOP) xml配置檔案配置AOP xml配置檔案配置AOP 使用之前建立的類ArithmeticCalculator和ArithmeticCalculatorImpl 去掉Log

Spring入門學習(AOP返回通知&異常通知&環繞通知&切面的優先順序) 第十六節

Spring入門學習(AOP返回通知&異常通知&環繞通知) 返回通知 異常通知 環繞通知 切面的優先順序 返回通知 使用`@AfterReturning`註解,在方法正常結束後執行的通知,它是可以獲得方法的返回

Spring入門學習(AOP前置通知和後置通知) 第十五節

Spring入門學習(AOP前置通知) AOP前置通知 後置通知 AOP前置通知 前置通知:在方法之前執行的通知,使用`@Before`註解並將切入點表示式的值作為註解值。 使用Maven新增依賴的jar包:<!--

Spring入門學習(AOP) 第十四節

Spring入門學習(AOP) 為什麼需要AOP 一種方法是使用動態代理解決 使用Spring AOP AOP簡介 AOP術語 為什麼需要AOP 新建一個介面Ari

Spring入門學習(泛型依賴注入) 第十三節

Spring入門學習(泛型依賴注入) Spring4泛型依賴注入 Spring4泛型依賴注入 建立泛型類BaseRepository<T>,BaseService<T>,BaseService中注入了BaseRe

Spring入門學習(外部屬性檔案) 第七節

Spring入門學習 第七節 外部屬性檔案 匯入連線資料庫相應的jar包 建立Spring配置檔案 外部屬性檔案 匯入連線資料庫相應的jar包 新增相應的包<!-- https://mvnrep