Spring 标准 Bean 注入与自动装配

发布于 2024-11-05 00:09:55 字数 820 浏览 1 评论 0 原文

据我了解,当使用依赖注入时,所有 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注入?

希望我说清楚了。

As far as I understand When Using Dependency Injection all bean are initializing on Start.

<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>

and the configuration above means that userService and userPreferences created when application starts. Is it correct?

When using Autowiring and using <context:component-scan>

public class SimpleUserService{

@Autowired
UserPreferences userPreferences;

//omitted
} 

1) Is userPreference created on Application init?

2) What is the default scope for bean injected by autowire and how can we change it?

3) How affects bean creation and bean injection?

Hope I made myself clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡绿茶香 2024-11-12 00:09:56

上面的配置意味着 userService 和 userPreferences 在应用程序启动时创建。正确吗?

是的

userPreference 是在应用程序初始化时创建的吗?

是的

自动装配注入的 bean 的默认范围是什么?我们如何更改它?

默认范围始终是“singleton”。可以使用 @Scope@Bean 上的 scope XML 属性来更改此设置。

如何影响 Bean 创建和 Bean 注入?

这不是一个明确的问题。如果更改 bean 范围,则在创建它时(应用程序启动、每个请求、每个会话等)也会发生更改。接线配置保持不变,只是生命周期发生变化。

the configuration above means that userService and userPreferences created when application starts. Is it correct?

Yes

Is userPreference created on Application init?

Yes

What is the default scope for bean injected by autowire and how can we change it?

The default scope is always "singleton". This can be changed either using @Scope with @Bean or the scope XML attribute on <bean>.

How affects bean creation and bean injection?

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.

不必你懂 2024-11-12 00:09:56

@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

不必你懂 2024-11-12 00:09:55

首先,您应该将 @Service@Component 添加到 SimpleUserService 类中。

  • 1 是的,UserPreferences 的一个实例是在应用程序初始化时创建的
  • 2 默认范围是单例,您可以使用 @Scope 注释更改它(@See Spring 参考: 3.11.4.4 指定bean范围)
  • 3 组件扫描和XML配置以相同的方式工作(生命周期)

也许你应该花一些时间来理解Spring的生命周期。您需要了解 Spring 以这种方式工作(不是 100% 正确):

  • 首先它创建一个 bean 池
  • ,然后将属性注入到 bean 中,

但它不是这样工作的:获取一个类,查看引用它的内容需要创建此引用(递归),然后创建类。

如果您理解这一点,那么您也会理解,bean 的 @Scope 是在 bean 声明/类中定义的,而不是在引用中定义的。

First of all you should add @Service or @Component to the SimpleUserService class.

  • 1 Yes, the ONE instance of UserPreferences is created at application intialization
  • 2 Default scope is singleton, You can change it with the @Scope annotation (@See Spring Reference: 3.11.4.4 Specifying bean scope)
  • 3 Component scan and XML configuration work in the same way (life cycle)

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):

  • first it creates a pool of beans
  • then it injects the properties into the beans

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.

香草可樂 2024-11-12 00:09:55

1) userPreference 是否创建于
应用程序初始化?

无论哪种情况,userPreferences 都会在加载 Spring Context 时初始化。您可以通过将 lazy-init="true" 添加到 bean 配置来更改此行为。

2) bean的默认作用域是什么
通过 autowire 注入,我们怎样才能
改变它吗?

注入的范围是Spring中加载的所有bean。如果您从另一个项目导入 XML 配置,它也会被包含在内。我不确定你是否可以限制你的范围。

3)如何影响bean创建和bean
注射?

无论是自动装配还是通过 XML 配置,行为都应该是相同的。我更喜欢显式定义依赖关系而不是自动注释。话又说回来,我也喜欢强类型语言。

1) Is userPreference created on
Application init?

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.

2) What is the default scope for bean
injected by autowire and how can we
change it?

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.

3) How affects bean creation and bean
injection?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文