Wiring Spring bean through annotations and xml context
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 w开发者_如何学JAVAire 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.
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.
精彩评论