1. Setter injection

(1) First add the set method to the object you want to create:

(2) Configure the related properties in the Spring configuration file.

In the Spring configuration file, use the bean tag and add the corresponding properties to the tag to create the object. Class: the full path of the object to be created. The default is to execute the parameterless constructor to create the object<bean id = "book" class="Full path to book class">-- name: attribute in the class to be created -- value: specific value to be injected into the attribute<property name = "bname"  value = "Red chamber"></property>
    <property name = "bauthor" value= "Old cao"></property>Here we are assigning basic attributes and strings not collections</bean>1. Add the p namespace to the configuration file<beans xmlns:p="http://www.springframework.org/schema/p">2. Perform attribute injection<bean id = "book" class="Full path to book class" p:banme = "value"  p:author = "value">
</bean>

Copy the code

2. Complete by parameter construction

(1) Create class, define attribute, create parameter constructor

(2) Configure the XML file

<bean id = "order" class = "Order class full path">
		<constructor-org name = "Attribute Name 1" value= "Attribute value"></constructor-org>
		<constructor-org name = "Attribute Name 2" value= "Attribute value"></constructor-org>
</bean>
Copy the code

Inject other properties (literals, collections, objects)

(1) Literal

1. A null value

<property name = "address">-- If you want to assign the address attribute to null<null/>
</property>
Copy the code

Attribute contains special symbols (using escape or CDATA)

<property name = "address">-- If you want to assign the address attribute to null<value><! [CDATA[nanjing]]></value>
</property>
Copy the code

(2) Inject the external bean

① Create two classes UserService and UserDao

2. Configure the XML file

We need to create two objects: UserService and UserDao. Since UserDao is the interface, we use its implementation class<bean id = "userService" calss = "Full path of userService class">
	<property name = "userDao" ref = "userDaoImpl"></property>
</bean>

<bean id = "userDaoImpl" calss = "Full path of userDaoImpl class">
</bean>
Copy the code

(3) Inject internal beans and cascade assignments

<bean id = "userService" class="com.glp.UserService">
	<property name = "userDao">
		<bean id = "userDao" class="com.glp.UserDao">
			<property name = "username" value="glp"></property>
		</bean>
	</property>
</bean>
Copy the code

Cascading assignment (unlike external beans, which are introduced with attributes set to the external Bean.)

<bean id = "userService" class="com.glp.UserService">
	<property name = "userDao" ref="userDao"></property>
</bean>

<bean id = "userDao" class="com.glp.UserDao">
	<property name = "username" value="glp"></property>
</bean>
Copy the code

(4) Injection collection

① Inject the array type attribute

<bean id = "stu" class = "Full path of the class to be created">
	<property name = "Course is an array type">
		<array>
			<value>java</value>
			<value>oracle</value>
		</array>
	</property>
</bean>
Copy the code

Insert the list collection

<bean id = "stu" class = "Full path of the class to be created">
	<property name = "Course is a list">
		<list>
			<value>java</value>
			<value>oracle</value>
		</list>
	</property>
</bean>
Copy the code

③ Inject the map collection type

<bean id = "stu" class = "Full path of the class to be created">
	<property name = "Course is map type">
		<map>
			<entry key = "" value = ""></entry>
			<entry key = "" value = ""></entry>
		</map>
	</property>
</bean>
Copy the code

(4) set collection

<bean id = "stu" class = "Full path of the class to be created">
	<property name = "Course is a list">
		<set>
			<value>java</value>
			<value>oracle</value>
		</set>
	</property>
</bean>
Copy the code

4. Two details

The types in collections are objects

In fact, and the introduction of the object in front of the same, to learn how to extrapolate oh<bean id = "stu" class = "Full path of the class to be created">
	<property name = "Course is a list">
		<list>
			<ref bean = "course1"></value>
			<ref bean = ""></value>
		</list>
	</property>
</bean>

<bean id = "course1" class = "Full path of the class to be created">
	<property name = "" value = ""></property>
</bean>
Copy the code

2. Make collections available to other classes

<util:list id = "bookList">
	<value>1</value>
	<value>2</value>
	<value>3</value>
</util>
<bean id = "course1" class = "Full path of the class to be created">
	<property name = "list" ref= "bookList"></property>
</bean>
Copy the code

5. Plain beans and factory beans

1. Common: What type is defined and what type is returned

The <bean class=""></bean> class type in the XML file, defines what type it generates when you call it using getBean.Copy the code

2. Factory: defined returns are different

Create a class that acts as a FactoryBean, a time interface FactoryBean

2, implement the interface inside the method, in the implementation of the method to define the return bean type,

So we’re going to return Course