Four basic annotations in SPRINGMVC

Source: Internet
Author: User

@Component, @Repository @Service, @Controller

Look at the literal meaning, easy but not out of three of them:

  @Controller Control layer, that's our action layer.

  @Service The business Logic layer, which is our service or manager layer

  @Repository Persistence layer, which is what we often call the DAO layer

and @Component (literally a component), it's used when you can't decide which layer to use.

In fact, these four annotations have the same effect, and spring will load them as a bean that needs to be injected into the context;

However, in the project, it is recommended that you use the project in strict accordance with the meanings of the remaining three annotations except Componen. This is good for a layered Web architecture!!

Example:

1. Control layer

    // Note for controller @RequestMapping ("/login")  Publicclass  loginaction {@Autowired @Qualifier ("userservice"// Note specifying the injected Bean Private  Iuserservice UserService; 。。。。。。  Other slightly ... }  

2. Business Logic Layer

   @Service ("userservice")  publicclass  Userserviceimpl Implements Iuserservice {@Autowired @Qualifier ("userdao")   Private  Iuserdao Userdao; 。。。。。。  Other slightly ... }

3. Persistence Layer

@Repository ("Userdao") Public classUserdaoimpl implements Iuserdao {Private StaticLogger Logger = Loggerfactory.getlogger (Userdaoimpl.class); PrivateDataSource DataSource; Privatejdbctemplate template; @Autowired PublicUserdaoimpl (DataSource DataSource) { This. datasource=DataSource; Template=NewJdbcTemplate ( This. DataSource); }

Four basic annotations in SPRINGMVC

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.