基于xml配置文件实现1
一、基于xml方式创建对象
<!--配置User对象创建-->
<bean id="user" class="com.znb.spring5.User"></bean>
-
在spring配置文件中使用bean标签,标签里面添加对应属性,就可以实现对象的创建。
-
bean标签中的常用属性
id属性:唯一标识 class属性:类的全路径
-
创建对象的时候,默认也是去执行无参的构造方法。
二、基于xml方式注入属性
1. set方法注入属性
- 创建实体类
package com.znb.spring5;
public class Book {
private String bookName;
private String bookAuthor;
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void bookAndAuthor(){
System.out.println(this.bookName + "的作者是" + this.bookAuthor);
}
}
- 编写配置文件
<?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">
<!-- <!–配置User对象创建–>
<bean id="user" class="com.znb.spring5.User"></bean>-->
<!--创建对象-->
<bean id="book" class="com.znb.spring5.Book">
<!--注入属性-->
<property name="bookName" value="红楼梦"></property>
<property name="bookAuthor" value="曹雪芹"></property>
</bean>
</beans>
- 测试
package com.znb.spring5;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BookTest extends TestCase {
public void testBookAndAuthor() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Book book = context.getBean("book", Book.class);
book.bookAndAuthor();
}
}
- 结果
2. 有参构造注入
- 实体类
public class Orders {
private String orderName;
private String address;
public Orders(String orderName, String address) {
this.orderName = orderName;
this.address = address;
}
public void Print(){
System.out.println("订单名称: " + this.orderName + "; 订单地址: " + this.address);
}
}
- 编写配置文件
<?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="orders" class="com.znb.spring5.Orders">
<constructor-arg name="orderName" value="手机"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
</bean>
</beans>
- 测试
public class OrdersTest extends TestCase {
public void testPrint() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Orders orders = context.getBean("orders", Orders.class);
orders.Print();
}
}
- 运行结果
3. 基于xml方式注入属性的特殊情况
①. null值
<!--注入空值null-->
<bean id="orders" class="com.znb.spring5.Orders">
<constructor-arg name="address">
<null></null>
</constructor-arg>
<constructor-arg name="orderName" value="zz"></constructor-arg>
</bean>
②. 特殊字符
<!--注入特殊字符
1. 使用转义字符
2. CDATA域
-->
<bean id="book" class="com.znb.spring5.Book">
<!--注入属性-->
<property name="bookName">
<value>
<![CDATA[<<特殊字符>>]]>
</value>
</property>
<property name="bookAuthor" value="<>曹雪芹"></property>
</bean>
4. 基于xml方式注入属性-外部bean
- 创建service类和dao类
UserService类
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();
}
}
UserDao类
public interface UserDao {
public void update();
}
UserDaoImpl类
public class UserDaoImpl implements UserDao {
@Override
public void update() {
System.out.println("dao update!");
}
}
- 编写配置文件
<?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" class="com.znb.spring5.service.UserService">
<!--2. 注入userDao对象
name:为类中属性的名称
ref:为创建的对象的id值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.znb.spring5.dao.impl.UserDaoImpl"></bean>
</beans>
- 测试类
public class UserServiceTest extends TestCase {
public void testAdd() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
- 测试结果
5. 基于xml方式注入属性-内部bean和级联赋值
- 实体类
//员工类
public class Emp {
private String empName;
private String gender;
//员工属于的部门
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public void setGender(String gender) {
this.gender = gender;
}
public void show(){
System.out.println("员工姓名: " + this.empName + "; 性别:" + this.gender + "; 部门: " + this.dept);
}
}
//部门类
public class Dept {
private String deptName;
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Dept{" +
"deptName='" + deptName + '\'' +
'}';
}
}
- 配置文件
内部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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--内部bean-->
<bean id="emp" class="com.znb.spring5.bean.Emp">
<!--设备两个普通属性-->
<property name="empName" value="张三"></property>
<property name="gender" value="男"></property>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="com.znb.spring5.bean.Dept">
<property name="deptName" value="设计部"></property>
</bean>
</property>
</bean>
</beans>
级联赋值
<?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.znb.spring5.bean.Emp">
<property name="empName" value="李四"></property>
<property name="gender" value="女"></property>
<!--级联赋值-->
<property name="dept" ref="dept1"></property>
</bean>
<bean id="dept1" class="com.znb.spring5.bean.Dept">
<property name="deptName" value="安保部"></property>
</bean>
</beans>
- 测试内部bean
public class EmpTest extends TestCase {
public void testShow() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Emp emp = context.getBean("emp", Emp.class);
emp.show();
}
}
- 测试级联赋值
public class EmpTest1 extends TestCase {
public void testShow() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
Emp emp = context.getBean("emp", Emp.class);
emp.show();
}
}