我是否需要明确使用“autowire”?在 xml 文件中自动装配 Spring Web 应用程序
我尝试了很多,然后发现我没有在bean中使用autowire =“byName”,这就是为什么它没有自动写入。
1)我想问的是,使用@Resource也是同样的情况。我的意思是我是否需要在bean中编写自动装配
2)但是在某些文件中@Resource无需在bean中编写自动装配即可工作
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 autowire in beans
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您按名称自动装配时(默认是按@Autowired注释中的类型),那么它和@Resource之间基本上没有区别。您可以选择在 xml 中定义这些自动装配的 bean,或者只是添加
到 application-context.xml,其中 service(例如)是您的包,用于告诉 spring 在项目中的何处扫描注释。然后它会自动找到那些带注释的类(在该包内)。
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
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).
效果本质上是相同的。
@Resource
提供了与 spring 的解耦,因为它位于javax
包中。@Resource
也相当于将@Autowired
与@Qualifier
结合使用是的,这就是它的工作原理。当像
@Resource("someBean")
一样应用时,它会执行"autowire"-byname
,而不管autowire = "byName"
的任何值如何。The effect is essentially the same. The
@Resource
provides a decoupling from spring as it's in thejavax
package.@Resource
is also equivalent of using@Autowired
in combination with@Qualifier
Yes, that's how it works. It does
"autowire"-by-name
when applied like@Resource("someBean")
regardless of any valueautowire = "byName"
.@Resource
注释不使用自动装配,因为您指定了要连接的资源的名称。因此您不需要在 bean 上指定任何特定的自动装配值。即使您使用
@Autowire
注释,您也不需要设置autowire
属性。您可以使用属性或注释,但不需要两者都使用。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 theautowire
attribute. You use the attribute or the annotation but you don't need both.