對 spring中xml配置的初步理解,併成功注入(spring jar包版本號一定要同一)
//.java
package com.learning.ioc.interfaces;
public interface OneInterface {
public void say(String arg);
}
package com.learning.ioc.interfaces;
public class OneInterfaceImpl implements OneInterface {
@Override
public void say(String arg) {
System.out.println(“ServiceImpl say :”+arg);
}
}
//.test
package com.learning.ioc.interfaces;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class OneInterfaceTest {
@Test
public void test(){
OneInterface oneInterface = new OneInterfaceImpl();
oneInterface.say(“kkk”);
}
@Test
public void testHello(){
//new 載入了容器了已經
ApplicationContext context = new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
OneInterface oneInterface = (OneInterface) context.getBean(“oneInterface”,OneInterface.class);
oneInterface.say(“This is very hard”);
}
}
package com.learning.ioc.interfaces;
import com.learning.ioc.injection.service.InjectionService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection {
// private ClassPathXmlApplicationContext context;
// private String springXmlPath;
//
// @Before
// public void setUp() throws Exception {
// if (StringUtils.isEmpty(springXmlPath)) {
// springXmlPath = “classpath*:spring-*.xml”;
// }
// context = new ClassPathXmlApplicationContext(springXmlPath.split("\s"));
// context.start();
// }
//
// @Test
// public void testSetter (){
// InjectionService service = context.getBean(“injectionService”, InjectionService.class);
// service.save(“這個是在test中要儲存的資料(set方法)”);
// }
// @Test
// public void testCons(){
// InjectionService service = context.getBean(“InjectionService”,InjectionService.class);
// service.save(“這是在Test中要儲存的(構造方法)”);
// }
//
}