I understand Inversion of Control IOC!



package jk.Test;

import jk.hellow.hellow;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class myTest {
 public static void main(String[] args){
  //Parse the beans.xml file to generate and manage the corresponding bean object
  ApplicationContext context =new ClassPathXmlApplicationContext();
     hellow hellow=(hellow)context.getBean("hellow") ;
     hellow.show();
     //Thinking? Who created the hello object? spring container
     //Thinking? Who created the hello object property? Spring container setting
     // This process is called inversion of control -- content of control: who controls the creation of objects; traditional applications are created by the program itself,
     // but after the spring framework, this object is Created by spring management
     //——Reverse: The forward is a program to create an object, and the reverse is the reverse, which does not create an object itself, but passively receives the object.
     //Summary: In the past, objects were created by the program itself. After using Spring, the program passively receives objects created by Spring.
     //Inversion of control: dependency injection (indpendency injection)
 }
}

<?xml version="1.0" encoding="UTF-8"?>

<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"> <bean class="jk.hellow.hellow" name="hellow"/>

<!-- beans are java objects, created by Spring -->


<property name="name" value="小姜"/>


<!-- Dependency injection here, how to inject it, is injected through the set method! -->


</beans>


package jk.hellow;

public class hellow {
 private String name;
 private void  setName(String name) {
  this.name=name;
 }
 
 public void  show() {
  System.out.println("hellow!"+name);
 }
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326035612&siteId=291194637