Spring-IoC

本文介绍了IoC(控制反转)的概念,并通过Spring框架的应用上下文配置文件applicationContext.xml展示了如何进行依赖注入。具体包括配置文件的结构、实体类Customer的设计及测试类Test的实现。

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);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值