zoukankan      html  css  js  c++  java
  • java_spring_依赖注入

    IOC反转控制 PersonService属性 PersonDao personDao接受通过xml注入的对象 PersonDaoBean.  Person中save()调用 PersonDaoBean中的add()


    package com.PersonDaoBean.test;
    
    public interface PersonDao {
    
    	public abstract void add();
    
    }



    package com.PersonDaoBean.test;
    
    //依赖注入
    
    public class PersonDaoBean implements PersonDao {
    	
    
    	@Override
    	public void add(){
    		System.out.println("PersonDaoBean执行。。。。。。。。。。。。。。。。。");
    	}
    }



    package com.bean.www;
    
    import com.PersonDaoBean.test.PersonDao;
    import com.dao.bean.www.PersonServiceDao;
    
    public class PersonServiceBean implements PersonServiceDao {
    
    	//personDao接受PersonDaoBean注入的对象
    	private PersonDao personDao;
    
    	public PersonDao getPersonDao() {
    		return personDao;
    	}
    
    	public void setPersonDao(PersonDao personDao) {
    		this.personDao = personDao;
    	}
    
    	public void save() {
    		personDao.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-3.0.xsd">
    	
    	
    	<bean id="personDao" class="com.PersonDaoBean.test.PersonDaoBean"></bean>
    	
    	
    	<bean id="personService" class="com.bean.www.PersonServiceBean">
    		<property name="personDao" ref="personDao"></property>
    	</bean>
    
    	
    </beans>



    package com.itcast.www;
    
    import static org.junit.Assert.*;
    
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.dao.bean.www.PersonServiceDao;
    
    public class TestCaseDemo {
    
    	@BeforeClass
    	public static void setUpBeforeClass() throws Exception {
    	}
    
    	@Test
    	public void instanceSpring() {
    
    		ApplicationContext ctx = new ClassPathXmlApplicationContext(
    				"applicationContext.xml");
    
    		PersonServiceDao personService = (PersonServiceDao) ctx
    				.getBean("personService");
    		personService.save();
    
    	}
    
    }
    































































































  • 相关阅读:
    ZOJ 1002 Fire Net (火力网)
    UVa OJ 117 The Postal Worker Rings Once (让邮差只走一圈)
    UVa OJ 118 Mutant Flatworld Explorers (变体扁平世界探索器)
    UVa OJ 103 Stacking Boxes (嵌套盒子)
    UVa OJ 110 MetaLoopless Sorts (无循环元排序)
    第一次遇到使用NSNull的场景
    NSURL使用浅析
    从CNTV下载《小小智慧树》
    NSDictionary and NSMutableDictionary
    Category in static library
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720412.html
Copyright © 2011-2022 走看看