java – Spring – 使用@Configurable和@Value注释使用new运算符创建对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Spring – 使用@Configurable和@Value注释使用new运算符创建对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2166字,纯文字阅读大概需要4分钟。
内容图文
![java – Spring – 使用@Configurable和@Value注释使用new运算符创建对象](/upload/InfoBanner/zyjiaocheng/803/5129df1baa1944e891376734e21e464d.jpg)
是否可以在使用AspectJ编织的类上使用@Configurable,并使用Spring加载使用@Value注释的字段/方法的值?
我知道它可能与@Autowired和@Resource等…还有其他人.
例如
@Configurable
public Class MyObj{
@Value("$(my.prop)")
private String aField;
public String getAField(){
return aField;
}
}
然后有类似的东西
public aMethodSomewhereElse(){
MyObj obj = new MyObj()
assertNotNull(obj.getAField());
}
是否有任何替代方法可以使用new运算符创建MyObj并仍然可以处理注释?
– 编辑: –
使用@Autowired时可以使用new来实现这一点,看一下Spring和AOP的一些Hibernate和JPA的东西……我过去曾经用它来做一些Java代码的分析.但我真的想在我模拟一个完整的例子之前使用SPEL和@Value,我希望在这里找到答案.仅供参考 – 如果您不相信我,Spring手册甚至表示可以这样做,我想知道的是它是否可以在同一范围内使用@Value注释…
The Spring container instantiates and configures beans defined in your
application context. It is also possible to ask a bean factory to
configure a pre-existing object given the name of a bean definition
containing the configuration to be applied. The spring-aspects.jar
contains an annotation-driven aspect that exploits this capability to
allow dependency injection of any object.
和…
Using the annotation on its own does nothing of course. It is the
AnnotationBeanConfigurerAspect in spring-aspects.jar that acts on the
presence of the annotation. In essence the aspect says “after
returning from the initialization of a new object of a type annotated
with @Configurable, configure the newly created object using Spring in
accordance with the properties of the annotation”. In this context,
initialization refers to newly instantiated objects (e.g., objects
instantiated with the ‘new’ operator) as well as to Serializable
objects that are undergoing deserialization (e.g., via readResolve()).
http://static.springsource.org/spring/docs/3.0.0.RC2/reference/html/ch07s08.html
干杯.
解决方法:
你是绝对正确的 – @Autowired字段将连接到一个@Configurable注释类,即使在Spring容器之外,假设你有一个AspectJ基础设施.
你已经注意到了很好的捕获,@ Value字段由Spring bean后处理器(AutowiredAnnotationBeanPostProcessor)处理,它解析了@Value注释字段.它不会对容器外部实例化的对象起作用 – 所以简而言之,@ Autowired字段应该接通,但@Value属性不会.
内容总结
以上是互联网集市为您收集整理的java – Spring – 使用@Configurable和@Value注释使用new运算符创建对象全部内容,希望文章能够帮你解决java – Spring – 使用@Configurable和@Value注释使用new运算符创建对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。