创建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.");
}
}
网友评论