IoC即控制反转。一.依赖注入
1.主配置文件:applicationContext.xml(存在src目录下)
<?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-3.0.xsd">
<bean id="customer" class="com.lx.cn.Customer"> <beans>
</beans>2.建立实体类,Customer.java(存在com.lx.cn包中)public class Customer{ private int empno;
private String ename;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}3.建立测试类,Test.java(存在com.lx.cn包中),需要导入junit4包
package com.xtkj.test;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lx.pojo.Employee;
public class Test {
@org.junit.Test
public void show1() {
//1.读取配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取对象
Customer customer = (Customer)context.getbean("customer"); System.out.println(customer);}
本文介绍了IoC(控制反转)的概念,并通过Spring框架的应用上下文配置文件applicationContext.xml展示了如何进行依赖注入。具体包括配置文件的结构、实体类Customer的设计及测试类Test的实现。
9938

被折叠的 条评论
为什么被折叠?



