spring Basics - Introduction
This article follows the basic introduction of the previous article and doesn't know what spring is for. You can take a look at the previous article first.
This series of articles all use idea as the development tool and maven warehouse. If you don't know something, you can know it yourself first.
Ioc (Inversion of Controller) concept
The Chinese name control reversal, as the name suggests, is to flip the control right of the object. The original creation, initialization, destruction and other operations of the object are all in the hands of the developer. Now hand over these control rights to the regulatory container, and the developer can free himself from the creation of the object and focus more on the owner. Well, don't talk much nonsense and go directly to the code.
Join dependency
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.18.RELEASE</version> </dependency>
Create a toy class
public class Toy { private int numberNO; private String name; private Double price; //Omit getter setter method }
After creating a child class, let the child get the toy
public class children { private int age; private String name; public void getToy(){ Toy toy = new Toy(); toy.setName("puddle jumper"); toy.setNumberNO(1); toy.setPrice((double)20.0); //Omit getter setter method }
In this way, this toy is always in the hands of only one child, but if another child wants to play, at this time, only adults can buy another one. If adults (developers) don't have time, who will buy the second toy? This is the function of the container. Like servants, children can directly ask servants for it without adults (developers) Spend your energy
After using spring, we can hand over the bean creation, initialization, destruction and other operations to the spring container. You only need to register the bean in the spring container when the project is initialized. When other objects need it again, we don't need to manually go to new.
First, create a spring configuration file in the resource directory. Note that spring dependencies must be added.
Give the toy to the spring container
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.zhiqiu.springdemo01.bean.Toy" id="toy"/> </beans>
Class is the full path of the bean, and id is the unique identifier of the bean. After configuration, we will load the configuration file.
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Toy toy = (Toy) context.getBean("toy"); System.out.println(toy); }
ClassPathXmlApplicationContext will search the configuration file under classpath, or you can use FileSystemXmlApplicationContext to search from the operating system path, that is,
new FileSystemXmlApplicationContext("F:\\work\springdemo01\\applicationContext.xml")
In addition to the above methods, you can also get it through bean.class, as follows`
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Toy toy = context.getBean(Toy.class); System.out.println(toy); }
Note that this method does not require forced conversion, but it also has a disadvantage. When there is only one bean in xml, if there are multiple beans, an error will be reported. Therefore, it is generally recommended to use id or name to obtain the bean instance.
Attribute injection
To learn spring, you must learn xml configuration. In early spring projects, there are many xml configurations. If you don't understand xml configuration and maintain some old projects, it will be a headache.
First, let's look at the first attribute injection method, construction method injection
<bean class="org.zhiqiu.springdemo01.bean.Toy" id="toy"> <constructor-arg name="name" value="robot"/> <constructor-arg name="numberNO" value="007"/> <constructor-arg name="price" value="100.00"/> </bean>
As you can see from the Main method before running, the injected properties will be printed out.
In addition to name injection, there is also index injection in the following table, but this method is not recommended, because if there are many attributes in the class, it will be difficult to remember the quantity order.
Inject according to set method
<bean class="org.zhiqiu.springdemo01.bean.Toy" id="toy"> <property name="name" value="robot"/> <property name="numberNO" value="004"/> <property name="price" value="144.00"/> </bean>
One thing to note here is that the injected attribute name is not the attribute name defined in the class, but the attribute name analyzed through reflection and java internal mechanism.