Spring课程 Spring入门篇 2-2 Spring注入方式

Posted 扈江离与辟芷兮,纫秋兰以为佩。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring课程 Spring入门篇 2-2 Spring注入方式相关的知识,希望对你有一定的参考价值。

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

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,实现类中的变量,必须保证完全一致,否则,系统报错,找不到指定的方法。

 

以上是关于Spring课程 Spring入门篇 2-2 Spring注入方式的主要内容,如果未能解决你的问题,请参考以下文章

Spring入门篇——第1章 概述

Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期

Spring课程 Spring入门篇 3-1 Spring bean装配(上)之bean的配置项及作用域

Spring课程 Spring入门篇 4-6 Spring bean装配之基于java的容器注解说明--@ImportResource和@Value java与properties文件交互

慕课-《Spring入门篇》学习笔记 专题一 IOC

Spring Boot干货系列:优雅的入门篇