IOC容器中IOC操作之Bean管理
Bean管理的多种属性注入方式
1、p名称空间注入
1.1 使用p名称空间注入,可以简化基于xml配置方式
(1)添加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 http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
(2)进行属性注入,在bean标签里面进行操作
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--2 set方法注入属性-->
<bean id="book" class="com.example.spring5.domain.Book" p:bname="盒饭" p:bauthor="阿姨">
</bean>
</beans>
2、xml注入其他类型属性
2.1 字面量
(1)null值
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--2 set方法注入属性-->
<bean id="book" class="com.example.spring5.domain.Book">
<!--使用property完成属性注入
name: 类里面属性名称
value: 向属性注入的值
-->
<property name="bname" value="西瓜"></property>
<property name="bauthor" value="瓜农"></property>
<!--null值-->
<property name="address">
<null/>
</property>
</bean>
</beans>
(2)属性值包含特殊符号
<!--属性值包含特殊符号
1 把<>进行转义
2 把带特殊符号内容写到CDATA
-->
<property name="address">
<value><![CDATA[南瓜]]></value>
</property>
2.2 注入属性-外部bean
(1)创建两个类service类和dao类
UserDao
package com.example.spring5.dao;
public interface UserDao {
public void update();
}
UserDaoImpl
package com.example.spring5.dao;
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("dao update........");
}
}
(2)在service调用dao里面的方法
package com.example.spring5.service;
import com.example.spring5.dao.UserDao;
public class UserService {
//创建UserDao类型属性,生成set方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add.........");
userDao.update();
}
}
(3)在spring配置文件中进行配置
<?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">
<!--1 service和dao对象创建-->
<bean id="userService" name="com.example.spring5.service.UserService">
<!--注入userDao对象
name属性值:类里面属性名称
ref属性:创建userDao对象bean标签id值
-->
<property name="userDao" ref="userDaoImpl"/>
</bean>
<bean id="userDaoImpl" class="com.example.spring5.dao.UserDaoImpl"/>
</beans>
3、注入属性-内部bean和级联赋值
(1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门
部门是一,员工是多
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
Dept类
package com.example.spring5.bean;
//部门类
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
}
Emp类
package com.example.spring5.bean;
//员工类
public class Emp {
private String ename;
private String gender;
//员工属于某一个部门,使用对象形式表示
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
}
(3)在spring配置文件中进行配置
<?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="emp" class="com.example.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="com.example.spring5.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
</beans>
(4)测试样例
package com.example.spring5;
import com.example.spring5.bean.Emp;
import com.example.spring5.domain.Orders;
import com.example.spring5.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean1.xml")
class OneToMoreTest {
@Test
public void testAdd(){
//1 加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
//2 获取配置创建的对象
Emp emp = context.getBean("emp", Emp.class);
System.out.println(emp);
emp.add();
}
}
4、注入属性-级联赋值
(1)第一种写法
<?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="emp" class="com.example.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"/>
<property name="gender" value="女"/>
<!--级联赋值-->
<property name="dept" ref="dept"/>
</bean>
<bean id="dept" class="com.example.spring5.bean.Dept">
<property name="dname" value="财务部"/>
</bean>
</beans>
(2)第二种写法
先在对应实体类里添加get方法
public Dept getDept() {
return dept;
}
再写对应配置文件
<?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="emp" class="com.example.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"/>
<property name="gender" value="女"/>
<!--级联赋值-->
<property name="dept" ref="dept"/>
<property name="dept.dname" value="技术部"/>
</bean>
<bean id="dept" class="com.example.spring5.bean.Dept">
<property name="dname" value="财务部"/>
</bean>
</beans>