Spring 标准 Bean 注入与自动装配
据我了解,当使用依赖注入时,所有 bean 都会在启动时初始化。
<bean id="userPreferences" class="com.foo.UserPreferences">
</bean>
<!-- a singleton-scoped bean injected to the above bean -->
<bean id="userService" class="com.foo.SimpleUserService">
<!-- a reference to the userPreferences bean -->
<property name="userPreferences" ref="userPreferences"/>
</bean>
上面的配置意味着 userService 和 userPreferences 在应用程序启动时创建。正确吗?
当使用自动装配并使用
public class SimpleUserService{
@Autowired
UserPreferences userPreferences;
//omitted
}
1) userPreference 是在应用程序初始化时创建的吗?
2)自动装配注入的bean的默认范围是什么,我们如何更改它?
3)如何影响bean创建和bean注入?
希望我说清楚了。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的
是的
默认范围始终是“singleton”。可以使用
@Scope
和@Bean
或
上的scope
XML 属性来更改此设置。这不是一个明确的问题。如果更改 bean 范围,则在创建它时(应用程序启动、每个请求、每个会话等)也会发生更改。接线配置保持不变,只是生命周期发生变化。
Yes
Yes
The default scope is always "singleton". This can be changed either using
@Scope
with@Bean
or thescope
XML attribute on<bean>
.This isn't a clear question. If you change the bean scope, you change when it gets created (start of application, on each request, on each session, etc). The wiring configuration remains the same, only the lifecycle changes.
@autowired 表示法是表示 @inject 的过时方式。后者是 JavaEE 6 的一项功能。
stackoverflow.com/questions/7142622/what-is-the-difference- Between -inject-and-autowired-in-spring-framework-which
The @autowired notation is an obsolete way to say @inject. THe latter is a feature of JavaEE 6.
stackoverflow.com/questions/7142622/what-is-the-difference-between-inject-and-autowired-in-spring-framework-which
首先,您应该将
@Service
或@Component
添加到SimpleUserService
类中。也许你应该花一些时间来理解Spring的生命周期。您需要了解 Spring 以这种方式工作(不是 100% 正确):
但它不是这样工作的:获取一个类,查看引用它的内容需要创建此引用(递归),然后创建类。
如果您理解这一点,那么您也会理解,bean 的 @Scope 是在 bean 声明/类中定义的,而不是在引用中定义的。
First of all you should add
@Service
or@Component
to theSimpleUserService
class.Maybe you should spend some time in understanding the Spring life cycle. You need to understand that Spring works a bit in this way (not 100% correct):
But it does NOT work this way: taking a class, look what references it needs creating this references (recursive) and then creating the class.
If you understand this, then you will also understand, that the @Scope of a bean is defined at the bean declaration/class, but not at the references.
无论哪种情况,userPreferences 都会在加载 Spring Context 时初始化。您可以通过将
lazy-init="true"
添加到 bean 配置来更改此行为。注入的范围是Spring中加载的所有bean。如果您从另一个项目导入 XML 配置,它也会被包含在内。我不确定你是否可以限制你的范围。
无论是自动装配还是通过 XML 配置,行为都应该是相同的。我更喜欢显式定义依赖关系而不是自动注释。话又说回来,我也喜欢强类型语言。
In either case userPreferences is initialized when Spring Context is loaded. You can change this behavior by adding
lazy-init="true"
to the bean configuration.The scope of what is injected is all beans loaded into Spring. If you import an XML configuration from another project, it too would be included. I'm not sure if you can limit your scope.
Whether is autowired, or configured via XML, the behavior should be the same. I prefer explicitly defining dependencies over automatic annotations. Then again I also like strongly typed languages.