Web Technology Application Context ApplicationContext is a sub- interface of Bean Factory and adds more useful features Support for resolving text messages Generic means of loading file resources Publishing of events WebApplicationContext is a sub-interface XmlWebApplication Context is the implementation used in web applications
Web Technology Application Context ▪ ApplicationContext is a sub-interface of BeanFactory and adds more useful features - – Support for resolving text messages - – Generic means of loading file resources - – Publishing of events ▪ WebApplicationContext is a sub-interface - XmlWebApplicationContext is the implementation used in web applications 6
Web Technology What is loc loc (Inversion of control) Also called DI (Dependency Injection) Hollywood Principle: Don't call me, I'lI call you A way of sorting out dependencies between objects and automatically "injecting"references to collaborating objects on demand The loc framework, usually via XML configuration files determines injection ■控制:对象的生命周期和对象间的关系
Web Technology 7 What is IoC ▪ IoC (Inversion of control) Also called DI (Dependency Injection) Hollywood Principle: "Don't call me, I'll call you.“ ▪ A way of sorting out dependencies between objects and automatically “injecting” references to collaborating objects on demand ▪ The IoC framework, usually via XML configuration files determines injection ◼ 控制: 对象的生命周期和对象间的关系
Web Technology loC Benefits Removes the responsibility of finding or creating dependent objects and moves it into configuration Reduces coupling between implementation objects and encourages interface based design Allows an application to be re-configured outside of code Can encourage writing testable components
Web Technology IoC Benefits ▪ Removes the responsibility of finding or creating dependent objects and moves it into configuration ▪ Reduces coupling between implementation objects and encourages interface based design ▪ Allows an application to be re-configured outside of code ▪ Can encourage writing testable components 8
Web Technology Anti-loC Example- Traditional ways C⊥ass private DataSource ds private static final string dataSourceJNDI="] Source private Connection getConnection() throws Exception t nu11) nitialContext context new InitialContextoi ds Data Source PortableRemoteObject. narrow(context. lookup(dataSourceJNDI), DataSource. class)i return ds getconnection ()i public void add(object xxx)(
Web Technology 9 Anti-IoC Example-Traditional ways class XxxDAO { private DataSource ds; private static final String dataSourceJNDI="jdbcDataSource"; private Connection getConnection() throws Exception { if (ds == null) { InitialContext context = new InitialContext(); ds = (DataSource) PortableRemoteObject.narrow(context.lookup(dataSourceJNDI), DataSource.class); } return ds.getConnection(); } public void add(Object xxx){ // ... } }
Web Technology loc Style Example class xxxdao i private DataSource ds; public void setData Source(DataSource ds)i this ds=ds i private Connection getConnection() throws Exception t return ds. getconnection ()i public void add(object xxx)i
Web Technology 10 IoC Style Example class XxxDAO { private DataSource ds; public void setDataSource(DataSource ds){ this.ds=ds; } private Connection getConnection() throws Exception { return ds.getConnection(); } public void add(Object xxx){ // ... } }