What is dependency injection

  • Dependency: The creation of Bean objects depends on the container.
  • Injection: Refers to the resource on which the Bean object depends, which is set and assembled by the container.

There are three types of dependency injection

  1. Basic data types and String types
  2. Other bean types (configured beans in configuration files or annotations)
  3. Complex type/collection type

There are three ways to inject

  1. Constructor injection: use the tag constructor-arg
  • Type: Used to specify the data type to be injected
  • Index: Used to specify that the injected data is assigned to a parameter in the constructor that specifies the index position, starting at 0
  • Name: Used to assign a value to a parameter named in a constructor (recommended)
  • Value: Used to provide basic and string data assignments
  • Ref: Used to specify other bean data types, which are bean objects that have appeared in the Spring container, by reference to the id

Testing:

Entity class

public class User { private String name; private Integer age; private Date date; public User(){ } public User(String name, Integer age, Date date) { this.name = name; this.age = age; this.date = date; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", date=" + date + '}'; }}Copy the code

The Spring core configuration file beans.xml

   <bean id="user" class="com.Dao.User">
        <constructor-arg name="name" value="chenhui"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
        <constructor-arg name="date" ref="date"></constructor-arg>
    </bean>
    <bean id="date" class="java.util.Date"></bean>

Copy the code

The test class:

@ Test public void Test () {/ / by ClassPathXmlApplicationContext loading configuration file will be the javabean object to spring ApplicationContext to management Context=new ClassPathXmlApplicationContext("bean.xml"); User = context.getbean (" User ", user.class); System.out.println(user); }Copy the code

Results:

  1. Injection using the set method (common)
  • Use the tag Property
  • Name: Used to specify the name of the set method to be called at injection time
  • Value: Used to provide basic and string data assignments
  • Ref: Used to specify other bean data types, which are bean objects that have appeared in the Spring container, by reference to the id

The set method must be capitalized with the first letter of the set + attribute. If the attribute is of Boolean type, there is no set method, is.

Testing:

Entity class

public class User { private String name; private Integer age; private Date date; public User(){ } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", date=" + date + '}'; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setDate(Date date) { this.date = date; }}Copy the code

The Spring core configuration file beans.xml

<! <bean id="user" class=" com.dao.user ">< property name="name" value="chenhui"></property> <property name="age" value="22"></property> <property name="date" ref="date"></property> </bean> <bean id="date" class="java.util.Date"></bean>Copy the code

The test class:

@ Test public void Test () {/ / by ClassPathXmlApplicationContext loading configuration file will be the javabean object to spring ApplicationContext to management Context=new ClassPathXmlApplicationContext("bean.xml"); User = context.getbean (" User ", user.class); System.out.println(user); }Copy the code

Results:

Injection of complex/collection types

  • Use to inject tags into the list structure collection:
  • list array set
  • Tags used to inject into the map structured collection:
  • map property
  • The same structure, labels can be interchangeable, that is, the label body can be used interchangeably, no error

Testing:

Entity class:

public class User { private String[] strings; private List<String> list; private Map map; private Properties properties; private Set set; public void setStrings(String[] strings) { this.strings = strings; } public void setList(List<String> list) { this.list = list; } public void setMap(Map map) { this.map = map; } public void setProperties(Properties properties) { this.properties = properties; } public void setSet(Set set) { this.set = set; } public User(){ } public void print(){ System.out.println(Arrays.toString(strings)); System.out.println(list); System.out.println(map); System.out.println(set); System.out.println(properties); }}Copy the code

The Spring core configuration file beans.xml

<bean id="user" class="com.Dao.User"> <property name="strings"> <array> <value>dasd</value> <value>aaaa</value> <value>dfff</value> </array> </property> <property name="list"> <list> <value>dff</value> <value>hjjgj</value> <value>dgfhfgh</value> </list> </property> <property name="set"> <set> <value>1213</value> <value>1fsdf</value> <value>trg</value> </set> </property> <property name="map"> <map> <! - use the first way - > < entry key = "chenhui" value = "123" > < / entry > < entry key = "xie" value = "123" > < / entry > <! -- <entry key=" Chen "> <value>333</value> </entry> </map> </property> <property name="properties"> <props> <prop key="cc">1</prop> <prop key="ww">2</prop> <prop key="dsa">3</prop> </props> </property> </bean>Copy the code

The test class:

@ Test public void Test () {/ / by ClassPathXmlApplicationContext loading configuration file will be the javabean object to spring ApplicationContext to management Context=new ClassPathXmlApplicationContext("bean.xml"); User = context.getbean (" User ", user.class); // Call the method to make collection type injection execute user.print(); }Copy the code

Results:

When the structure is the same, the labels can be interchanged

<bean id="user" class="com.Dao.User"> <property name="strings"> <set> <value>1213</value> <value>1fsdf</value> <value>trg</value> </set> </property> <property name="list"> <array> <value>dasd</value> <value>aaaa</value> <value>dfff</value> </array> </property> <property name="set"> <list> <value>dff</value> <value>hjjgj</value> <value>dgfhfgh</value> </list> </property> <property name="map"> <props> <prop key="cc">1</prop> <prop key="ww">2</prop>  <prop key="dsa">3</prop> </props> </property> <property name="properties"> <map> <! - use the first way - > < entry key = "chenhui" value = "123" > < / entry > < entry key = "xie" value = "123" > < / entry > <! - also can use the second way - > < entry key = "Chen" > < value > 333 < / value > < entry > < / map > < / property > < / bean >Copy the code

Results:

The last

Welcome to pay attention to the public number: the future has light, receive a line of large factory Java interview questions summary + the knowledge points learning thinking guide + a 300 page PDF document Java core knowledge points summary! These are some of the things that the interviewer should ask during the interview. These include basics, Java collections, JVMS, multi-threaded concurrency, Spring principles, microservices, Netty and RPC, Kafka, diaries, design patterns, Java algorithms, databases, Zookeeper, distributed caching, data structures, and more.