通过注释和xml上下文连接Spring bean

时间:2011-05-31 07:49:06

标签: java spring annotations ejb

我有以下Spring服务:

@Service
public class Worker {

    @Autowired
    private MyExecutorService executor;

    @Autowired
    private IRun run;

    private Integer startingPoint;

    // Remainder omitted

}

现在我想通过startingPoint文件加载.properties

是否可以同时通过注释和xml上下文连接Spring服务?

也许是这样的:

<bean id="worker" class="Worker">
    <property name="startingPoint">
        <value>${startingPoint}</value>
    </property>
</bean>

startingPoint通过xml上下文文件连接,其他所有内容都自动连接。

1 个答案:

答案 0 :(得分:3)

是的!这绝对是可能的,如果你不能使用一点点XML,这是一个很好的方法。只需将所有带注释的字段保留为未指定状态,它们就会自动注入。

虽然要清楚,但我相信你必须为你的整数字段提供一个setter。 Spring不希望直接访问并通过XML描述符设置字段。

相关问题