一:set方式依赖注入
1. 注入普通字段
//pojo层:
private String name;
<!--spring配置文件-->
<bean id="student" class="com.spring.pojo.Student">
<property name="name" value="jack"/>
</bean>
//测试类
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student)Context.getBean("student");
System.out.println(student.getName());
}
2. 引用类型注入
//实体类
private Address address;
<bean id="address" class="com.spring.pojo.Address"/>
<!--spring配置文件-->
<bean id="student" class="com.spring.pojo.Student">
<!--bean注入-->
<property name="address" ref="address"/>
</bean>
3. 数组类型注入
//数组类型
private String[] books;
<!--spring配置文件-->
<bean id="student" class="com.spring.pojo.Student">
<!-- 数组注入-->
<property name="books">
<array>
<value>数组元素1</value>
<value>数组元素2</value>
<value>数组元素3</value>
</array>
</property>
</bean>
4. List类型注入
//实体类
private List<String> hobbies;
<!--spring配置文件-->
<bean id="student" class="com.spring.pojo.Student">
<!--list注入-->
<property name="hobbies">
<list>
<value>value1</value>
<value>value2</value>
<value>value3</value>
</list>
</property>
</bean>
5. Map类型注入
//实体类
private Map<String,String> card;
<!--spring配置文件-->
<bean id="student" class="com.spring.pojo.Student">
<!--list注入-->
<property name="card">
<map>
<entry key="key01" value="value01"/>
<entry key="key02" value="value02"/>
<entry key="key03" value="value03"/>
</map>
</property>
</bean>
6. Set类型注入
//实体类
private Set<String> games;
<!--spring配置文件-->
<bean id="student" class="com.spring.pojo.Student">
<!--set注入-->
<property name="games">
<set>
<value>set01</value>
</set>
</property>
</bean>
7. null类型注入
//实体类
private Set<String> games;
<!--spring配置文件-->
<bean id="student" class="com.spring.pojo.Student">
<!--空值注入-->
<property name="kong">
<null></null>
</property>
</bean>
8. property类型注入
//实体类
private Properties info;
<!--spring配置文件-->
<bean id="student" class="com.spring.pojo.Student">
<!--property类型注入-->
<property name="info">
<props>
<prop key="admin">adminxxx@xxx</prop>
</props>
</property>
</bean>
二:p命名空间方式依赖注入
使用命名空间需要先导入约束: xmlns:p=“http://www.springframework.org/schema/p”
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
- p命名注入
//实体类
private String name;
private int age;
<!--spring配置文件-->
<bean id="user" class="com.spring.pojo.User" p:name="admin1" p:age="99"/>
三:c命名空间方式依赖注入
约束: xmlns:c=“http://www.springframework.org/schema/c”
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
<!--bean配置,c依赖构造器,pojo中需要有参构造-->
<bean id="user" class="com.spring.pojo.User" c:name="name01" c:age="88"/>