Spring-IOC实现-XML-实例(二)

实例

1、创建项目

创建项目

2、创建dao层

public interface IUserDAO {
	public String say(String msg);
}

@Override
	public String say(String msg) {
		System.out.println("--->"+msg);
		return "hello";
	}

3、创建service层

public interface IUserService {
	public String say(String msg);
}
private IUserDAO dao;
	

	@Override
	public String say(String msg) {
		
		return dao.say(msg);
	}

	
	/**
	 * 设值注入必要提供的setter方法
	 * @param dao
	 */
	public void setDao(IUserDAO dao) {
		this.dao = dao;
	}

4、创建control层

public class UserController {
	private IUserService service;
	
	public void setService(IUserService service) {
		this.service = service;
	}
	
	public void say(String msg){
		
	     System.out.println("controller:"+service.say(msg));
		
		
	}
}

5、配置文件

<?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">
 	<!-- 注册dao层实例 -->
 	<bean class="com.sxt.dao.impl.UserDAOImpl" id="userDAOImpl"></bean>
 	
 	<!-- 注册service实例 -->
	<bean class="com.sxt.service.UserServiceImpl" id="userServiceImpl">
		<property name="dao" ref="userDAOImpl"></property>
	</bean>	
	
	<!-- 注册controller实例 -->
	<bean class="com.sxt.controller.UserController">
	   <property name="service" ref="userServiceImpl"></property>
	</bean>
</beans>

6、测试

public class test {
	public static void main(String[] args) {
		ApplicationContext ac=
				new ClassPathXmlApplicationContext("applicationContext.xml");
		UserController controller = ac.getBean(UserController.class);
		    controller.say("aaa");
	}
	
	
	
}

测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值