zoukankan      html  css  js  c++  java
  • Spring的DI(Ioc)

    1: 在给对象提供构造器

    public class PersonServiceImpl implements PersonService {
    	
    	
    	private PersonDao personDao;
    	private String name;
    	
    	
    	public PersonServiceImpl(PersonDao personDao, String name) {
    		super();
    		this.personDao = personDao;
    		this.name = name;
    	}
    
    	public void save() {
    		personDao.save();
    		System.out.println("name = " + name);
    		System.out.println("service :  " + " save 方法");
    	}
    	
    }
    

      

    2: 配置XML文件:

    <?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-2.5.xsd">
               
        <bean id="personDaoImpl" class="cn.gbx.dao.PersonDaoImpl"></bean>
     	<bean id="personServiceImpl" class="cn.gbx.serviceimpl.PersonServiceImpl" >
     		<constructor-arg index="0" type="cn.gbx.daoimpl.PersonDao" ref="personDaoImpl">
     		</constructor-arg>
     		<constructor-arg index="1" value="Myname"></constructor-arg>
     	</bean>
    </beans>
    

      

    3: 测试即可。

  • 相关阅读:
    第二次作业循环语句
    c语言01次作业分支,顺序结构
    PAT 1027. Colors in Mars
    PAT 1026 Table Tennis
    PAT 1035 Password
    PAT 1038. Recover the Smallest Number
    PAT 1028 List Sorting (25)
    PAT 1041 Be Unique (20)
    PAT 1025 PAT Ranking
    1037. Magic Coupon
  • 原文地址:https://www.cnblogs.com/E-star/p/3559008.html
Copyright © 2011-2022 走看看