Spring Common annotations

Source: Internet
Author: User

There are several common annotations:

@Autowired
@Qualifier ("")
@Repository
@Service
@Component
@Controller
@WebServelet
@Entity
@Table (name= "")
@Id
@Column (length=)
@GeneratedValue (Strategy=generationtype.auto)
@Scope ("")


Statement: A class that uses the above annotations is typically a bean that is not declared in an XML file.By using @Repository, @Component, @Service, and @Constroller annotations on the class, spring automatically creates the corresponding Beandefinition object and registers it in ApplicationContext. These classes become spring-managed components.when a bean is automatically detected, its bean name is generated based on the Beannamegenerator policy of that scanner. By default, name is used as the name of the Bean for @Component, @Repository, @Service, and @controller that contain the Name property. If this annotation does not contain a name value or any other component found by the custom filter, the default Bean name will be the unqualified class name at the beginning of the lowercase. This rule is injected when using @qualifier injection.
@AutowiredTypically used for automatic assignment of member variables, the object to be assigned to the variable, which can be declared as a bean in XML, the type is automatically matched, and if not declared in XML, it needs to be labeled as "component" and can be recognized by spring. There are several ways to label components:
• @Repository          the callout can only be used for labeling the components of the DAO layer implementation class , because the class is not only identifying the class as a bean, but also accessing the dataThe associated exception is encapsulated as spring's own data access exception type. Spring itself provides a rich and data-access exception structure that is independent of the specific data-access technology used to encapsulate exceptions thrown by different persistence-layer frameworks, making exceptions independent of the underlying framework.
• @Service   This annotation is typically used toBusiness LayerA component callout for a class.
        · @Controller This annotation is typically used toControl LayerA component callout for a class.
• @Component This callout is typically used to label components of the neutral layer class.
neutrality is due to the fact that the class does not have a distinct role division and is not part of a role in the MVC framework layer three, which can be a tool class.
@Qualifier ("")because of inheritance and other attributes, there are cases where the parent class is assigned a subclass (using @autowired), for example, assigning a class of DAO to an instance of Daoimp.The Daoimp class is not only annotated with @repository, where the reference to the subclass (@Autowired below) should be immediately labeled @qualifier (""),The class name of the class is indicated in the colon and the first letter is lowercase.
@WebServelet (urlpattern= "") for labeling servlet classes, you can define access path Urlpattern.
@Entity The common Language data table class callout, marked the @entity class, must be a database in the model layer of the table encapsulation.
@Table (name= "") immediately following the @entity, used to label the table names that correspond to the database
• @Idmark this field primary key• @Column (length=,nullable=)indicates the property of the field, corresponding to the properties of the table at the time of Disign. • @GeneratedValue (Strategy=generationtype.auto)indicates that the field is self-increment.
@Scope ("")This annotation qualifies whether the class is a singleton or a prototype. Select Prototype or singleton. Spring defaults to Singleton Singleton when the callout is not in place.

Spring Common annotations

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.