通过注解和 xml 上下文连接 Spring bean [英] Wiring Spring bean through annotations and xml context

查看:41
本文介绍了通过注解和 xml 上下文连接 Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆