Spring Annotation Encyclopedia Enabled

Source: Internet
Author: User
the role of spring @component
1, @controller Controller (injection service)
2, @service Service (injected DAO)
3, @repository DAO (Implementing DAO Access)
4, @component (Instantiate the normal pojo into the spring container, which is equivalent to the <bean id= "class="/>) in the configuration file
@Component, @Service, @Controller, @Repository annotation classes and incorporate these classes into the spring container for management.
Here's a scan component that introduces component.
<context:component-scan base-package= "COM.MMNC" >

Where Base-package is the package that needs to be scanned (including all child packages)
1, @Service for labeling business layer components
2, @Controller used to mark the control layer components (such as action in struts)
3, @Repository used to annotate data access components, that is, DAO components.
4, @Component refers to the component, when the component is not good to classify, we can use this annotation to annotate.
@Service public class Userserviceimpl implements UserService {}
The default name for the @Repository public class Userdaoimpl implements Userdao {} Getbean is the class name (first letter lowercase), and if you want to customize it, you can @service ("* *") This specifies that the bean defaults to a single example and, if you want to change, you can use @service ("Beanname")
@Scope ("prototype") to change. You can specify initialization methods and destruction methods (any of the method names) by using the following methods: @PostConstruct public void init () {}
the difference between @Autowired and @resource

1, @Autowired and @resource can be used to assemble beans. Can be written on a field, or written on a setter method.
2, @Autowired default by type assembly (this annotation is industry spring), by default must require a dependent object must exist, if you want to allow null values, you can set its Required property to false, such as: @Autowired (required= False), if we want to use name assembly can be used in conjunction with @qualifier annotations, as follows: Java code @Autowired () @Qualifier ("Basedao") private Basedao Basedao;

3, @Resource (this annotation belongs to the Java EE), the default security by name for assembly, the name can be specified by the names property,
If you do not specify the Name property, when the annotation is written on the field, the default fetch field name is searched by name, if the note is written on the setter method by default to assemble the property name. Assembly is done according to type when no bean matching the name is found. It should be noted, however, that if the Name property is specified, it will only be assembled by name. Java Code @Resource (name= "Basedao") private Basedao Basedao;

You can assemble in Java code using either @autowire or @resource annotations, the difference between the two annotations is:
@Autowire is assembled by type by default, which requires that dependent objects must exist. If allowed null, you can set it required property to False, if we want to use by name assembly, can be used in conjunction with @qualifier annotation;


@Resource by default, when a bean matching the name is not found to be assembled according to the type, can be specified by the Name property, if the Name property is not specified, and when the annotation is annotated on the field, that is, the default Fetch field is used as the bean name for the dependent object. When the annotation is annotated on the property's setter method, the default Fetch property name is used as the bean name to find the dependent object.


various annotation ways

1. @Autowired annotations (not recommended, recommended to use @resource)

@Autowired can annotate member variables, methods, and constructors to accomplish automatic assembly work. @Autowired are labeled in different locations, they automatically assemble this property when spring initializes the bean. To enable @autowired to work, you also need to include the following in the configuration file

XML code

<bean class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

2. @Qualifier Notes

@Autowired are automatically assembled according to the type. For example, if there is more than one Userdao type of bean in the spring context, a Beancreationexception exception is thrown, and if the Userdao type of bean does not exist in the spring context, Also throws a Beancreationexception exception. We can use @qualifier with @autowired to solve these problems. As follows:

1). There may be multiple Userdao instances

Java code

@Autowired @Qualifier ("Userserviceimpl") public iuserservice UserService;

Or

Java code

@Autowired public void Setuserdao (@Qualifier ("Userdao") Userdao Userdao) {This.userdao = Userdao; }

In this way, spring will find the bean with ID userserviceimpl and Userdao to assemble.

2). Userdao instances may not exist

Java code

@Autowired (required = false) public Iuserservice UserService;

3. @Resource Notes

The JSR-250 standard annotation is recommended for use in place of Spring's proprietary @autowired annotations. The role of @Resource is equivalent to @autowired, but @autowired by Bytype automatic injection, and @resource by default by byname automatically injected Bale. It is important that @Resource have two properties, respectively, that name and type,spring resolve the Name property of the @Resource note to the bean, and the Type property resolves to the bean. So if you use the Name property, you use the ByName automatic injection policy, and the Type property uses the Bytype automatic injection policy. If neither the name nor the type attribute is specified, the byname is used to automatically inject the policy through the reflection mechanism. To enable @autowired to work, you also need to include the following in your configuration file:

XML code

<bean class= "Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

@Resource Assembly Order:

A. If name and type are specified at the same time, a unique matching bean is assembled from the spring context and the exception is thrown if it is not found

B. If name is specified, a bean that matches the name (ID) in the context is assembled, and an exception is thrown if it is not found

C. If type is specified, an assembly is found from the context that matches a unique bean, the exception is thrown when multiple is not found or found

D. If neither name is specified nor type is specified, the assembly is automatically byname (see 2), and if there is no match, the fallback is matched to an original type (Userdao) and automatically assembled if matched;

4. @PostConstruct (JSR-250) annotations

With annotation @postconstruct on the method, this method is executed by the spring container after the bean is initialized (note: The bean initialization includes, instantiating the bean, and assembling the Bean's properties (Dependency injection)). A typical scenario for this is when you need to inject a property defined in a parent class into the bean, and you cannot copy the setter method of the property or property of the parent class, such as:

Java code

public class Userdaoimpl extends Hibernatedaosupport implements Userdao {private Sessionfactory Mysessio                  Nfacotry; @Resource public void Setmysessionfacotry (Sessionfactory sessionfacotry) {This

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.