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

  • 相关阅读:
    聊聊豆瓣阅读kindle版..顺便悼念一下library.nu…
    PhoneGap+jQm webapp本地化(1)环境搭建以及资源介绍
    尝试分析Q群作为技术群是个不恰当的选择!
    某android平板项目开发笔记计划任务备份
    android 自动化测试的傻瓜实践之旅(UI篇) 小试身手
    latex/Xelatex书籍排版总结顺便附上一本排好的6寸android书…
    某android平板项目开发笔记自定义sharepreference UI
    android ORM框架的性能简单测试(androrm vs ormlite)
    网络管理员必学手册
    PPT插入FLV视频文件的简单方法
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/10421538.html
Copyright © 2020-2023  润新知