1:IOC 控制权由对象本身转向容器(Spring) ,再由容器同各国配置文件对其他相关联的对象进行注入(DI,依赖性注入)。
Bean工厂 ,bean工厂创建各个实例为bean.
动态的创建对象和调用对象里的方法。
第一种方式
- public static void main(String[] args) {
- ApplicationContext context = new FileSystemXmlApplicationContext(
- "applicationContext.xml");
- Animal animal = (Animal) context.getBean("animal");
- animal.say();
- }
第二种方式:WebApplicationContextUtil
在B/S系统中,通常在web.xml初始化bean的配置文件,然后由WebAppliCationContextUtil得到ApplicationContext.例如:
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
2:AOP 面向方面编程
针对一个方面编写一个InvocationHandle
r
,然后借用JDK反射包中的Proxy类
为各
种接口动态生成相应的代理类
Spring是一个容器,凡是在容器里的对象才会有Spring所提供的这些服务和功能。Spring
里用的最经典的一个设计模式就是:模板方法模式