Chapter 3 Spring4 Dependency Injection

Section 1: Assembling a bean



Section 2: Dependency Injection


IOC (Inversion of Control: InverseofControl), also known as Dependency Injection, is an important object-oriented programming principle to reduce the coupling problem of computer programs, and is also lightweight The core of the Spring framework.

1, property injection;

2, constructor injection; (by type; by index; joint use)

3, factory method injection; (non-static factory, static factory)

4, generic dependency injection; (By the way when Spring4 integrates Hibernate4 )



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

	<bean id="people" class="com.fx.entity.People"></bean>
	<!-- Property injection-->
	<bean id="people2" class="com.fx.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
	</bean>
	
	<!-- Constructor injection-->
	<bean id="people3" class="com.fx.entity.People">
		<constructor-arg type="int" value="2"></constructor-arg>
		<constructor-arg type="String" value="李四"></constructor-arg>
		<constructor-arg type="int" value="22"></constructor-arg>
	</bean>
	
	<bean id="people4" class="com.fx.entity.People">
		<constructor-arg index="0" value="3"></constructor-arg>
		<constructor-arg index="1" value="王五"></constructor-arg>
		<constructor-arg index="2" value="55"></constructor-arg>
	</bean>
	
	<bean id="people5" class="com.fx.entity.People">
		<constructor-arg index="0" type="int" value="4"></constructor-arg>
		<constructor-arg index="1" type="String" value="招六"></constructor-arg>
		<constructor-arg index="2" type="int" value="66"></constructor-arg>
	</bean>
	
	<bean id="peopleFactory" class="com.fx.factory.PeopleFactory"></bean>
	
	<!-- Factory Method Injection-->
	<bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>
  
  	<bean id="people8" class="com.fx.factory.PeopleFactory2" factory-method="createPeople"></bean>
</beans>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327045310&siteId=291194637