这两天学习了Spring的相关知识,一直没有弄懂控制反转(ioc)是什么意思,今天在做测试的时候突然茅塞顿开,以下是我在写代码时候的心得,通俗易懂,详细的解释了Spring创建对象与传统创建对象的区别。
1 2 3 4 5 6 7 8 9 10 11
| <?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="userdao" class="spring.Userdaoimpl"/> </beans>
|
1 2 3 4
| public interface Userdao { public void dosome(); }
|
1 2 3 4
| public interface Userdao { public void dosome(); }
|
- 在测试代码中详细的解释了Spring创建对象与传统创建对象的区别。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class Test { public static void main(String[] args) {
Userdao us=new Userdaoimpl(); us.dosome();
String xmlpath="bean.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(xmlpath);
Userdao userdao=(Userdao) ac.getBean("userdao"); userdao.dosome(); } }
|
