Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>@Component</code> and <code>@Configuration</code> are indeed very different types of annotations.</p> <p><code>@Component</code> and similar annotations (<code>@Service</code>, <code>@Repository</code>, etc. )and its <a href="http://jcp.org/aboutJava/communityprocess/final/jsr330/index.html" rel="noreferrer">JSR-330</a> counterpart <code>@Named</code> allow you to declare beans that are to be picked up by autoscanning with <code>&lt;context:component-scan/&gt;</code> or <code>@ComponentScan</code> they register the bean definition for the classes, so they are roughly equivalent to declaring the specified beans with the <code>&lt;bean ... /&gt;</code> tag in XML. This bean types will adhere to the standard proxy creation policies. </p> <p><code>@Configuration</code> annotation was designed as the replacement of the XML configuration file. To create <code>@Configuration</code> annotated beans, Spring will always use <code>CGLIB</code> to subclass the <code>@Configuration</code> annotated class, overriding its <code>@Bean</code> annotated method to replace it with the bean lookup method to make singleton beans to be created only once. (Spring does not use <code>CGLIB</code> to intercept <em>internal</em> method calls of <em>normal</em> 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, <code>@Configuration</code> annotated classes are still able to use annotated(<code>@Autowired</code>, <code>@Inject</code> etc.) fields and properties to request beans (and even other <code>@Configuration</code> annotated beans too) from the container.</p> <p><strong>Example</strong> from 4.12.5 section of the <a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-java" rel="noreferrer">documentation</a></p> <pre><code>@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(); } } </code></pre> <p>in the example above only one <code>ClientDao</code> instance will be created. </p> <p><code>@Autowired</code> is Spring annotation, while <code>@Inject</code> is a JSR-330 annotation. <code>@Inject</code> is equivalent to <code>@Autowired</code> or <code>@Autowired(required=true)</code>, but you can't get <code>@Autowired(required=false)</code> behavior with the JSR-330 <code>@Inject</code>annotation. This annotation always uses by-type autowiring.</p> <p>Spring implements <a href="http://jcp.org/en/jsr/detail?id=250" rel="noreferrer">JSR-250</a> <code>@Resource</code> annotation in a rather special way. <code>@Resource</code> 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 <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/jndi/support/SimpleJndiBeanFactory.html" rel="noreferrer">SimpleJndiBeanFactory</a>). The name of the corresponding bean can be specified as <code>name</code> attribute of <code>@Resource</code> 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.</p> <p><strong>Example</strong> Imagine that we have an <code>AlphaClass</code> bean named <em>beanAlpha</em> and a <code>BetaClass</code> bean <em>beanBeta</em> in the container.</p> <pre><code>@Resource BetaClass something; // Wires to beanBeta - by-type @Resource BetaClass beanAlpha; // Will throw exception, because "beanAlpha" is not BetaClass -&gt; it's a bad idea to use @Resource as a replacement of @Autowired @Resource Object beanAlpha; //Wires to beanAlpha - by-name </code></pre> <p>So it's a good practice to always specify resource name explicitly when using <code>@Resource</code> annotation. </p> <p><strong>Documentation</strong></p> <p><a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-java" rel="noreferrer">Spring annotations</a></p> <p><a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-standard-annotations" rel="noreferrer">Bean standard annotations</a></p> <p><strong>update</strong> fixed JSR references as <em>shevchik</em> has pointed out. DI specific annotations are provided by JSR-330, which was developed by Google (Guice Framework) and SpringSource(Spring Framework) engineers. <code>@Resource</code> is JNDI based and provided by <a href="http://jcp.org/en/jsr/detail?id=250" rel="noreferrer">JSR-250</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload