Before reading this article, you can first refer to the article " Understanding Dependency Injection and Control Inversion in Spring " to learn about the relevant content of Dependency Injection and Control Inversion.
Three ways of dependency injection
Property injection, inject the property value of the bean through the setter method or the dependent object construct injection factory method injection (seldom used)
example
Here we use spring-4.3.2, maven configuration file
<dependency> <groupid>org.springframework</groupid> spring-core</artifactid> <version>${org.springframework-version}</version> <exclusions> <exclusion> <groupid>commons-logging</groupid> commons-logging</artifactid> </exclusion> </exclusions></dependency><dependency> <groupid>org.springframework</groupid> spring-beans</artifactid> <version>${org.springframework-version}</version></dependency><dependency> <groupid>org.springframework</groupid> spring-aop</artifactid> <version>${org.springframework-version}</version></dependency><dependency> <groupid>org.springframework</groupid> spring-context</artifactid> <version>${org.springframework-version}</version></dependency><dependency> <groupid>commons-logging</groupid> commons-logging</artifactid> <version>1.2</version></dependency><!-- Junit --><dependency> <groupid>junit</groupid> junit</artifactid> <version>3.8.1</version> <scope>test</scope></dependency>applicationContext.xml configuration file
<!--?xml version="1.0" encoding="UTF-8"?--><beans xmlns="https://www.springframework.org/schema/beans" xmlns:p="https://www.springframework.org/schema/p" xmlns:util="https://www.springframework.org/schema/util" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd https://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> <!-- Configure bean id: Identify the bean object class: the full class name of the bean in the container, and create a bean in the IOC container through reflection, so the bean class must have a parameter constructor--> <bean id="helloWorld"> <property name="name" value="crystal"></property> </bean> <!-- Configuring beans through constructor methods allows you to specify the location and type of parameters to distinguish overloaded constructors --> <bean id="car"> <constructor-arg index="0" value="BENCHI"></constructor-arg> <constructor-arg index="1" type="double" value="200000.0"></constructor-arg> </bean> <bean id="car1"> <!-- If the literal contains special characters, use <![CDATA[]]> to wrap the attribute value with the value child nodes--> <constructor-arg type="java.lang.String"> <value><!--[CDATA[<shanghai-->]]></value> </constructor-arg> <constructor-arg index="1" type="int" value="200"></constructor-arg> </bean> <bean id="person"> <property name="name" value="Crystal"></property> <property name="age" value="20"></property> <!-- You can use ref to create relationships between references--> <!-- <property name="car" ref="car"></property> --> <!-- <property name="car"> <ref bean="car2"/> </property> --> <!-- <property name="car"> <bean> <constructor-arg value="changanFute"></constructor-arg> <constructor-arg value="3000000"></constructor-arg> <constructor-arg value="240"></constructor-arg> </bean> </property> --> <!-- Test assignment null --> <!-- <property name="car"><null/></property> --> <property name="car" ref="car1"></property> <!-- Assign values to cascading properties. Note: properties need to be initialized before they can be assigned values to cascading properties. They are different from structs2--> <property name="car.price" value="400000"></property> </bean> <!-- Test configuration collection properties--> <bean id="person3"> <property name="name" value="barry"></property> <property name="age" value="21"></property> <property name="cars"> <list> <ref bean="car"> <ref bean="car1"> <bean> <constructor-arg value="changanFute"></constructor-arg> <constructor-arg value="3000000"></constructor-arg> <constructor-arg value="240"></constructor-arg> </bean> </ref></ref></list> </property> </bean> <!-- Configure the property value of the Map--> <bean id="newPerson"> <property name="name" value="lina"></property> <property name="age" value="22"></property> <property name="cars"> <!-- Use map node and map entry child node to configure member variables of Map type --><map> <entry key="AA" value-ref="car"></entry> <entry key="BB" value-ref="car1"></entry></map> </property> </bean> <!-- Configure Properties property value --> <bean id="dataSource"> <property name="properties"> <!-- Use props and prop children to assign values to Properties properties --> <props> <prop key="user">root</prop> <prop key="password">1234</prop> <prop key="jdbcUrl">jdbc:mysql://test</prop> <prop key="jdbcDriver">com.mysql.jdbc.Driver</prop> </props> </property> </bean> <!-- Configure a singleton collection bean for reference by multiple beans. You need to import the util namespace--> <util:list id="cars"> <ref bean="car"> <ref bean="car1"> </ref></ref></util:list> <bean id="person4"> <property name="name" value="Jackie"></property> <property name="age" value="30"></property> <property name="cars" ref="cars"></property> </bean> <!-- Assigning values to bean properties through the p namespace, you need to import the p namespace first, which is more concise than traditional configuration methods--> <bean id="person5" p:age="32" p:cars-ref="cars" p:name="Queue"></bean></beans>
1. Below are simple attribute injection and construct injection test classes
Car.java
package com.spring.test;public class Car {private String name;private int maxSpeed;private double price;public Car() {}public Car(String name, double price) {this.name = name;this.price = price;}public Car(String name, int maxSpeed) {this.name = name;this.maxSpeed = maxSpeed;}public Car(String name, double price, int maxSpeed) {this.name = name;this.price = price;this.maxSpeed = maxSpeed;}public void setPrice(double price) {this.price = price;}@Override public String toString() {return "Car [name:" + name + ", price:" + price + ", maxSpeed:" + maxSpeed + "]";}}HelloWorld.java
package com.spring.test;public class HelloWorld {private String name;public HelloWorld() {System.out.println("HelloWorld constructor...");}public String getName() {return name;}public void setName(String name) {System.out.println("setName:" + name);this.name = name;}@Override public String toString() {return "hello," + name;}}Person.java
package com.spring.test;public class Person {private String name;private int age;public Car car;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}@Override public String toString() {return "Person: [name=" + name + ", age=" + age + ", car=" + car + "]";}}Main.java
package com.spring.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {HelloWorld hello = new HelloWorld();hello.setName("barry");System.out.println("print:"+ hello + "/n");// Load Spring configuration file/** * Load Spring configuration file* ApplicationContext is an IOC container, which has two main implementation classes (ClassPathXmlApplicationContext and FileSystemXmlApplicationContext) * ApplicationContext instantiates all singletons' beans when initializing the context */ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//HelloWorld hello1 = (HelloWorld) context.getBean("helloWorld"); // Get the bean object through id HelloWorld hello1 = context.getBean(HelloWorld.class);// Get the bean object by type (requires that there can only be one object of this type in the IOC container) System.out.println(hello1);}@Test public void testContructor() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Car car = (Car) context.getBean("car");// Get the bean object by type (requires that there can only be one object of this type in the IOC container) Car car1 = (Car) context.getBean("car1");System.out.println(car);System.out.println(car1);Person person = (Person) context.getBean("person");System.out.println(person);}} 2. Below is the test class for the collection
NewPerson.java
package com.spring.test.collections;import java.util.Map;import com.spring.test.Car;public class NewPerson {private String name;private int age;private Map<string, car=""> cars;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Map<string, car=""> getCars() {return cars;}public void setCars(Map<string, car=""> cars) {this.cars = cars;}@Override public String toString() {return "Person: [name=" + name + ", age=" + age + ", cars=" + cars + "]";}}Person.java
package com.spring.test.collections;import java.util.List;import com.spring.test.Car;public class Person {private String name;private int age;public List<car> cars;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public List<car> getCars() {return cars;}public void setCars(List<car> cars) {this.cars = cars;}@Override public String toString() {return "Person: [name=" + name + ", age=" + age + ", cars=" + cars + "]";}}DataSource.java
package com.spring.test.collections;import java.util.Properties;public class DataSource {private Properties properties;public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}@Override public String toString() {return "DataSource: [properties:" + properties + "]";}}Main.java
package com.spring.test.collections;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {@Test public void testCollections() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Person person = (Person) context.getBean("person3");System.out.println(person);NewPerson newPerson = (NewPerson) context.getBean("newPerson");System.out.println(newPerson);DataSource dataSource = (DataSource) context.getBean("dataSource");System.out.println(dataSource);Person person4 = (Person) context.getBean("person4");System.out.println(person4);Person person5 = (Person) context.getBean("person5");System.out.println(person5);}}Summarize
The above is all about the example of Spring framework dependency injection method in this article, I hope it will be helpful to everyone. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!