Web Technology 高级Web技术 Introduction to Spring Framework
deeper Web Technology 高级Web技术 Introduction to Spring Framework
Web Technology Spring Overview Spring是Rod主创的一个应用于J2E领域的轻量应用程序框架,其核心是一个 Oc容器以及AOP实现 Rod Johnson(Expert One-on-One J2EE Design and Development) ng Spring Web 为内为 Spring Web MVC Source lovel metadata Web Mvc Spring Context JSP/ expert one onone Spring DAO Apa inion context 2EE Development Vaidation JDBC suppo NNDL EJB support& Remoting without EJB Spring Ce an conta nor
Web Technology 2 Spring Overview Rod Johnson 《Expert One-on-One J2EE Design and Development》 ◼ Spring是Rod主创的一个应用于J2EE领域的轻量应用程序框架,其核心是一个 IOC容器以及AOP实现
Web Technology Spring's benefits Offering a lightweight JavaBean container that eliminates the need to write repetitive plumbing code such as lookups Providing an inversion of control framework that allows bean dependencies to be automatically resolved upon object instantiation Allowing cross cutting concerns such as transaction management to be woven into beans as"aspects"rather than becoming the concern of the business object Offering layers of abstraction on top of popular existing technologies such as jdbc and hibernate that ease their use and organize configuration management
Web Technology Spring’s benefits ▪ Offering a lightweight JavaBean container that eliminates the need to write repetitive plumbing code such as lookups ▪ Providing an inversion of control framework that allows bean dependencies to be automatically resolved upon object instantiation ▪ Allowing cross cutting concerns such as transaction management to be woven into beans as “aspects” rather than becoming the concern of the business object ▪ Offering layers of abstraction on top of popular existing technologies such as JDBC and Hibernate that ease their use and organize configuration management 3
Web Technology Spring Core a Spring's core container provides the fundamental functionality of the Spring framework a In this module you'll find Spring,s Bean Factory, the heart of any Spring- based application. A Bean Factory is an implementation of the factory pattern that applies loc to separate your application's configuration and dependency specifications from the actual application code ORM DAO Web Spring JDBC JEE Framework Integration Batis JMX Rich View Support JSPS FreeMarke AOP Spring Portlet MvC Spring AOP AspectJ integratic Core The loc container
Web Technology 4 Spring Core ◼ Spring’s core container provides the fundamental functionality of the Spring framework. ◼ In this module you’ll find Spring’s BeanFactory, the heart of any Springbased application. A BeanFactory is an implementation of the factory pattern that applies IoC to separate your application’s configuration and dependency specifications from the actual application code
Web Technology The Bean Factory Configuration mechanism for managing beans The most common is the Xml BeanFactory o Could theoretically be any type of configuration - XML Properties File LDAP
Web Technology The Bean Factory ▪ Configuration mechanism for managing beans ▪ The most common is the XmlBeanFactory ▪ •Could theoretically be any type of configuration - – XML - – Properties File - – Database - – LDAP ▪ 5 ... ...
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){ // ... } }