do i need to explicitly use "autowire" in xml file to autowire in Spring web app
I tried so much and then find out the i didn't use autowire = "byName"
in bean and thats why it was not autowriring.
1) i want to ask that is that the same case with using @Resource. i mean do i need to write autowire in bean for that
2)But in some of files @Resource is working without writing autow开发者_C百科ire in beans
When you autowire by name (default is by type within the @Autowired annotation), then there is basically no difference between it and @Resource. You can either choose to define those autowired beans in xml, or you just add
<context:component-scan base-package="service"/>
to your application-context.xml, where service (for example) is your package to tell spring where to scan for annotations in your project. Then it will automatically find those annotated classes (within that package).
i want to ask that is that the same case with using @Resource. i mean do i need to write autowire in bean for that
The effect is essentially the same. The @Resource
provides a decoupling from spring as it's in the javax
package. @Resource
is also equivalent of using @Autowired
in combination with @Qualifier
But in some of files @Resource is working without writing autowire in beans
Yes, that's how it works. It does "autowire"-by-name
when applied like @Resource("someBean")
regardless of any value autowire = "byName"
.
The @Resource
annotation doesn't use autowiring, since you specify the name of the resource you want to wire in. So you don't need to specify any particular autowire value on the bean.
Even if you use the @Autowire
annotation, you shouldn't need to set the autowire
attribute. You use the attribute or the annotation but you don't need both.
精彩评论