深圳全网站建设公司,哪个网站可以免费做音乐相册,网站开发合同里的坑,视频交易类网页Spring框架有几个提供一系列服务的模块#xff0c;其中许多模块仅可用于托管对象#xff08;Spring Bean#xff09;。有关这些服务的一些示例是依赖注入#xff0c;事务管理#xff0c;AOP服务等。当我们使用时#xff0c;一切都很好对象即服务#xff0c;因此由Spring… Spring框架有几个提供一系列服务的模块其中许多模块仅可用于托管对象Spring Bean。有关这些服务的一些示例是依赖注入事务管理AOP服务等。当我们使用时一切都很好对象即服务因此由Spring在特定范围内进行管理。 但是有时候我们需要我们的领域对象拥有这些服务。 通常域对象是使用new关键字创建的因此默认情况下无法使用spring对其进行管理。 在我的上一篇文章 如何在Spring 3.x中使用事件 中我们有一个名称为Order的域对象。 对于对象之间的解耦我们使用了事件。 但是只有受管bean才能在Spring框架中引发事件可能是您知道并具有此功能的每个框架。 Spring引入了一个名为Configurable的注释。 在我们的域对象上使用此注释使它们由spring管理。 但是它是如何工作的出于其目的可配置需要AspectJ Compiler您的类需要在编译时或加载时提高字节码直到可以满足您的要求。 我想带给您一个有关如何在应用程序中配置和使用可配置电源的简单示例。 最好让环境对象使所有系统都可以访问其属性以捕获有关系统的信息。 例如我们需要知道系统的当前时间简单的解决方案是使用Calendar.getInstance().getTime()或new Date() 但是有一些缺陷您的代码对于需要测试日期断言的部分将无法进行测试我将尽快编写一系列后测试和可测试代码。 另一个问题是当您希望系统使用伪时钟时。 例如您的客户希望在假期最后一个非假期日期使用系统。 因此为这些需求提供一种机制很有价值。 作为此示例中的一个简单解决方案我将创建一个环境接口该接口具有一个方法getCurrentTime。 如果我需要系统时间则代码中的每个地方都将使用此方法。 在我可以愉快地使用此方法之前必须将环境接口注入到我的对象中。 Spring bean对使用Environment没有任何问题但是在我们的域对象中我们必须使用Configurable Annotation。 如果使用Maven则需要将以下依赖项添加到pom中 groupIdorg.springframework/groupId
artifactIdspring-aspects/artifactId
version3.1.1.RELEASE/versiondependency
groupIdorg.aspectj/groupId
artifactIdaspectjrt/artifactId
version1.6.8/version
/dependency
dependency
groupIdorg.aspectj/groupId
artifactIdaspectjweaver/artifactId
version1.6.8/version
/dependency
dependency
groupIdorg.aspectj/groupId
artifactIdaspectjtools/artifactId
version1.6.8/version
/dependency 要使用maven编译应用程序可以使用以下Aspectj-maven-plugin配置 buildpluginsplugingroupIdorg.codehaus.mojo/groupIdartifactIdaspectj-maven-plugin/artifactIdversion1.4/versionconfigurationshowWeaveInfotrue/showWeaveInfosource1.6/sourcetarget1.6/targetXlintignore/XlintcomplianceLevel1.6/complianceLevelencodingUTF-8/encodingverbosefalse/verboseaspectLibrariesaspectLibrarygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactId/aspectLibrary/aspectLibraries/configurationexecutionsexecutiongoalsgoalcompile/goalgoaltest-compile/goal/goals/execution/executionsdependenciesdependencygroupIdorg.aspectj/groupIdartifactIdaspectjrt/artifactIdversion1.6.8/version/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjtools/artifactIdversion1.6.11/version/dependency/dependencies/plugin/plugins/build 如果使用IDE编译代码请不要忘记将其编译器更改为AspectJ。 您可以在AspectJrt目录中的本地Maven存储库中找到它。 假设有一个Product类我们想知道其创建日期以及销售日期。 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;}} 产品是一个非常简单的类我们使用preConstruction true是因为我们的产品构造需要使用环境。 环境及其实现也非常简单 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是在测试包中创建的因为我们仅在测试中需要此类。 除了使用此类之外您还可以使用一些模拟库作为Mocktio及其扩展Springockito。 但是在此示例中我们的重点不在它们上。 我们的测试也非常简单 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());}} 您可以从以下网址下载源代码 https : //github.com/psycho-ir/Spring-Configurable.git 参考 Just另一个Java博客博客中的JCG合作伙伴 Soroosh Sarabadani的Spring可配置魔术 。 翻译自: https://www.javacodegeeks.com/2013/09/spring-configurable-magic.html