[Spring Framework] learning notes -- the foundation of stereotype such as @ Component, and stereotypespring

Source: Internet
Author: User

[Spring Framework] learning notes -- the foundation of stereotype such as @ Component, and stereotypespring

Before continuing to explain Spring MVC, we need to talk about several annotation commonly used to mark stereotype.

@ Component, @ Controller, @ Repository, @ Service.

The four are under the org. springframework. stereotype package, and the last three are @ Component.

It can be understood that @ Component is the base class of @ Controller, @ Repository, and @ Service.

 

@ Component is used to mark any Component managed by Spring.

@ Controller is used to mark the presentation layer (such as web controller ).

@ Repository is used to mark the persistence layer (such as DAO ).

@ Service is used to mark the service layer.

 

What if we add the annotation to our class and let Spring automatically find these classes and implement registration?

The <context: component-scan> element must be used in Spring xml. base-package is the package name to be scanned.

<beans    ...    xmlns:context="http://www.springframework.org/schema/context"    ...>    <context:component-scan base-package="xxx.xxx"/></beans>

 

<Context: component-scan> is activated by default. What does <context: annotation-config> do?

It is mainly for the @ Autowired service (in other words, enable Spring to process annotations such as @ Autowired and @ Configuration). If there is no <context: annotation-config>, We need to register every bean in the XML file of Spring,

As a result, the entire XML file is very large and difficult to maintain. Now we can use a row to implement registration, which is simple and convenient.

 

NOTE 1: With <context: component-scan>, we do not need to explicitly define <context: annotation-config>.

NOTE 2: <context: annotation-config> only search for beans marked by annotation under the same application context.

For example, if <context: annotation-config> is added to WebApplicationContext, it only checks beans marked as Autowired in the controller and does not check services.


Which jar file does orgspring frameworkstereotypecontroller belong?

Spring. jar or spring-context.jar

The orgspringframeworkstereotype package in spring310M2 cannot be found. You need to know which package the package is in.

Is the problem solved? I also encountered this problem. The solution was just to export the annotation Action.
Import org. apache. struts2.convention. annotation. Action;
 

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.