The first Spring program (implementation of DI)

Keywords: Java Spring xml Attribute encoding

First, dependency injection: Dependency Injection (DI) and Inversion of Control (IoC), different perspectives but the same concept. First, let's understand that in the traditional way, we use the new way to create an object, which will increase the coupling between the object and the instantiated object, which is not conducive to maintaining the code, which is very painful. In spring framework, object instances are created by spring framework instead. Spring container is responsible for controlling the relationship between programs. This is spring's control inversion. From the perspective of spring container, spring container is responsible for assigning dependent objects to member variables, which is equivalent to injecting the instance object it depends on, which is spring's dependency injection.

Second, the implementation of dependency injection (testing):

Establishing Phone interface (call() method) and Student interface (learn() method)

1 package com.home.homework;
2 public interface Phone 
3 {
4     //define One way
5     public void call();  
6 }
1 package com.home.homework;
2 public interface Student 
3 {
4     //define One learn()Method
5     public void learn();
6 }

(2) Establishing PhoneImpl implementation class (implementing call() method), StudentImpl implementation class (creating name,phone attribute and implementing learn() method)

1 package com.home.homework;
2 public class PhoneImpl implements Phone{
3     //Realization Phone Interface call()Method
4     public void call()
5     {
6         System.out.println("calling .....! ");
7     }
8 }
 1 package com.home.homework;
 2 public class StudentImpl implements Student
 3 {
 4     //define student's name attribute
 5     private String name="Texsin";
 6     //define One phone attribute
 7     private Phone phone;
 8     //Establish setPhone The method can be used to phone assignment
 9     public void setPhone(Phone phone) 
10     {
11         this.phone = phone;
12     }
13     //Realization call Method and Implementation Student Interface learn()Method
14     public void learn()
15     {
16         this.phone.call();
17         System.out.println(name+"is learning via MIX");
18     }
19 }

Establishing Spring configuration file beans.xml

(4) Introducing phone (bean) into student (bean) using setter method in configuration file

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
 6     <bean id="phone" class="com.home.homework.PhoneImpl" />
 7     <bean id="student" class="com.home.homework.StudentImpl">
 8         <property name="phone" ref="phone"></property> 
 9     </bean>
10 </beans>

_Establish TestSpring test class and test it in main() method

 1 package com.home.homework;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class TestSpring {
 7     public static void main(String[] args)
 8     {
 9         //Establish ApplicationContext Interface instance, specify XML Configuration file, initialization instance
10         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/home/homework/beans.xml");
11         //Access through containers student Example
12         Student student=(Student) applicationContext.getBean("student");
13         //Call the instance in learn Method
14         student.learn();
15     }
16 }

Test results

Third, errors encountered in the testing process: interface and method classes are implemented by inheritance or the following errors will be reported (between interfaces and implementation classes through implements)

Posted by polandsprings on Fri, 22 Mar 2019 07:03:53 -0700