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: 测试即可。

  • 相关阅读:
    对position的认知观
    对于布局的见解
    Java中的多态
    继承中类型的转换
    继承中方法的覆盖
    继承条件下的构造方法调用
    Java函数的联级调用
    关于java中String的用法
    凯撒密码
    检查java 中有多少个构造函数
  • 原文地址:https://www.cnblogs.com/E-star/p/3559008.html
Copyright © 2011-2022 走看看