美文网首页
Spring-ioc-Bean

Spring-ioc-Bean

作者: gentel_liao | 来源:发表于2018-07-06 09:35 被阅读0次

    创建IInterface

    IOneInterface

    package com.spring.ioc.interfaces;
    
    public interface IOneInterface {
        public void say(String arg);
    }
    

    创建实现类

    IOneInterfaceImpl

    package com.spring.ioc.interfaces;
    
    public class IOneInterfaceImpl implements IOneInterface {
        @Override
        public void say(String arg) {
            System.out.println("ServiceImpl say: " + arg);
        }
    }
    
    

    Spring.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">
    
        <bean id="oneInterface" class="com.spring.ioc.interfaces.IOneInterfaceImpl"></bean>
    
    </beans>
    

    调用Bean

    package com.spring.test.ioc.interfaces;
    
    import com.spring.ioc.interfaces.IOneInterface;
    import com.spring.test.base.UnitTestBase;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    
    @RunWith(BlockJUnit4ClassRunner.class)
    public class InterfacesTest extends UnitTestBase {
        public InterfacesTest(){
            super("classpath:spring-ioc.xml");
        }
    
        @Test
        public void testSay(){
            IOneInterface oneInterface = super.getBean("oneInterface");
            oneInterface.say("This is a test.");
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Spring-ioc-Bean

          本文链接:https://www.haomeiwen.com/subject/kycvuftx.html