Spring study notes 2 - SpringIOC (DI) Introduction

 

  • IOC (DI) and Spring AOP is the core of the two functions

But now spawned spring boot, spring cloud, springdata ... etc.

  • IOC: Inversion of Control (DI: dependency injection) the same thing the IOC and DI

Inversion of Control: Reverse is: get the object from the way new (created) → get (take).

                   Control is: by the Spring to help us to create responsible for destroying objects, control the lifetime of an object and so on.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://www.springframework.org/schema/beans" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">

//通过name来利用容器获取对象
//class:被管理对象的全包名
<bean name="stu" class="cn.lk.bean.Student"></bean>
</beans>
@Test
	public void Test1() {
		//获取容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	    //通过getBean方法获取配置好的bean对象
		Student stu = (Student)ac.getBean("stu");
	    System.out.println(stu);
	}
  • Since the inversion of control concepts difficult to understand

In a meeting, IOC was renamed DI (dependency injection)

Dependency injection: The assignment values ​​are arranged (injection) variable

Injection mode: set injection mode, injection configuration, injection properties

  • IOC is a programming idea, but also a design pattern . It can be seen as a super plant (factory design pattern simple (involving multi-state) Premium)

No matter what subject you want, you can go directly obtaining container springioc

Therefore ioc operations are divided into two steps: first when storing objects to the ioc ① and ② need to take the assignment from the ioc

 

  • --bean tag attribute xml configuration --scope

= scope Singleton : Singleton (default) project generally use only the attribute value default

              prototype : multiple cases

             request : lifetime of an object request is consistent with the request

              session : the life cycle of session coincides with the object (one session after the release out)

 

  • Injection method:. 1 the SET keyword is injected into the property

() Method of assignment by setXxx, reflected by the underlying implementation.

Reflection: the Java reflection to say is in the operating state, for any one class, we are able to know what this class has methods and properties. For any object, we are able to carry out its methods and properties call. We call this dynamic acquisition object information and call the function object method called reflection.

id: a unique identifier class: the specified type  

property: property class represented by the class name: attribute name value: property value

Injection complex type:

 

Configuration Notes: The register objects to the container

Guess you like

Origin blog.csdn.net/weixin_42153410/article/details/93397284