Spring 03 dependency injection

Dependency Injection: Dependency Injection. It is the concrete implementation of ioc, the core of spring framework.

When our program is written, we give the creation of objects to spring through inversion of control, but it is impossible to have no dependency in the code.

ioc decoupling only reduces their dependency, but does not eliminate it. For example, our business layer still calls methods of the persistence layer.

After using spring, the dependency between business layer and persistence layer is maintained by spring.

Simply put, it's waiting for the framework to pass the persistence layer objects into the business layer, instead of getting them ourselves.

Constructor Inject

Use the constructor in the class to assign values to member variables. Note that assignment is not done by ourselves, but by configuration
Let the spring framework inject for us.

/**
*/
public class AccountServiceImpl implements IAccountService {
	private String name;
	private Integer age;
	private Date birthday;
	public AccountServiceImpl(String name, Integer age, Date birthday) {
		this.name = name;
		this.age = age;
		this.birthday = birthday; }
	@Override
	public void saveAccount() {
		System.out.println(name+","+age+","+birthday);
	}
}
<! -- use constructor to pass value to property in service
 Requirement:
	Class needs to provide a constructor for the corresponding parameter list.
Label involved:
constructor-arg
	Properties:
		Index: Specifies the index position of the parameter in the constructor parameter list
		Type: Specifies the data type of the parameter in the constructor
		Name: Specifies the name of the parameter in the constructor to assign to
		=======The above three are for whom to assign value, and the following two are for what value to assign==============
		value: the values it can assign are basic data type and String type
		ref: the value it can assign is another bean type, that is, it must be a bean configured in the configuration file
--> 
< bean id = "accountservice" class = "com. Itheima. Service. Impl. Accountserviceimpl" >

</bean> <bean id="now" class="java.util.Date"></bean>

set method injection

Provide set methods in the class that need to inject members

public class AccountServiceImpl implements IAccountService {
	private String name;
	private Integer age;
	private Date birthday;
	public void setName(String name) {
		this.name = name; 
	}
	public void setAge(Integer age) {
		this.age = age; 
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday; 
	}
	@Override
	public void saveAccount() {
		System.out.println(name+","+age+","+birthday);
	} 
}
<! -- passing values to the properties in the bean through the configuration file: using the set method
 Label involved:
	property
	Properties:
		name: looking for the part after the set method in the class
		ref: assigning a property is of another bean type
		value: assigning a property is of basic data type and string type
 In actual development, this method is used more.
--> 
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
	<property name="name" value="test"></property> 
	<property name="age" value="21"></property> 
	<property name="birthday" ref="now"></property>
</bean> 

<bean id="now" class="java.util.Date"></bean>

Injection set properties

Passing values to the set members in a class is also the way of set method injection, except that the data types of variables are all sets.
Let's talk about injecting array, List,Set,Map,Properties.

public class AccountServiceImpl implements IAccountService {
	private String[] myStrs;
	private List<String> myList;
	private Set<String> mySet;
	private Map<String,String> myMap;
	private Properties myProps;
	public void setMyStrs(String[] myStrs) {
		this.myStrs = myStrs; 
	}
	public void setMyList(List<String> myList) {
		this.myList = myList; 
	}
	public void setMySet(Set<String> mySet) {
		this.mySet = mySet; 
	}
	public void setMyMap(Map<String, String> myMap) {
		this.myMap = myMap; 
	}
	public void setMyProps(Properties myProps) {
		this.myProps = myProps; 
	}
	@Override
	public void saveAccount() {
	System.out.println(Arrays.toString(myStrs));
	System.out.println(myList);
	System.out.println(mySet);
	System.out.println(myMap);
	System.out.println(myProps);
	} 
}
}
<!-- Inject collection data
List Structural:
	array,list,set
Map Structural
	map,entry,props,prop
--> 
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<!-- When injecting set data, labels can be interchanged as long as the structure is the same -->
<!-- Inject data into an array --> 
	<property name="myStrs"> 
		<set>
			<value>AAA</value> 
			<value>BBB</value> 
			<value>CCC</value>
		</set>
	</property>
<!-- injection list Aggregate data --> 
	<property name="myList"> 
		<array> 
			<value>AAA</value> 
			<value>BBB</value> 
			<value>CCC</value>
		</array>
	</property>
<!-- injection set Aggregate data --> 
	<property name="mySet">
		 <list>
		 	<value>AAA</value> 
		 	<value>BBB</value> 
		 	<value>CCC</value>
		</list>
	</property>
<!-- injection Map data --> 
	<property name="myMap"> 
		<props> 
			<prop key="testA">aaa</prop> 
			<prop key="testB">bbb</prop>
		</props>
	</property>
<!-- injection properties data -->
	<property name="myProps"> 
		<map>
			<entry key="testA" value="aaa"></entry> 
			<entry key="testB"> <value>bbb</value>
			</entry>
	</map>
	</property>
</bean>
267 original articles published, 95 praised, 80000 visitors+
Private letter follow

Tags: Spring Java

Posted on Mon, 09 Mar 2020 23:32:56 -0400 by kr9091