通过注释和 xml 上下文连接 Spring bean
我有以下 Spring 服务:
@Service
public class Worker {
@Autowired
private MyExecutorService executor;
@Autowired
private IRun run;
private Integer startingPoint;
// Remainder omitted
}
现在我想通过 .properties
文件加载 startingPoint
。
是否可以同时通过注释和 xml 上下文连接 Spring 服务?
也许是这样的:
<bean id="worker" class="Worker">
<property name="startingPoint">
<value>${startingPoint}</value>
</property>
</bean>
startingPoint
通过 xml 上下文文件连接,其他所有内容都会自动连接。
I've got the following Spring service:
@Service
public class Worker {
@Autowired
private MyExecutorService executor;
@Autowired
private IRun run;
private Integer startingPoint;
// Remainder omitted
}
Now I want to load the startingPoint
through a .properties
file.
Is it possible to wire a Spring service through annotations and an xml context at the same time?
Maybe something like this:
<bean id="worker" class="Worker">
<property name="startingPoint">
<value>${startingPoint}</value>
</property>
</bean>
startingPoint
is wired through the xml context file, everything else gets auto-wired.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的!这绝对是可能的,如果您无法避免使用一点 XML,那么这是一个好方法。只需保留所有带注释的字段未指定,它们就会神奇地自动注入。
不过为了清楚起见,我相信您必须为您的 Integer 字段提供一个设置器。 Spring 不想通过 XML 描述符直接访问并设置字段。
Yes! This is most definitely possible, and it's a good way to go if you can't get around using a little bit of XML. Just leave all your annotated fields unspecified, and they'll get injected auto-magically.
Though just to be clear, I believe that you'll have to provide a setter for your Integer field. Spring doesn't want to reach in directly and set fields via the XML descriptor.