如何使用 Java 1.4 兼容版本替换 @Resource 注解
我有一个测试类,它有一个用于 setter 的 @Resource 注释,我需要使其符合 Java 1.4,所以显然必须删除该注释。我正在使用春天。
那么,我如何替换 @Resource("my.resource") 这样的东西,以便设置器获得正确的依赖注入?我需要在 xml 文件中创建一个 bean 吗?
我对此很陌生,所以如果我没有提供足够的信息,请告诉我。
I have a test class that has a @Resource annotation for a setter and I need to make it Java 1.4 compliant, so obviously the annotation has to go. I'm using Spring.
So, how would I replace something like @Resource("my.resource") so that the setter gets the correct dependency injection? Would I need to make a bean in an xml file?
I'm pretty new to this so if I'm not providing enough information, let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您处于 Java 1.4 环境中,则不能依赖注释,正如您已经正确提到的那样。因此,您必须在配置 Spring ApplicationContext 的 XML 文档中声明依赖项和 bean 定义。
If you are in a Java 1.4 environment you cannot rely on Annotations as you already mentioned correctly. So you have to declare dependencies and bean definitions inside your XML-document that configures your Spring ApplicationContext.
考虑一下xdoclet?
XDoclet 实际上是 Java 5 中注释的前身。它是一个开源代码生成库,通过插入特殊的 Javadoc 标签,可以实现面向属性的 Java 编程。它附带了一个预定义标签库,可简化各种技术的编码:Java EE、Web 服务、Portlet 等。
Consider xdoclet?
XDoclet was really the predecessor to what became annotations in Java 5. It is an open-source code generation library that enables Attribute-oriented programming for Java via insertion of special Javadoc tags. It comes with a library of predefined tags, which simplify coding for various technologies: Java EE, Web services, Portlet etc.
一种巧妙的方法是实现 ApplicationContextAware 接口,然后在类中的 setApplicationContext 内使用 ApplicationContext.getBean 获取 bean。
http://static.springsource。 org/spring/docs/2.0.x/api/org/springframework/context/ApplicationContextAware.html
A hacky way to do it, is implementing the ApplicationContextAware interface, and then getting the bean using ApplicationContext.getBean inside setApplicationContext in your class.
http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/context/ApplicationContextAware.html