spring框架现在用的是如火如荼
管理bean的功能想必是大多数用户用途最广的一个功能
而依赖注入用途也是非常广泛
不知道从什么版本开始注解功能让开发变得很简单,也很简洁
本小例子,就是用注解的方式实现bean管理和依赖注入
小米2手机快要面市了,人人都想要,本例就以此为情境。
1.手机接口---不用多说,为了减少耦合性
package com.ioc.inerface; public interface ITelphone { public void call(); }
2.小米实现
package com.ioc.impl; import org.springframework.stereotype.Component; import com.ioc.inerface.ITelphone; @Component("telphone") public class TelphoneImpl implements ITelphone{ public String toString(){ return "我是手机(小米2)"; } @Override public void call() { System.out.println("bing~~~喂,小米1,我是米2"); } }
3.手机开发出来,得有电话本,姑且认为是一个单独类(不是手机的组件),电话本,不需要接口,耦合度不需要考虑
package com.ioc.impl; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Component; @Component("addressBook") public class AddressBook { private Map mapBook = null; private void init(){ mapBook = new HashMap(); mapBook.put("小米1", "0001"); } public String getNum(String name){ if(mapBook == null){ init(); } return (String) mapBook.get(name); } }
4.ok,手机开发好了,电话本也开发好了,人类出场,毕竟造这些东西都是给人类用,人类接口
package com.ioc.inerface; public interface IPerson { public void call(); }
5.人类实现:
package com.ioc.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.ioc.inerface.IPerson; import com.ioc.inerface.ITelphone; import com.ioc.util.BeanFactory; @Component("person") public class PersonImpl implements IPerson{ @Autowired private AddressBook addressBook; public String toString(){ return "i am a human"; } @Override public void call() { ITelphone telPhone = (ITelphone) BeanFactory.getBean("telphone"); String xm1Num = addressBook.getNum("小米1"); System.out.println("---开始打电话,电话号码:"+xm1Num); telPhone.call(); System.out.println("---打电话结束"); } }
6.可是spring管理容器还没造好呢,难道要用的时候再造么,那不太晚了
package com.ioc.util; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanFactory { private static ClassPathXmlApplicationContext ctx ; static{ ctx = new ClassPathXmlApplicationContext("bean.xml"); } public static Object getBean(String beanName){ return ctx.getBean(beanName); } public static void closeBeanFactory(){ ctx.destroy(); } }
7.嗯,配置文件还没加上
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 扫描com.ioc下的所有类,自动为spring容器管理 --> <context:component-scan base-package="com.ioc"/> </beans>
8.ok,我们来测试吧
package com.ioc.test; import com.ioc.inerface.IPerson; import com.ioc.util.BeanFactory; public class SpringIocTest { public static void main(String[] args) { IPerson perSon = (IPerson) BeanFactory.getBean("person"); perSon.call(); BeanFactory.closeBeanFactory(); } }
运行结果:
---开始打电话,电话号码:0001
bing~~~喂,小米1,我是米2
---打电话结束
大功告成---其实在大多数运用中,这些知识就足够,简洁,易用,至于更详细的知识,需要进一步学习了。欢迎点评。
附:
----------------------------------------------
需要的jar
1.aspectjrt.jar
2.aspectjweaver.jar
3.cglib-nodep-2.1_3.jar
4.common-annotations.jar
5.commons-logging.jar
6.log4j-1.2.15.jar
7.spring.jar
-----------------------------------------------