Spring+MVC+MYbatis annotation development

Spring Common Annotations

Annotation 1: @Configuration

Used on a class, the class with this annotation can become a spring xml configuration file, using the configuration of java code

Annotation 2: @ComponentScan

Used on classes, adding annotations can specify the scanning path

Note 3: Notes on creating objects

1. @Controller: Usually used for the Controller class, which is the control layer (MVC).

2. @Service: Usually used in the Service class, which is the service layer.

3. @Repository: Usually used for DAO classes, which is the persistence layer.

4. @Component: Generic Creation

Notice:

1. Do not specify the name of the bean, the default is the name of the lowercase initials of the class

2. Specify the name of the bean

Note 4: @Autowired [default BY TYPE]

This annotation can be used on properties, setter methods, and constructors. This annotation is used to inject dependent objects. When adding the @Autowired annotation to a property, sometimes it may be necessary to specify some additional values, and Spring will automatically assign the value to this property.

Note 5: @Qualifier

This annotation is used together with @Autowired. When you want to have more control over the injection process, @Qualifier can help specify more detailed configurations. Generally, when two or more beans are of the same type, spring will be confused when injecting. If only @Autowired is used, there will be confusion, but @Qualifier can be used to achieve distinction.

Note 6: @Resource [default BY NAME]

Spring resolves the name attribute to the name of the bean, and the type attribute is resolved to the type of the bean. So if you use the name attribute, use the ByName automatic injection strategy, and if you use the Type type, use the ByType automatic injection strategy. If neither is specified, the ByName automatic injection strategy is used through the reflection mechanism.

Note 7: Bean's life scope and life cycle

 Note eight: @ContextConfiguration

 accomplish:

1. xml implementation

2. Pure annotation method

Note 9: Notes on aspect-oriented programming

1. @Pointcut

This annotation is an annotation for aspect-oriented programming. This annotation is used on a method to import a certain method to this method, as follows:

When we need to use oriented programming, first we need to add an annotation @Aspect to the class to indicate that this class is an aspect; then we can add @Pointcut at the corresponding position to indicate that this method is used as a connection point, and use the corresponding method !

2. @Aspect

Indicates that this class is an aspect

3. @Before(" ") ----- pre-notification

4. @After(" ") ---- post notification

5. @Around(" ") ---- surround notification

MVC Common Annotations

Note ten @Requestmapping

This is a method annotation used to set the current controller method request access path

Note eleven @ResponseBody

Set the response content of the current controller method as the current return value without parsing

Annotation twelve @RequestBody and @RequestParam

Note 13 @ EnableWebMvc

When you want to enable spring mvc, the application uses the annotation @EnableWebMvc to enable the configuration of spring mvc. In addition, if you want to customize these configurations, use a configuration class that can be hosted in the spring container, inherit the WebMvcConfigurerAdapter class and rewrite the required custom configuration of those methods.

Guess you like

Origin blog.csdn.net/young_man2/article/details/129180943