Spring's properties configuration file and Spring common annotations

Spring's properties configuration file

problem:

After learning the knowledge of Spring, the resources that need to be managed by Spring are used to tell Spring container objects who and how to manage them in the form of configuration files. But as the number of uses increases, we find that there are more and more tags configured in the configuration file. Every time we create a new project, we only need to modify the database connection parameters of the configured configuration file in the project. Normal use, but because the content of the configuration file is increasing, the readability of the configuration file is getting worse and the modification of the database connection parameter is not easy to find, what should I do?

solve:

Extract the database related parameters into a new file for special configuration, and then obtain the configuration information in the database connection parameter configuration file in the applicationcontext.xml configuration file. To put it bluntly, it is to decouple the database connection parameters from the Spring configuration file.

Implementation:
Spring's property configuration file configures database connection parameters
Use:

① Create a db.properties file under src and declare database connection parameters. Note: The configuration is key-value pair data
Insert picture description here
② Configure the scanning of property files in applicationcontext.xml
Insert picture description here
③ Use ${key name} in the data source bean to get the database connection parameters in the properties file
Insert picture description here

note:

Change the dependency injection between the factory bean and the bean scanned by the mapper into a normal attribute injection, because dependency injection>property file scanning>property injection. If the dependency injection between factory and mapper scans is not changed to attribute injection, the database parameters have not been read from the configuration file into the container, and then the mapper scan will be performed, and the database parameter abnormal error will be reported.
Insert picture description here

Spring's common annotations

① @Component: used on ordinary java classes

② @Service: Used on business layer classes
Function:

Declaring an object of a class as a bean object is equivalent to configuring the bean tag.

note:

It is equivalent to using a parameterless constructor to create an object.

problem:

Only use the @Service annotation, and the effect of A a=new A(); is the same. What is created is a bean object with no initialized data, but in the actual production environment, the Spring container is often needed to help us create one with initialization. Data object (attribute injection|dependency injection)

solve:

The annotation is still used to tell the Spring container object how to initialize the assignment of the object declared by the annotation. Basic type attribute assignment and reference type attribute assignment

achieve:

Dependency injection annotation: @Autowire and @Resource
attribute injection annotation: @Value

③ @Controller: Used on the control layer class, SpringMVC
functions:

Declaring an object of a class as a bean object is equivalent to configuring the bean tag

use:

Used on the controller class of SpringMVC.

④ The
role of @Repository :

Declaring an object of a class as a bean object is equivalent to configuring the bean tag

use:

Used on the database function class. Such as Dao layer object

⑤ @Resource
role:

Replace the dependency injection in the configuration file.

use:

Use it on properties that have dependencies. For example, if there is property B in A, declare it on property B, telling the Spring container object to inject the bean object of B into the bean object of A

note:

This annotation is jdk official annotation, not Spring annotation. It will be injected according to byName first. If not, it will continue to be injected according to byType. If there is none, it will not be injected. And no need to provide get/set methods

⑥ @Autowired
role:

Same as @Resource, it replaces the configuration of dependency injection.

use:

Declare on attributes that require dependency injection

note:

This annotation is officially provided by Spring, and byType is used by default.

⑦ @Value
function:

Used to replace the attribute injection in the configuration file.

use:

Declared on the property, the value is ${"key name"}

Example: @Value(${“name”})

note:

To use this annotation for injection, there is no need to provide get/set methods.

Example:

Insert picture description here