The meaning of annotation in springboot project

Time:2021-6-14

@Component

@Component: an annotation used to describe the bean class in spring. It is used to tell spring that the instance of this class is created by spring. When this object is created and managed by spring, it will be stored in the bean pool by default.

@Controller
public class GoodsController {

}

@SpringBootTest

@Springboottest: this annotation is used to tell the spring framework that the test class is managed by spring.

@SpringBootTest
public class GoodsDaoTest {

}

@Autowired and @ qualifier

@Autowired: when describing a property, this annotation is used to tell the spring framework to inject a value into the property. The basic rules are as follows:

  1. First, we need to detect whether there are objects in the container that match the property or method parameter types. If there is and only one, we will inject them directly.
  2. Secondly, if multiple objects are detected, whether there are objects with matching names will be searched according to the property or method parameter names described by @ Autowired. If there are, the objects will be injected directly. If not, an exception will be thrown.
  3. Finally, if we have specific requirements, we must inject the object with the specified type and name. We can also use @ qualifier annotation to describe its properties or parameters (this annotation must be used with @ Autowired annotation).

@Qualifier: this annotation describes its properties or parameters.

@Autowired
@Qualifier("weakCache")
private Cache cache;

@Lazy

@Lazy: this annotation is used to describe a class. Its purpose is to tell the spring framework that this class supports deferred loading and is usually used with singleton scopes.

@Lazy
public class GoodsController {
}

@Scope

@Scope: this annotation is used to define the scope of bean object in spring. Its common values are:

singleton prototype
There is a bean instance in the whole memory. When this instance is created is related to the delayed loading feature configuration of the class. After this instance is created, the life cycle will be managed by the spring framework A new instance will be created every time you get it. This instance will be created when needed, which has nothing to do with lazy feature. After this instance is created, it will not be managed by spring. Spring can initialize it, but it is not responsible for destroying it.
@Scope("singleton")
public class GoodsController {
}

@PostConstruct

@Postconstruct: this annotation is used to describe the initialization method in the bean object life cycle method. This method will be executed after the object construction method (initialization after object creation)

@PostConstruct 
public void init() {
    System.out.println("init()");
}

@PreDestroy

@Predestroy: this annotation is used to describe the destroy method in the bean object life cycle method, which will be executed before the object is destroyed (this method will not be executed when the scope is prototype).

@PreDestroy
    public void close() {
        System.out.println("close()");
    }

@Mapper

@Mapper: this annotation is defined by the mybatis framework to describe the data layer interface (all annotations only play a descriptive role). It is used to tell the spring framework that the implementation of this interface is created by mybatis and store its implementation class object in the spring container.

@Mapper
public interface GoodsDao {
}

@Param

@Param: this annotation is used to refer to the parameters in brackets

int deleteObjects(@Param("ids")Integer... ids);

@Controller

@Controller: this annotation is used to mark a class. The class marked with it is a spring MVC controller object. The distribution processor will scan the method of the class that uses the annotation and detect whether the method uses the annotation

@Controller
public class GoodsController {

}

@RequestMapping

@Request mapping: this annotation is used to process the annotation of request address mapping, which can be used on class or method. For a class, the address is used as the parent path for all methods responding to requests in the class.

@RequestMapping("findAll")
public String findAll(Model model) {
    List<User> lists = goodsService.findAll();
    model.addAttribute("list", lists);
    return "goods";
    }