Preparation
bean: An object used in multiple places.
spring and spring boot: spring and spring boot projects; spring is equivalent to the old version of
spring boot, and the essence is still a spring project; in order to facilitate the construction of spring projects; it is easier to operate
spring (Core) core project; it is a common project. It is not a spring web project
Requirements: MySQL5.7 version; idea 2021 version (because related plug-ins will be charged after 2022 version)
Preparation:
1: Create a Maven project; the saved path cannot have Chinese
2: Add dependency
maven and download it on an international site It is easy to go wrong; but we can configure domestic sources; domestic mirror websites. The default is to download from foreign websites; there is also a maximum time limit for downloading. For example, if your network is unblocked, it can only be 1kb per second; the next one is 1024kb; suppose the maximum timeout period is 60 seconds; if it fails to download after 60 seconds, it will report an error.
Configure domestic sources: idea must be configured in both places
There are two steps here:
if (there is a settings.xml file in the above folder); you need to modify the configuration yourself; use vscode to open; in this way, it is easy to see which are comments with color distinction. You don't have to pay attention to these.
What is not configured is the following effect:
copy this content in the past
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
else: The above folder does not have this file; directly copy the ready-made version. No further configuration is required.
Configure the same operation above again:
then maven refreshes; that is, we manually added a configuration in pom.mxl before; we also need to trigger the download after refreshing here,
but there is a problem here; it will only download unfinished jar packages . If you do not configure the domestic source, the download of some jar packages fails; halfway through the download and then fails; this kind of jar package will not be re-downloaded; it depends on whether you have it. So we clear all the previous jar packages in the local warehouse before refreshing; next to the place we just configured. (Delete everything here; then refresh maven)
Add spring dependency
spring dependency
Use 5.x version; 5.3.26; 6.x version requires jdk17 or above. Create the dependencies tag in pom.mxl; paste it in. Then refresh maven again.
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.26</version>
</dependency>
Download one; give a few more for free; context depends. The above steps are set up; set up once; there is no need to set up again in the future.
store bean
Create a startup class: Create a normal class in java normally; there is a main method. The operation of storing beans and fetching beans is here.
Create a configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:content="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--名字望文生义即可;这段配置文件保存下来-->
</beans>
Inject object:
id attribute: give this object a name; then when you take it, you also need the name to take it. The
class is the class name; if your class is under a certain package; here must be the path; package name + class name
Note: The fetching and saving names must be the same; the saved name must not be repeated; the definition of the bean is not found; the name of the xml or bean is correct
Take bean:
ApplicationContext: It is an interface; currently it can be considered as representing spring itself.
It can also be used: BeanFactory as the spring context;
BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
The difference between the two:
suppose to add another teacher class: there is a construction method in it; it is used to verify the object There is no create. We store this teacher object in spring,
The difference:
the code is executed to this line; execute the code in your xml; it will create all the objects; it is actually storing all the objects in the spring.
The second way of writing: none of the objects are created; only whoever is used can create them. Lazy loading; this way the efficiency is really slow (how can it be created when it is needed) getBean will load the corresponding Bean. Lazy man mode; above is hungry man mode. This is an object that does not create a teacher class.
The first type: memory cost; advantages; one-time loading; subsequent reading will be very fast.
The second type: save memory; load only when calling; performance is relatively low. (Limited memory in the past; limited resources are more suitable)
In terms of relationship: ApplicationContext is a subclass of BeanFactory; it is different in function; the function of the parent class is smaller than that of the subclass.
Three ways to get beans
There is a problem with the above method of obtaining beans; when fetching, it must be forcibly converted; if it is null, it will report an error.
1: Acquisition of the name; the way it was written just now.
2: Obtain according to the type; no mandatory conversion is required; this type is returned
but this method is used; we will store a type multiple times (this kind of storage is very common; because it is equivalent to storing multiple objects); this way can be used directly This method throws; No unique bean found constraint exception; it doesn't know which one to use.
3: Acquire together according to type and name. This will find the only one. This is recommended.