Spring's preliminary understanding (IOC)

      IOC: The initialization of the previous code class is achieved through new, which is more troublesome. Before the new thing is handed over to the factory class, we directly call the factory data.

      The purpose of IOC is to realize it as a factory.

      Link address: https://spring.io/projects/spring-framework

      Operation document address: https://docs.spring.io/spring/docs/5.2.5.RELEASE/spring-framework-reference/

      Core content: https://docs.spring.io/spring/docs/5.2.5.RELEASE/spring-framework-reference/core.html#spring-core

      Steps for usage:

      1. Build maven project

      2. Introduce spring jar package:

<!-- spring 核心 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.2.5.RELEASE</version>
		</dependency>
		<!-- spring IOC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>5.2.5.RELEASE</version>
		</dependency>
        <!-- spring 环境配置  -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.2.5.RELEASE</version>
		</dependency>

 

 3. Create an application.xml file as a configuration file (under the resource folder)

content:

<?xml version="1.0" encoding="UTF-8"?>
					<beans xmlns="http://www.springframework.org/schema/beans"
						xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
						xsi:schemaLocation="http://www.springframework.org/schema/beans
							https://www.springframework.org/schema/beans/spring-beans.xsd">
					</beans>

Among the constraints, you need to download the file, otherwise you may not be prompted. The file downloaded here is a file called xsd:

3.1 Open the browser and copy the xsd file link, paste it into the address bar, press Enter, save it, and save it in your own folder   

3.2 eclipse-> window-> preferences-> xml-> xml catalog-> click the add button-> click the File System button of Location-> select the xsd file-
> key will be automatically generated or we You can copy the line above the xsd is actually key-> key type select Schema location-> save

3.3 Create a package, then create a class, as a test, create the Student class

3.4 Write a bean tag in xml
                      

  <!-- 
                            id:唯一不为空即可
                            class:一个类的全限定名
                         -->
                        <bean id="stu" class="com.zcy.bean.Student"></bean>

3.5 Test

public static void main(String[] args) {
							ApplicationContext context = new ClassPathXmlApplicationContext(
									"application.xml");
							Student stu = context.getBean(Student.class);
							System.out.println(stu);
						}

Inversion of control (IOC): spring helps us to new data, just like a factory

 

4. Although spring helps us initialize the object, the default is a single case
                    . Configure scope = "prototype" in the bean tag to be multiple cases (generally not to configure, because it reduces efficiency)

5. Set the attribute value of the object:

5.1 Create classes and corresponding properties

public class Student {
						private String name;
						private String sex;
						private Integer age;
					}

5.2 In the configuration file:

<bean id="stu" class="com.zcy.bean.Student">
						这里的property就是设置属性,name就是属性名,value就是属性值
						<property name="name" value="ZCY"></property>
						<property name="sex" value="男"></property>
						<property name="age" value="100"></property>
					</bean>

Here is achieved by calling the set method

6. Set the object attribute value (construction pass parameter): (generally not used this way)

<bean id="stu" class="org.lgs.bean.Student">
						<constructor-arg index="0" value="ZCY"></constructor-arg>
						<constructor-arg index="1" value="男"></constructor-arg>
						<constructor-arg index="2" value="100"></constructor-arg>
					</bean>

7. Set harder properties (Map, List, Object)

<bean id="stu" class="com.zcy.bean.Student">
						<property name="name" value="ZCY"></property>
						<property name="sex" value="男"></property>
						<property name="age" value="100"></property>
						<property name="parents">
							<map>
								<entry key="兄弟" value="左手"></entry>
								<entry key="朋友" value="右手"></entry>
							</map>
							<!-- <list>
								<value>香蕉</value>
								<value>苹果</value>
								<value>榴莲</value>
							</list> -->
						</property>
					</bean>

Object addition:

public class Student {

						private String name;
						private String sex;
						private Integer age;
						private Map<String, String> parents;
						private ClassInfo cinfo;
					}

Let spring create a class class, id is called class 

<bean id="class" class="com.zcy.bean.ClassInfo">
						<property name="cname" value="ZCY2020"></property>
					</bean>
					
					<bean id="stu" class="com.zcy.bean.Student">
						<property name="name" value="ZCY"></property>
						<property name="sex" value="男"></property>
						<property name="age" value="100"></property>
						<property name="parents">
							<map>
								<entry key="兄弟" value="左手"></entry>
								<entry key="朋友" value="右手"></entry>
							</map>
							<!-- <list>
								<value>香蕉</value>
								<value>苹果</value>
								<value>榴莲</value>
							</list> -->
						</property>
						<!-- 这里的ref是引用的意思,调用其他spring自己new出来的对象  -->
						<property name="cinfo" ref="class"></property>
					</bean>

 Injection (DI): Set any type of parameter through the set or constructor, to the corresponding object properties

 

8. Property file configuration:

The parameters are written in the properties file, for example: database

8.1. Create a property file, for example: config.properties

8.2. Write parameters in the property file: username = ZCY

8.3. Call the property file on the xml side:

8.3.1 First, you need to add constraints:

<beans xmlns="http://www.springframework.org/schema/beans"
							xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
							xmlns:util="http://www.springframework.org/schema/util"
							xsi:schemaLocation="http://www.springframework.org/schema/beans
								https://www.springframework.org/schema/beans/spring-beans.xsd
								http://www.springframework.org/schema/util
								https://www.springframework.org/schema/util/spring-util.xsd">

8.3.2 Then use the attribute tag: classpath: tell the system that this file is in the src folder

<util:properties id="config" location="classpath:config.properties"></util:properties>

8.3.3 The property file is used in the corresponding bean, through # {property id. Property name}

 <bean id="stu" class="com.zcy.bean.Student">
						<property name="name" value="#{config.username}"></property>
						<property name="sex" value="男"></property>
						<property name="age" value="100"></property>
						<property name="parents">
							<map>
								<entry key="兄弟" value="左手"></entry>
								<entry key="朋友" value="右手"></entry>
							</map>
						</property>
						<!-- 这里的ref是引用的意思,调用其他spring自己new出来的对象  -->
						<property name="cinfo" ref="class"></property>
					                </bean>

 

It is still too difficult to configure 9.xml. If the service has 100 java files, do you configure each bean?

No, we can use the scanning method:

9.1 Configuration constraints:

<beans xmlns="http://www.springframework.org/schema/beans"
							xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
							xmlns:util="http://www.springframework.org/schema/util"
							xmlns:context="http://www.springframework.org/schema/context"
							xsi:schemaLocation="http://www.springframework.org/schema/beans
								https://www.springframework.org/schema/beans/spring-beans.xsd
								http://www.springframework.org/schema/util
								https://www.springframework.org/schema/util/spring-util.xsd
								http://www.springframework.org/schema/context
								https://www.springframework.org/schema/context/spring-context.xsd">

9.2 Configure Scan

Scan all the classes (Java files) under the com.zcy.bean package and make all the annotated classes new

<context:component-scan base-package=com.zcy.bean"></context:component-scan>

9.3 Add annotations to the classes that require new

@Component
						public class Student {}

9.4 Property injection:

Method 1:

@Value("ZCY")
private String name;

Method 2:

@Value("#{config.username}")
private String name;

Method 3: Important

@Resource
private ClassInfo cinfo;

Annotations are the most common practice in enterprises because they are easy to use

Published 173 original articles · Like 92 · Visits 10,000+

Guess you like

Origin blog.csdn.net/weixin_42995083/article/details/105459062