Three commonly used implementation classes of ApplicationContext
ClassPathXmlApplicationContext: It can load configuration files under the classpath, and the configuration files must be under the classpath. If you are away, you can't load it. (More commonly used)
FileSystemXmlApplicationContext: It can load configuration files in any path of the disk (must have access rights)
AnnotationConfigApplicationContext: It is used to read annotations to create a container.
Problems caused by the two interfaces of the core container
ApplicationContext: (Singleton object is applicable)
When it builds the core container, its strategy for creating objects is to use immediate loading. In other words, as soon as you finish reading the configuration file, you will create the objects configured in the configuration file.
BeanFactory: (applicable to multiple objects)
When it builds the core container, its strategy for creating objects is to use lazy loading. In other words, when is the object obtained based on the Id, and when is the object actually created.
Dependency injection in spring
Dependency Injection: Dependency Injection
The role of IOC: reduce the coupling between programs (dependency)
Dependency management: all will be maintained by spring in the future
In the current class, objects of other classes are used, provided by spring. We only need to explain the maintenance of dependencies in the configuration file, which is called dependency injection.
Dependency injection
There are three types of data that can be injected
Basic types and String
Other bean types (beans configured in configuration files or annotations)
Complex type/collection type
Ways of injection: there are three
Use the constructor to provide
Use label: constructor-arg
Where the label appears: inside the bean label
Attributes in the label:
type: used to specify the data type of the injected data, which is also the type of one or some parameters in the constructor
index: Used to assign the data to be injected to the parameter at the specified index position in the constructor. The position of the index starts from 0
name: Used to assign the parameter assignment of the specified name in the constructor (commonly used)
value: used to provide basic type and String type data
ref: used to specify other bean type data. It refers to the bean object that appeared in the spring IOC core container.
Advantages: When obtaining a bean object, injecting data is a necessary operation, otherwise the object cannot be created successfully.
Disadvantages: The instantiation method of the bean object has been changed, so that when we create the object, if the data is not used, we must also provide it.
Construct a method with parameters in UserServiceImp
private String string;private Integer integer;private Date date;publicUserServiceImp(String string, Integer integer, Date date){
this.string = string;this.integer = integer;this.date = date;}
Use the set method to provide (more commonly used)
Involved tags: property
Appearance position: inside the bean tag
Label attributes
name: Used to assign a value to the parameter with the specified name in the constructor (commonly used).
value: used to provide basic type and String type data.
ref: used to specify other bean type data. It refers to the bean object that appeared in the spring IOC core container.
Advantages: There are no clear restrictions when creating objects, you can directly use the default constructor.
Disadvantages: If a member must have a value, the set method cannot guarantee certain injection, and the set method may not be executed when the object is obtained.
Tell Spring the package to scan when creating the container. The tags required for configuration are not in the constraints of beans, but in a name space and constraints named context.
Used to create objects: their role is the same as a <bean> tag written in the xml configuration file
Component
Role: used to store the current class object in spring
Attributes:
value: used to specify the id of the bean. When we do not write, its default value is the current class name, and the first letter is changed to lowercase
Controller: Generally used in the presentation layer
Service: generally used in the business layer
Repository: generally used in the persistence layer
The functions and attribute values of the above three annotations are exactly the same as those of Component. The three of them are that the Spring framework provides us with clear annotations for the use of the three layers, which makes our three-layer objects more clear.
Used to inject data: their role is the same as a <property> tag written in the xml configuration file
Autowired
Function: Automatically inject according to the type, as long as there is only one bean object type in the container that matches the type of the variable to be injected, the injection can be successful. If there is no bean type in the IOC container that matches the type of the variable to be injected, an error is reported. If there are multiple matching types in the IOC container, the injection will be performed according to the variable name. If there is no matching variable name, the injection will fail.
Appearance position: it can be in variable or method
Details: When using annotation injection, the set method is not necessary
Qualifier
Function: Inject according to the name on the basis of injecting in the class. It cannot be used alone when injecting class members. But when injecting method parameters, you can
Attributes
value: used to specify the id of the injected bean.
Resource
Function: Inject directly according to the id of the bean. It can be used alone
Attributes
name: used to specify the id of the bean
The above three injections can only inject data of other bean types, while the basic types and String types cannot be implemented using the above annotations. In addition, the injection of collection types can only be achieved through xml.
value
Role: used to inject basic type and String type data
Attributes:
value: used to specify the value of the data, it can use spEL in spring (that is, spring el expression)
How to write spEL: ${Expression}
Used to change the scope: their role is the same as a <scope> tag written in the xml configuration file
scope:
Role: used to specify the scope of the bean
Attribute: value: specify the range of values (singleton, prototype)
Related to the life cycle: their role is the same as using the init-method and destroy-method tags in the xml configuration file
PerDestroy: used to specify the destruction method
PostConstruct: used to specify the initialization method
@Autowired注释写在方法上方报错:Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userService’: Injection of autowired dependencies failed; nested exception is java.lang.NullPointerException
Solution: write @Autowired annotation above the variable