Spring's four basic annotations annotation (control layer, business layer, persistence layer) @Component, @

Source: Internet
Author: User

Four basic annotations in SPRINGMVC:


@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!!

This is about the role of these four annotations in Springmvc, in fact srping the role of these four annotations and springmvc the same.


Example:

1. Control layer

@Controller//Comment as Controller
@RequestMapping ("/login")
public class Loginaction {

@Autowired
@Qualifier ("UserService")//Note Specify the injected Bean
Private Iuserservice UserService;

。。。。。。 Other slightly ...

}

2. Business Logic Layer

@Service ("UserService")
public class Userserviceimpl implements Iuserservice {

@Autowired
@Qualifier ("Userdao")
Private Iuserdao Userdao;

。。。。。。 Other slightly ...

}

3. Persistence Layer

@Repository ("Userdao")
public class Userdaoimpl implements Iuserdao {
private static Logger Logger = Loggerfactory.getlogger (Userdaoimpl.class);
Private DataSource DataSource;
private JdbcTemplate template;

@Autowired
Public Userdaoimpl (DataSource DataSource) {
This.datasource= DataSource;
Template = new JdbcTemplate (This.datasource);
}

Spring's four basic annotations annotation (control layer, business layer, persistence layer) @Component, @

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.