Spring Basics--Several Commonly Used Injection Annotations

@Component

  • Function: Let the resources be managed by spring. Equivalent to configuring a bean in xml.
  • Attribute: value: the id of the specified bean. If you do not specify the value attribute, the default bean id is the class name of the current class. Lowercase the first letter.
    @Component("account")
    account is the id of the bean class

Three annotations derived from Component

The three annotations @Controller, @Service, and @Repository
are all derived annotations for one, and their functions and attributes are exactly the same. They just provide more explicit semantics.

  • @Controller: Generally used for annotations in the presentation layer.
  • @Service: Generally used for business layer annotations.
  • @Repository: Generally used for annotations of the persistence layer. Details: If there is one and only one attribute to be assigned in the annotation, and the name is value, the value may not be written in the assignment.

@Autowired

  • Function: Automatically inject according to type. When using annotations to inject properties, the set method can be omitted.
    It can only inject other bean types. When multiple types match, use the object variable name to be injected as the bean id, search in the spring container, and if found, the injection can be successful. If you can't find it, report an error.
@Autowired
IAccountDao accountDao1;

The IOC container is a Map structure, the key is the specified value, and the value is its class name and inherited class. It
will search the IOC container for the class name or inherited class of IAccountDao, and the only one will be directly injected.
If there are more than one, the key will be searched in the IOC container according to the custom variable name accountDao1, if there is an equal injection, an error will be reported if there is no

insert image description here

@Qualifier

@Qualifier injects by name (key) on the basis of class injection (@Autowired).
When injecting class member variables, it must be used together with @Autowired. Can be used alone when injecting member methods.
In this way, it will look for the bean type whose id is specified as ("accountDao2") in the spring container

@Autowired
@Qualifier("accountDao2")

@Resource

Combining annotations of @Autowired and @Qualifier

  • Function: Inject directly according to the id of the Bean. It can also only inject other bean types.
  • Attributes: name: specifies the id of the bean.
@Resource("accountDao2")
相等于一下两个注解
@Autowired
@Qualifier("accountDao2")

@Value

  • Function: Inject basic data types and String type data
  • Attributes: value: used to specify the value

@Scope

  • Role: Specifies the scope of the bean.
  • Attributes: value: the value of the specified range.
    Value: singleton prototype
    singleton: singleton object
    prototype: multiple instance objects
@Scope("protype")

@Configuration

Configuration

  • Role: Specifies that the current class is a configuration class
  • Details: When the configuration class is used as the parameter created by the AnnotationConfigApplicationContext object, this annotation can not be written.

@ComponentScan

  • Role: used to specify the package to be scanned by spring when creating the container through annotations
  • Attribute:
    value: It has the same function as basePackages, both of which are used to specify the package to be scanned when creating a container.
  • Using this annotation is equivalent to configuring in xml:
    <context:component-scan base-package="com.iheima"></context:component-scan>
@ComponentScan("com.itheima")

@Bean

Function: used to store the return value of the current method as a bean object in the spring ioc container

  • Attributes:
  • name: The id used to specify the bean. When not written, the default is the name of the current method
  • detail:
  • When we use annotations to configure methods, if the method has parameters, the spring framework will go to the container to find out if there are any bean objects available.
  • The way to find is the same as the Autowired annotation

@Import

It is impossible for us to have only one configuration class when setting up the configuration file. The general structure is a main configuration class, and other sub-configuration classes are imported. If you use @ComponentScan to import packages one by one, it is too troublesome. We can import them directly using
@Import

  • Role: used to import other configuration classes
  • Attributes:
  • value: Bytecode used to specify additional configuration classes.
  • When we use the Import annotation, the class with the Import annotation is the parent configuration class, and all the imports are sub-configuration classes
@Import("jdbcConfig.class")

@PropertySource

  • Role: used to specify the location of the properties file
    Attributes:
  • value: Specifies the name and path of the file.
  • Keyword: classpath, means under the class path
@PropertySource("classpath:jdbcConfig.properties")

Annotations used in Junit tests

Every test method in our test needs to get the container to get the object is too troublesome, but Junit itself does not know whether we use spring, and it is said to help us create the spring container, but Junit provided me with annotations to replace itself The container itself runs

@Runwith

Replace the original runner

@RunWith(SpringJUnit4ClassRunner.class)

@ContexConfiguration

@ContextConfiguration annotation:

  • locations attribute: used to specify the location of the configuration file. If it is in the class path, you need to use classpath: to indicate
  • classes attribute: classes used to specify annotations. When xml configuration is not used, this attribute needs to be used to specify the location of the annotation class.
@ContextConfiguration(classes = SpringConfig.class)

Note: The version of Junit required here must be 4.12 and above

Guess you like

Origin blog.csdn.net/qq_44660367/article/details/108153452