2021-05-03 09:46  阅读(150)
文章分类:Spring 入门教程 文章标签:SpringSpring 教程
©  原文作者:一点教程 原文地址:http://www.yiidian.com/spring/

通过无参构造函数+setter方法注入。

一、编写CustomerDao接口

    
    package com.yiidian.dao;
    /**
     * 
     * @author http://www.yiidian.com
     *
     */
    public interface CustomerDao {
    
    	public void save();
    }
    

二、编写CustomerDaoImp实现类

    
    package com.yiidian.dao.impl;
    
    import com.yiidian.dao.CustomerDao;
    /**
     * @author http://www.yiidian.com
     *
     */
    public class CustomerDaoImpl implements CustomerDao {
    	
    	@Override
    	public void save() {
    		System.out.println("CustomerDaoImpl被成功注入!");
    	}
    
    }

三、编写CustomerService接口

    
    package com.yiidian.service;
    /**
     * 
     * @author http://www.yiidian.com
     *
     */
    public interface CustomerService {
    
    	public void save();
    }
    

四、编写CustomerServiceImpl实现类

    
    package com.yiidian.service.impl;
    
    import com.yiidian.dao.CustomerDao;
    import com.yiidian.service.CustomerService;
    /**
     * 
     * @author http://www.yiidian.com
     *
     */
    public class CustomerServiceImpl implements CustomerService {
    	private CustomerDao customerDao;
    
    	//关键在这里,提供CustomerDao作为参数的setter方法,待会在配置文件进行注入
    	public void setCustomerDao(CustomerDao customerDao) {
    		this.customerDao = customerDao;
    	}
    
    
    	@Override
    	public void save() {
    		customerDao.save();
    	}
    
    
    }

五、配置applicationContext.xml

    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:p="http://www.springframework.org/schema/p"
        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">
    
    	 <bean id="customerDao" class="com.yiidian.dao.impl.CustomerDaoImpl"></bean>
    	 
    	 <bean id="customerService" class="com.yiidian.service.impl.CustomerServiceImpl">
    	 		<!-- 
    	 			name:代表CustomerServiceImpl中的setter方法名称
    	 			ref:代表引用另一个bean对象,即CustomerDaoImpl对象
    	 		 -->
    	 		<property name="customerDao" ref="customerDao"></property>	 
    	 </bean>
        
    </beans>

20210503094602408_1.png

六、测试

    
    package com.yiidian.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.yiidian.service.CustomerService;
    
    /**
     * @author http://www.yiidian.com
     * 
     */
    public class Demo1 {
    
    	@Test
    	public void test1() {
    		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    		CustomerService customerService = (CustomerService)context.getBean("customerService");
    		customerService.save();
    	}
    
    }

七、运行结果

20210503094602407_2.png

源码下载:http://pan.baidu.com/s/1kVDuwIF

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> 依赖注入-setter方法注入
上一篇
依赖注入-构造方法注入
下一篇
依赖注入-p名称空间