问题描述
我有以下 Spring 服务:
I've got the following Spring service:
@Service
public class Worker {
@Autowired
private MyExecutorService executor;
@Autowired
private IRun run;
private Integer startingPoint;
// Remainder omitted
}
现在我想通过 .properties
文件加载 startingPoint
.
Now I want to load the startingPoint
through a .properties
file.
是否可以同时通过注解和 xml 上下文来连接 Spring 服务?
Is it possible to wire a Spring service through annotations and an xml context at the same time?
也许是这样的:
<bean id="worker" class="Worker">
<property name="startingPoint">
<value>${startingPoint}</value>
</property>
</bean>
startingPoint
通过 xml 上下文文件连接,其他一切都自动连接.
startingPoint
is wired through the xml context file, everything else gets auto-wired.
推荐答案
是的!这绝对是可能的,如果您不能使用一点 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.
虽然只是为了清楚起见,但我相信您必须为您的 Integer 字段提供一个 setter.Spring 不想通过 XML 描述符直接访问和设置字段.
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.
这篇关于通过注解和 xml 上下文连接 Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!