课程链接:

本节主要讲了以下两块内容:

1    xml两种注入方式

2    注入方式代码实现

3    特别注意

 

 

 

 

 

1    xml两种注入方式

构造注入和set注入

 

2    注入方式代码实现

2.1  set注入方式的实现

实现类

package com.imooc.ioc.injection.service;

import com.imooc.bean.ioc.interfaces.InjectionDao;
import com.imooc.bean.ioc.interfaces.InjectionService;

public class InjectionServiceImpl implements InjectionService {
    
    private InjectionDao injectionDao;
    
    //set注入
    public void setInjectionDao(InjectionDao injectionDao) {
        this.injectionDao = injectionDao;
    }

    
    @Override
    public void save(String sArgs) {
        injectionDao.save(sArgs);
        // TODO Auto-generated method stub
        System.out.println("开始保存!!!"+sArgs);
    }

}

 

注入类

package com.imooc.ioc.injection.service;

import com.imooc.bean.ioc.interfaces.InjectionDao;

public class InjectionDaoImpl implements InjectionDao{

    @Override
    public void save(String args) {
        // TODO Auto-generated method stub
        System.out.println("导层保存参数"+args);
    }

}

 

spring-injection.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.xsd"
default-init-method="init" default-destroy-method="destroy">


<bean id="injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl">
<property name="injectionDao" ref="injectionDao"/>
</bean> 

<bean id="injectionDao" class="com.imooc.ioc.injection.service.InjectionDaoImpl"></bean> 


</beans>

 

 

测试类:

 

package com.imooc.test.ioc.interfaces;

import org.junit.Test;

import com.imooc.bean.ioc.interfaces.InjectionService;
import com.imooc.test.base.UnitTestBase;

public class TestInjectionService extends UnitTestBase{

    public TestInjectionService() {
        super("classpath*:spring-injection.xml");
        // TODO Auto-generated constructor stub
    }
    
    @Test
    public void testInjectionService(){
        //得到当前对象
        InjectionService iService = super.getbean("injectionService");
        iService.save("12345");
    }
    

}

 

 

2.2  构造注入方式的实现

 实现类

package com.imooc.ioc.injection.service;

import com.imooc.bean.ioc.interfaces.InjectionDao;
import com.imooc.bean.ioc.interfaces.InjectionService;

public class InjectionServiceImpl implements InjectionService {
    
    private InjectionDao injectionDao;
    
    /*//set注入
    public void setInjectionDao(InjectionDao injectionDao) {
        this.injectionDao = injectionDao;
    }*/
    
    //构造器注入
    //注意:这里要有 this.injectionDao = injectionDao;
    public InjectionServiceImpl(InjectionDao injectionDao) {
        // TODO Auto-generated constructor stub
        this.injectionDao = injectionDao;
    }

    
    @Override
    public void save(String sArgs) {
        injectionDao.save(sArgs);
        // TODO Auto-generated method stub
        System.out.println("开始保存!!!"+sArgs);
    }

    

}

 

注入类:(同上)

 

spring-injection.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.xsd"
default-init-method="init" default-destroy-method="destroy">


<!-- <bean id="injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl">
<property name="injectionDao" ref="injectionDao"/>
</bean>  -->

<!-- 注意:这里是ref -->
<bean id = "injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl">
<constructor-arg name="injectionDao" ref="injectionDao"></constructor-arg>
</bean>


<bean id="injectionDao" class="com.imooc.ioc.injection.service.InjectionDaoImpl"></bean> 


</beans>

 

测试类:

package com.imooc.test.ioc.interfaces;

import org.junit.Test;

import com.imooc.bean.ioc.interfaces.InjectionService;
import com.imooc.test.base.UnitTestBase;

public class TestInjectionService extends UnitTestBase{

    public TestInjectionService() {
        super("classpath*:spring-injection.xml");
        // TODO Auto-generated constructor stub
    }
    
    @Test
    public void testInjectionService(){
        //得到当前对象
        InjectionService iService = super.getbean("injectionService");
        iService.save("12345");
    }
    
    
    
    @Test
    public void testInjectionServiceConstruct(){
        //得到当前对象
        try {
            InjectionService iService = super.getbean("injectionService");
            iService.save("12346");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
    }
    

}

 

 

 

3    特别注意

xml配置的name,实现类中的变量,必须保证完全一致,否则,系统报错,找不到指定的方法。

 

posted on 2019-02-23 07:57  菜鸟乙  阅读(159)  评论(0编辑  收藏  举报