Spring注释之间的区别 [英] Difference between Spring annotations

查看:134
本文介绍了Spring注释之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

1) @Component 与<$ c之间的差异$ C> @Configuration ?

我已经读过,两者都删除了将代码放入XML格式的必要性,但没有区分这些代码。

I have read that both remove the necessity of wiring code to be put in XML, but did not get the difference between these.

2) @Autowired @Inject @Resource ?

- 使用哪一个?

- 每个的优点/缺点是什么?

2) What are the differences between @Autowired, @Inject and @Resource?
- Which one to use when?
- What are the pros/cons of each?

推荐答案

@Component @Configuration 确实是非常不同类型的注释。

@Component and @Configuration are indeed very different types of annotations.

@Component 和类似的注释( @Service @Repository 等)及其 JSR-330 对应 @Named 允许您使用< context:component-scan /> 或 @ComponentScan 他们注册了类的bean定义,因此它们大致相同使用XML中的< bean ... /> 标记声明指定的bean。此bean类型将遵循标准代理创建策略。

@Component and similar annotations (@Service, @Repository, etc. )and its JSR-330 counterpart @Named allow you to declare beans that are to be picked up by autoscanning with <context:component-scan/> or @ComponentScan they register the bean definition for the classes, so they are roughly equivalent to declaring the specified beans with the <bean ... /> tag in XML. This bean types will adhere to the standard proxy creation policies.

@Configuration 注释被设计为替换XML配置文件。要创建 @Configuration 带注释的bean,Spring将始终使用 CGLIB 来继承 @Configuration 带注释的类,覆盖其 @Bean 带注释的方法,用bean查找方法替换它,使单个bean只创建一次。 (Spring不使用 CGLIB 来拦截普通 Spring bean的内部方法调用,它会创建一个单独的代理实例相反(与JDK代理一样)。这样做允许使用代理来避免基数不匹配 - 例如,代理单例可以获取当前会话bean,这对于类继承是不可能的。)。尽管如此, @Configuration 带注释的类仍然可以使用带注释的( @Autowired @从容器中注入等)字段和属性以请求bean(甚至其他 @Configuration 带注释的bean)。

@Configuration annotation was designed as the replacement of the XML configuration file. To create @Configuration annotated beans, Spring will always use CGLIB to subclass the @Configuration annotated class, overriding its @Bean annotated method to replace it with the bean lookup method to make singleton beans to be created only once. (Spring does not use CGLIB to intercept internal method calls of normal Spring beans, it creates a separate instance of proxy instead(same way like JDK proxy does). Doing so allows to use proxies to avoid cardinality mismatch - for example a proxy singleton can fetch current session bean, which is not possible with class inheritance only. ). Despite that, @Configuration annotated classes are still able to use annotated(@Autowired, @Inject etc.) fields and properties to request beans (and even other @Configuration annotated beans too) from the container.

示例来自文档

@Configuration
public class AppConfig {

  @Bean
  public ClientService clientService1() {
    ClientServiceImpl clientService = new ClientServiceImpl();
    clientService.setClientDao(clientDao());
    return clientService;
  }
  @Bean
  public ClientService clientService2() {
    ClientServiceImpl clientService = new ClientServiceImpl();
    clientService.setClientDao(clientDao());
    return clientService;
  }

  @Bean
  public ClientDao clientDao() {
    return new ClientDaoImpl();
  }
}

在上面的例子中只有一个将创建ClientDao 实例。

in the example above only one ClientDao instance will be created.

@Autowired 是Spring注释,而 @Inject 是一个JSR-330注释。
@Inject 相当于 @Autowired @Autowired(required = true ),但是你无法使用JSR-330获取 @Autowired(required = false)行为 @Inject 注释。此注释始终使用按类型自动装配。

@Autowired is Spring annotation, while @Inject is a JSR-330 annotation. @Inject is equivalent to @Autowired or @Autowired(required=true), but you can't get @Autowired(required=false) behavior with the JSR-330 @Injectannotation. This annotation always uses by-type autowiring.

Spring实现 JSR-250 @Resource 以相当特殊的方式进行注释。 @Resource 最初设计用于在Java EE中定位JNDI资源,但Spring扩展了它的适用性,使得可以连接到容器中的任何bean(JNDI资源可用作bean SimpleJndiBeanFactory <的帮助/ A>)。
如果没有名称,可以将相应bean的名称指定为 name @Resource 注释的属性如果已指定,则将使用带注释的字段或属性的名称。另一个奇怪的特征是,如果没有找到具有属性名称的bean,则spring将回退到类型接线。

Spring implements JSR-250 @Resource annotation in a rather special way. @Resource was originally designed for locating JNDI resources in Java EE, but Spring widens it applicability making it possible to wire to any bean in the container(JNDI resources are available as beans with the help of SimpleJndiBeanFactory). The name of the corresponding bean can be specified as name attribute of @Resource annotation, if no name was specified, then the name of the annotated field or property will be used. Another strange feature is that if no bean with the property name was found spring will fallback to by-type wiring.

示例
想象一下,我们有一个名为 beanAlpha AlphaClass bean和一个 BetaClass bean beanBeta容器中的

Example Imagine that we have an AlphaClass bean named beanAlpha and a BetaClass bean beanBeta in the container.

@Resource 
BetaClass something;  // Wires to beanBeta - by-type

@Resource 
BetaClass beanAlpha;  // Will throw exception, because "beanAlpha" is not BetaClass -> it's a bad idea to use @Resource as a replacement of @Autowired

@Resource 
Object beanAlpha;  //Wires to beanAlpha - by-name

因此,最好始终明确指定资源名称使用 @Resource 注释。

So it's a good practice to always specify resource name explicitly when using @Resource annotation.

文档

Spring注释

Bean标准注释

更新修复了 shevchik 指出的JSR引用。 DI特定注释由JSR-330提供,JSR-330由Google(Guice Framework)和SpringSource(Spring Framework)工程师开发。 @Resource 是基于JNDI的,由提供JSR-250

update fixed JSR references as shevchik has pointed out. DI specific annotations are provided by JSR-330, which was developed by Google (Guice Framework) and SpringSource(Spring Framework) engineers. @Resource is JNDI based and provided by JSR-250.

这篇关于Spring注释之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆