ug弹簧可变性装配_弹簧可配置魔术
ug彈簧可變性裝配
Spring框架具有幾個(gè)提供一系列服務(wù)的模塊,其中許多模塊僅可用于托管對象(Spring Bean)。有關(guān)這些服務(wù)的一些示例是依賴注入,事務(wù)管理,AOP服務(wù)等。當(dāng)我們使用時(shí),一切都很好對象即服務(wù),因此由Spring在特定范圍內(nèi)進(jìn)行管理。 但是有時(shí)候我們需要我們的領(lǐng)域?qū)ο髶碛羞@些服務(wù)。 通常,域?qū)ο笫鞘褂胣ew關(guān)鍵字創(chuàng)建的,因此默認(rèn)情況下無法使用spring對其進(jìn)行管理。
在我的上一篇文章( 如何在Spring 3.x中使用事件 )中,我們有一個(gè)名稱為Order的域?qū)ο蟆?對于對象之間的解耦,我們使用了事件。 但是,只有受管bean才能在Spring框架中引發(fā)事件(可能是您知道并具有此功能的每個(gè)框架)。
Spring引入了一個(gè)名為Configurable的注釋。 在我們的域?qū)ο笊鲜褂么俗⑨屖顾鼈冇蓅pring管理。
但是它是如何工作的:出于其目的可配置,需要AspectJ編譯器,您的類需要在編譯時(shí)或加載時(shí)提高字節(jié)碼,直到可以滿足您的要求。
我想帶給您一個(gè)有關(guān)如何在應(yīng)用程序中配置和使用可配置電源的簡單示例。 最好讓環(huán)境對象使所有系統(tǒng)都可以訪問其屬性以捕獲有關(guān)系統(tǒng)的信息。 例如,我們需要知道系統(tǒng)的當(dāng)前時(shí)間,簡單的解決方案是使用Calendar.getInstance().getTime()或new Date()
但是有一些缺陷,您的代碼將無法測試需要測試日期斷言的部分(我將盡快編寫一系列的post bout Test和Testable代碼)。
另一個(gè)問題是當(dāng)您希望系統(tǒng)使用偽時(shí)鐘時(shí)。 例如,您的客戶希望在假期(最后一個(gè)非假期日期)使用系統(tǒng)。
因此,擁有一種滿足這些要求的機(jī)制非常有價(jià)值。 作為此示例中的簡單解決方案,我將創(chuàng)建一個(gè)環(huán)境接口,該接口具有一個(gè)方法(getCurrentTime())。 如果我需要系統(tǒng)時(shí)間,則代碼中的每個(gè)地方都將使用此方法。 在我可以愉快地使用此方法之前,必須將環(huán)境接口注入到我的對象中。 Spring bean對使用Environment沒有任何問題,但是在我們的域?qū)ο笾?#xff0c;我們必須使用Configurable Annotation。
如果您使用Maven,則需要將以下依賴項(xiàng)添加到pom中:
<groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>3.1.1.RELEASE</version><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>1.6.8</version> </dependency>要使用maven編譯應(yīng)用程序,可以使用以下Aspectj-maven-plugin配置:
<build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><version>1.4</version><configuration><showWeaveInfo>true</showWeaveInfo><source>1.6</source><target>1.6</target><Xlint>ignore</Xlint><complianceLevel>1.6</complianceLevel><encoding>UTF-8</encoding><verbose>false</verbose><aspectLibraries><aspectLibrary><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></aspectLibrary></aspectLibraries></configuration><executions><execution><goals><goal>compile</goal><goal>test-compile</goal></goals></execution></executions><dependencies><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.6.8</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjtools</artifactId><version>1.6.11</version></dependency></dependencies></plugin></plugins></build>如果使用IDE編譯代碼,請不要忘記將其編譯器更改為AspectJ。 您可以在AspectJrt目錄中的本地Maven存儲庫中找到它。
假設(shè)有一個(gè)Product類,我們想知道其創(chuàng)建日期以及銷售日期。
@Configurable(preConstruction = true)public class Product {private final String name;private final String description;private final Date createDate;private Status status;private Date saleDate;@Autowiredprivate Environment environment;public Product(String name, String description) {this.name = name;this.description = description;this.status = Status.PENDING;this.createDate = environment.getCurrentDate();}public void sell() {this.status = Status.SALE;this.saleDate = environment.getCurrentDate();}public Date getCreateDate() {return createDate;}public Date getSaleDate() {return saleDate;}public static enum Status {PENDING, SALE;}}產(chǎn)品是一個(gè)非常簡單的類,我們使用preConstruction = true是因?yàn)槲覀兊漠a(chǎn)品構(gòu)造需要使用環(huán)境。
環(huán)境及其實(shí)現(xiàn)也非常簡單:
public interface Environment {Date getCurrentDate();}public class DefaultEnvironment implements Environment {@Overridepublic Date getCurrentDate() {return new Date();}}public class MockEnvironment implements Environment {private Date date;@Overridepublic Date getCurrentDate() {return this.date;}public void setCurrentDate(Date date){this.date = date;}}MockEnvironment是在測試包中創(chuàng)建的,因?yàn)槲覀儍H在測試中需要此類。 除了使用此類之外,您還可以使用一些模擬庫作為Mocktio及其擴(kuò)展(Springockito)。 但是在此示例中,我們的重點(diǎn)不在這些上。
我們的測試也非常簡單:
@ContextConfiguration({"classpath*:context.xml","classpath*:test-context.xml"})public class ProductTest extends AbstractJUnit4SpringContextTests {final Date time = Calendar.getInstance().getTime();@AutowiredEnvironment environment;@Beforepublic void before() {((MockEnvironment) this.environment).setCurrentDate(time);}@Testpublic void created_product_should_have_current_environment_date() {final Product product = new Product("", "");Assert.assertEquals(time, product.getCreateDate());}@Testpublic void sell_should_set_createDate_to_now(){final Product product = new Product("", "");product.sell();Assert.assertEquals(time, product.getSaleDate());}} 您可以從以下網(wǎng)址下載源代碼: https : //github.com/psycho-ir/Spring-Configurable.git
翻譯自: https://www.javacodegeeks.com/2013/09/spring-configurable-magic.html
ug彈簧可變性裝配
總結(jié)
以上是生活随笔為你收集整理的ug弹簧可变性装配_弹簧可配置魔术的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英雄安卓版(英雄牌安卓)
- 下一篇: 詹金斯搭建_詹金斯的Maven报告