Spring @ component: spring @ component

Source: Internet
Author: User

Spring @ component: spring @ component

Spring @ component

1. @ controller (injection Service)
2. @ service (inject dao)
3. @ repository dao (implementing dao access)
4. @ component (instantiating a common pojo to a spring container is equivalent to <bean id = "" class = ""/> in the configuration file)

@ Component, @ Service, @ Controller, @ Repository annotation class, and add these classes to the spring container for management.

Below is the scanning component that introduces component.

<context:component-scan base-package=”com.mmnc”> 

Base-package indicates the package to be scanned (including all sub-packages)

1. @ Service is used to mark Service layer components
2. @ Controller is used to mark control layer components (such as actions in struts)
3. @ Repository is used to mark the data access component, that is, the DAO component.
4. @ Component refers to a Component. When the Component is difficult to classify, we can use this annotation for annotation.
@ Service public class UserServiceImpl implements UserService {}

@ Repository public class UserDaoImpl implements UserDao {} the default name of getBean is the class name (the first letter is lower case). to customize it, @ Service ("***") in this way, this bean is a singleton by default. If you want to change it, you can use @ Service ("beanName ")

@ Scope ("prototype") to change. You can use the following method to specify the initialization method and destruction method (the method name is arbitrary): @ PostConstruct public void init (){}

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

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.