IoC and DI of Spring

Preface

This article summarizes the following understanding of Servlet, Filter, ServletContextListener through the previous articles.

Now learn to develop the SpringMVC framework based on Servlet technology.Understand the Spring framework before learning Spring MVC.

To understand Spring, you need to start with what Spring is, what Spring can do, what's the benefit of using Spring, and how Spring should use it.

Personal reference is the Spring series articles that Kaitao taught me.

What is Spring

Spring is an open source framework for JavaEE, which simplifies enterprise application development by implementing IoC/DI for container management and manages the dependencies of various modules.

IoC (Inversion of Control) controls inversion.Here you need to understand who controls who and what is reversal.

public class User{
	private UserInfo userInfo;
	public getUserName(){
		return userInfo.name;
	}
}

In the code above, there is a getUserName method that requires a name in the user information.Errors can occur if there is no UserInfo instance program.User now relies on UserInfo, which controls User.To use User properly, you must manually build UserInfo and assign it to User.

Based on the concept of IoC, User and UserInfo are created by class management containers and assigned to User if User is detected to depend on UserInfo.Examples are as follows:

public class IoCBeanFactory{
	public static Map<String, Object> beans = new HashMap();
	public void init(){
		beanLoad();
	}
	
	public void beanLoad(){
		beans.put("user", new User());
		beans.put("userInfo", new UserInfo());
	}
	
	public void dependencyInjection(){
		//Check User Dependency UserInfo
		User u = (User)beans.get("user");
		
		//set a property
		UserInfo uinfo = (UserInfo)beans.get("userInfo");
		u.setUserInfo(uinfo);
	}
}

User s acquired through IoCBeanFactory don't have to worry about userInfo.This is control reversal, which reverses the case where the relying party builds the relying party.Instances created by this class, checking the attribute dependency of an instance and setting corresponding attributes, are the implementation of the DI (dependency injection) concept.

What Spring can do

In addition to managing beans, Spring also manages beans through AOP facets, JDBC that operates on databases, integrates common persistence layer ORM s, and weib manager that implements MVC

What are the benefits of using Spring and how to use it

As a Demo

Add application.xml

<?xml version="1.0" encoding="utf-8" ?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xmlns:context="http://www.springframework.org/schema/context"  
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
 <bean id="hello" class="yongy.javaspring.service.impl.HelloWorldImpl"></bean>  
</beans>

With a simple xml configuration, you can add a bean to the ioc container and get the instance only by getBean when needed, reducing the need to build the instance manually.

public interface HelloWorld {  
  
  void sayHello();  
  
}

public class HelloWorldImpl implements HelloWorld {
    @Override
    public void sayHello() {
        System.out.println("HelloWorld!");
    }
}


public class JavaSpringApplicationTest {

    @Test
    public void helloWorld(){
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        HelloWorld hello = context.getBean("hello", HelloWorld.class);
        hello.sayHello();
    }

}

Keywords: Programming Spring xml JavaEE Attribute

Added by plasmahba on Wed, 04 Mar 2020 18:06:32 +0200