Declaring bean annotations:

@component component, bean without explicit role

@service, used in the business logic layer (service)

@repository, used in the data access layer (dao)

@controller, used in the presentation layer

@configuration declaration configuration class

The entity class does not need to add annotations,Because there is no need to "inject" the entity class

Annotations that specify the scope of the bean:

@scope ("prototype")

The default is singleton

Optional values ​​prototype, request, session, globalsession

Annotate the method that declares the bean:

@bean is used on methods,Tell the spring container that you can get a bean from this method

Get beans using the getbean method of the annotationapplicationcontext object

Annotation of injected bean:

@autowired, automatic injection (bytype injection by default), can be used on properties or methods,You can set required="false" to indicate that it is not required to inject multiple implementations of the same interface,Distinguished by @qualifier

When the injected variable is list or map, all interface implementations are injected.key is the name of the bean, and value is the implementation class object.You can specify the load order by [email protected]=1 on the implementation class,The smaller the number, the higher the priority.

@lazy starts delayed injection

Configuration class annotations:

@configuration declares that the current class is a configuration class,Equivalent to an xml file for spring configuration

@componentscan, automatically scan all beans under the package name where the configuration class is located

@enableautoconfiguration, start automatic configuration

These three annotations can be replaced with a @springbootapplication in spring boot

@enabletransactionmanagement, enable transaction support

Transaction Management:

@enabletransactionmanagement, added to the configuration class,Turn on transaction support

@transactional, added to the service method,Annotation requires transaction support

aop annotation:


Task scheduling:

@scheduled is used on methods that require scheduled execution
@enablescheduling is used on the entry class that needs to be used

Spring MVC integration:

First need to modify the application class

//1, add inheritance springbootservletinitializer
public class application extends springbootservletinitializer {
  public static void main (string [] args) {
    springapplication.run (application.class, args);
  //2, override the configure method
  protected springapplicationbuilder configure (springapplicationbuilder builder) {
    return super.configure (builder);
  Spring mvc annotations:
@controller, used in the presentation layer
  • Previous Detailed key namespace examples of PHP
  • Next Improved jQuery homemade tooltip tooltip