[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;