文章目录


Spring-IOC【03-Demo】

package com.sxt.dao;

public interface IUserDao {

	public String add();
}
package com.sxt.dao.impl;

import com.sxt.dao.IUserDao;

public class UserDaoImpl implements IUserDao{

	@Override
	public String add() {
		// TODO Auto-generated method stub
		return "hello";
	}
}
package com.sxt.service;

public interface IUserService {

	public String add();
}
package com.sxt.service.impl;

import com.sxt.dao.IUserDao;
import com.sxt.service.IUserService;

public class UserServiceImpl implements IUserService{

	private IUserDao dao;
	//设值注入 必须的方法
	public void setDao(IUserDao dao) {
		this.dao = dao;
	}
	@Override
	public String add() {
		// TODO Auto-generated method stub
		return dao.add();
	}
}
package com.sxt.controller;

import com.sxt.service.IUserService;

public class UserContreller {

	private IUserService service;

	public void setService(IUserService service) {
		this.service = service;
	}
	public String add(){
		return service.add();
	}
}
<?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">
 	<!-- 配置dao -->
 	<bean class="com.sxt.dao.impl.UserDaoImpl" id="userDaoImpl"/>
 	
 	<!-- 配置 service -->
 	<bean class="com.sxt.service.impl.UserServiceImpl" id="userServiceImpl">
 	
 	<!-- 设值注入dao对象 -->
 		<property name="dao" ref="userDaoImpl"></property>
 	</bean>	
 	
 	<!-- 配置controller -->
 	<bean class="com.sxt.controller.UserContreller">
 		<!-- 设值注入service对象 -->
 		<property name="service" ref="userServiceImpl"></property>
 	</bean>
 </beans>
package com.sxt.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sxt.controller.UserContreller;

public class TestDemo {

	@Test
	public void test(){
		//获取ApplicationContext对象 加载配置文件 反射+xml解析
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserContreller bean = ac.getBean(UserContreller.class);
		System.out.println(bean.add());
	}
}

Spring-IOC【03-Demo】

相关文章:

  • 2021-09-05
  • 2021-10-10
  • 2021-11-08
  • 2021-12-13
  • 2021-12-23
  • 2021-08-06
  • 2021-08-26
  • 2021-10-19
猜你喜欢
  • 2021-09-29
  • 2021-10-20
  • 2021-08-30
  • 2021-12-19
  • 2023-01-18
  • 2021-12-31
  • 2021-10-17
相关资源
相似解决方案