@Configurable

引用原文


内容如下
Not, all types(beans) used in a Spring Based Application are Spring managed. Sometimes, we need to inject some dependency into some Types which are not Spring Managed. The most common example being, injection of Service or DAO layer within the Entity class. Now the instances of Entity classes are being created using the new operator, i.e. they are not Spring Managed. Now we want to inject the corresponding Service or DAO Type which is Spring Managed within these Entity Types, and these can be performed with the help of @Configurable. It is the Aspects, which performs all the underlying engineering and ,makes this thing possible. Let’s see an example.

不是基于Spring的应用程序中使用的所有类型(bean)都是Spring管理的。有时,我们需要将一些依赖注入到一些非Spring管理的类型中。最常见的例子是在实体类中注入服务或DAO层。现在实体类的实例使用new创建,也即它们不是被Spring管理的。现在我们想要在这个类型中注入对应的Service或DAO类型,被注入类型是被Spring管理的,以上这些操作可以在 @Configurable 的帮助下完成。它是执行所有基础工程,并使之成为可能的因素之一。让我们看一个例子。


@Configurable
public class Person {@AutowiredResourceBundleThemeSource themeSource;private String firstName;private String lastName;private int age;private Address address;@CustomDateFormatDate birthDate;MultipartFile profile;public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public Date getBirthDate() {return birthDate;}public void setBirthDate(Date birthDate) {this.birthDate = birthDate;}public MultipartFile getProfile() {return profile;}public void setProfile(MultipartFile profile) {this.profile = profile;}@Overridepublic String toString() {return "Person [ThemeSource=" + themeSource.getTheme("default")+ ", firstName=" + firstName + ", lastName=" + lastName+ ", age=" + age + ", address=" + address + ", birthDate="+ birthDate + ", profile=" + profile + "]";}
}

Here, Person can be considered as a Model or Entity class, and it has got a bean of type ResourceBundleThemeSource injected into it. This bean is Spring Managed and the Person class is annotated with @Configurable. I have defined the themeSource in a java based configuration, instead of XML based as:

在这里,Person可以被看作是一个模型或实体类,它有一个类型为ResourceBundleThemeSource的bean注入其中。这个bean是被Spring管理的,Person类是用 @Configurable 注释的。我在java的配置中定义了themeSource,而不是在XML中配置的:


@Configuration
@Import(........
@EnableSpringConfigured
public abstract class SpringWebApplicationConfig extends WebMvcConfigurerAdapter {@Autowired
Environment environment;@Value(.......@Bean
public ResourceBundleThemeSource themeSource(){ResourceBundleThemeSource resourceBundleThemeSource = new ResourceBundleThemeSource();resourceBundleThemeSource.setBasenamePrefix("theme-");return resourceBundleThemeSource;}}

One of the most important things to be noticed out here, is that the class is annotated with @EnableSpringConfigured. The following dependencies should be included in the pom.xml along with the plugin definitions.

这里需要注意的最重要的事情之一是,该类使用@EnableSpringConfigured进行注释。xml中应该包含以下依赖项以及插件。

<dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.1.6.RELEASE</version>
</dependency><dependency><groupId>javax.persistence</groupId><artifactId>persistence-api</artifactId><version>1.0.2</version>
</dependency><dependency><groupId>javax.cache</groupId><artifactId>cache-api</artifactId><version>1.0.0</version>
</dependency><plugin><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><version>1.6</version><configuration><complianceLevel>1.6</complianceLevel><source>1.6</source><target>1.6</target><outxml>true</outxml><verbose>true</verbose><showWeaveInfo>true</showWeaveInfo><aspectLibraries><aspectLibrary><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></aspectLibrary></aspectLibraries></configuration><executions><execution><phase>process-classes</phase><goals><goal>compile</goal></goals></execution></executions>
</plugin>

Now after maven build, the target model class Person, would get the required dependencies, weaved in it, by AspectJ. So, every time we create an object of Person class which necessarily is not Spring-Managed will have the declared dependency weaved in it. We can verify it just by logging the object toString() versione as I have done in one of my Controller method just like the following.

现在,在maven构建之后,目标模型类Person将通过AspectJ获得所需的依赖项。因此,每当我们创建Person类的对象时(它不一定是spring管理的),声明的依赖关系就会交织在其中。我们可以通过记录对象toString()版本来验证它,就像我在控制器方法中所做的那样,如下所示:


@RequestMapping(value="Emp.......
public ModelAndView getEmployeeProfileLayout(){...............Person person = new Person();person.setLastName("PAUL");log.debug(" The Person is:"+person);...............................}

We can now find that the person object would contain an instance of type ResourceBundleThemeSource even though the object has been created using new operator.

现在我们可以发现person对象将包含ResourceBundleThemeSource类型的实例,即使该Person是使用new创建的。

@Configurable相关推荐

  1. FPGA中的Logic Cells, Logic Slices, Configurable Logic Blocks and Gates 的定义

    一个逻辑单元(logic cell) 包含了一个查找表,一个触发器和与附近单元的连接电路.查找表使用组合逻辑实现了一个4输入的逻辑表达式(与,或,与非,加等). 一个逻辑片(logic slice)  ...

  2. Spring5参考指南:AspectJ高级编程之Configurable

    文章目录 遇到的问题 @Configurable 原理 重要配置 遇到的问题 前面的文章我们讲到了在Spring中使用Aspect.但是Aspect的都是Spring管理的Bean. 现在有一个问题, ...

  3. Spring注解中@Configuration和@Configurable的区别

    前言 今天在写注解时,不小心把@Configuration写成了@Configurable,这两个长得很像,所以顺便探究下,有什么区别. @Configuration 该注解是可以用来替代XML文件. ...

  4. 如何在SAP里创建configurable material物料主数据

    使用tcode CT04创建characteristic: assign 所有可能的color value: 使用tcode CL02创建class. 类型选择300- variant: 将step1 ...

  5. magento 获取产品存货量以及configurable product 下associated children product信息

    Magento – Get Product Stock Quantity To get the quantity in stock for a particular product in Magent ...

  6. Magento 获取有效属性 Display available options for attributes of Configurable

    1. 获取全部 //get Product $cProduct = Mage::getModel('catalog/product')->load($_product->getId()); ...

  7. 关于数据属性特性configurable设置为false后的限制

    对象属性有两种,一种是数据属性,另一种是访问器属性. 数据属性有四个描述其行为的特性,分别为[[Configurable]].[[Enumerable]].[[Writable]]和[[Value]] ...

  8. Object对象Configurable,Enumerable,Writable含义解读

    1. configurable 当且仅当该属性的 configurable 键值为 true 时,该属性的描述符才能够被改变,同时该属性也能从对应的对象上被删除.默认为 false. 2. Enume ...

  9. FPGA底层学习-可配置逻辑块(Configurable Logic Block,CLB)

    文章目录 一.CLB概述 可配置逻辑块(Configurable Logic Block,CLB)是FPGA芯片的底层元件之一.如果把FPGA比作一个人体,那么CLB就是细胞,连接起来构造成身体. 但 ...

最新文章

  1. Java 输入流与输出流
  2. python whl大全
  3. js判断浏览器\屏幕分辨率(转载)
  4. web自动化之鼠标事件
  5. tomcat双击startup.bat启动时闪退
  6. ZoomIt – 屏幕标注、电子画笔 [小工具]
  7. 算法竞赛入门与进阶 (二)单调队列、单调栈
  8. java实现 - 树的层序遍历
  9. Android.mk宏定义demo【转】
  10. 一文理解JDK静态代理、JDK动态代理、Cglib动态代理
  11. 华为鸿蒙将比安卓快,任正非透露华为鸿蒙系统:将比安卓速度快60%
  12. 三星性能测试软件,三星R25的性能测试
  13. System center virtual machine manager 2008 R2安装部署
  14. Apache Flink 在翼支付的实践应用
  15. 《你不知道的javascript》读书笔记-上卷-第二章:this的全面解析
  16. java 本地文件上传到服务器,java本地文件上传到远程服务器
  17. 证书更新提示,网易漫画等...
  18. 计算机在语文教学中,计算机技术在语文教学中的运用
  19. 虾神段子小剧场:人人都该学点数据分析(1):达五十万的电竞人才缺口?
  20. 利用八爪鱼爬取关键词搜索的微博数据

热门文章

  1. imx8qxp DDR Test
  2. 计算机SCI/EI期刊投稿经验
  3. 洛谷 P5514 [MtOI2019]永夜的报应【数学/贪心】
  4. 怎么更改计算机管理员用户名和密码,Win10系统无法修改管理员账户密码怎么办?...
  5. 自学渗透避坑指南(二)
  6. java计算机毕业设计ssm“雅乐”私人牙科诊所管理系统的设计与实现
  7. UG NX安装包大集合(包括UG目前发布的所有版本)
  8. mock模拟数据,get、post请求
  9. otter自定义数据同步踩坑实录
  10. 浅谈发电机组接入动环系统