@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. libsvm库简介及使用
  2. 全网把Map中的hash()分析的最透彻的文章,别无二家。
  3. 生成式对抗网络GAN有哪些最新的发展,可以实际应用到哪些场景中
  4. Driver for device rausb0 has been compiled with version 22
  5. 【自动驾驶】5. ROS和DDS的区别总结
  6. 超干货 | 在线教育增长实操者案例分享:如何玩转教育增长模型?
  7. Java 13:文本块
  8. Facebook 推出多模态通用模型 FLAVA,吊打 CLIP 平均十个点!
  9. 海康sip服务器地址_完整SIP/SDP媒体协商概论SDP基础使用要求
  10. 软件测试要经过哪几个阶段?
  11. php友情链接大于3换行,细节见真情 PHPWind v5.3友情链接功能改进
  12. Bailian2952 循环数【数学】
  13. 2020年GitHub上的7个顶级的Java开源目推荐(强烈安利)
  14. Windows Registry
  15. mysql存储过程输出_MySQL存储过程
  16. wsimport命令生成webService java客户端代码
  17. 关于pr(premier)2020不能使用beat edit的解决方法
  18. selenium模拟破解京东滑块验证码
  19. Mac在线看视频卡顿
  20. icp许可证怎么申请

热门文章

  1. ScriptManager.RegisterStartupScript方法
  2. 国产ETL工具 BeeDI 产品“实时同步“之 高阶 功能组件
  3. WSH:一款功能强大的Web Shell生成器和命令行接口工具
  4. Mac版本的Sublime Text常用快捷键
  5. java 多线程(二)
  6. 成都奔驰驾驶辅助改装 蔚一名车汇
  7. 七牛云——对象存储管理工具介绍
  8. HDLC、PPP综合实验
  9. java实现的三阶贝塞尔曲线_n 阶贝塞尔曲线计算公式实现
  10. Matlab的学生版 价格很便宜呢,相比于商业版