1、Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Advanced Spring Framework,Rod Johnson CEO Interface21 ,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is pro
2、hibited.,Aims,Understand the capabilities of the Spring Framework component model, and how you can use them to add new power to your POJO-based applications,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Agenda,Spring co
3、mponent model fundamentals Value adds out of the box User extension points Spring 2.0 configuration extensions Spring 2.1: New Scaling out the Spring component model,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,What Is
4、 Spring?,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Much More Than an IoC Container,Core component model + Services + Patterns (Recipes) + Integration (Ecosystem) + Portability (Runs everywhere) = Universal POJO prog
5、ramming modelEmbraced by more and more enterprise vendors,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Simple Object,Enabling Technologies,Simple Object,Dependency Injection,AOP,Portable Service Abstractions (PSA),Copy
6、right 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Simple POJO,public class DefaultWeatherService implements WeatherService public String getShortSummary(String location) .public String getLongSummary(String location) .public vo
7、id update(String location, WeatherData newData)No special requirements No dependencies on Spring,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Applying Declarative Transactions,Pointcut identifies business service metho
8、ds,Advice adds behavior. This advice makes business services transactional.,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Value add: Exporting a Remote Endpoint,Can add any number of server side Exporters for each Sprin
9、g managed bean Dont need to write any Java code,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Remote Client: POJO,public class MyClient private WeatherService ws;public void setWeatherService(WeatherService ws) this.ws
10、= ws;/ Business methods omitted ,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Configuring the Remote Client POJO,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written perm
11、ission is prohibited.,Unit Testing the Dependency Injected Client,public void testForMyClient() MyClient mc = new MyClient();mc.setWeatherService(myMockService);/ Test mc Can simply inject proxy into client Imagine how much harder testing would be if we had coded the lookup method in the client,Copy
12、right 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Value add: Java Management Extensions (JMX) API Export,Can expose any bean as a JMX API MBean without writing JMX API code,Copyright 2004-2007, Interface21 Ltd. Copying, publish
13、ing, or distributing without expressed written permission is prohibited.,Extension Point: Auditing Aspect,Spring container can weave any managed POJO with aspects This aspect monitors access to the methods that ask for weather summary strings Single code module addresses single requirement (auditing
14、) Type safe with argument bindingAspect public class WeatherMonitorAuditAspect After(“execution(String *.WeatherService.*(String) & args(location) & this(ws)“)public void onQuery(String location WeatherService ws) / Location and WeatherService are bound/ to the aspect ,Pointcut identifies the method
15、s that should be affected,Advice method contains code to execute,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Applying the Aspect,Ensure an tag is used to turn on automatic application of any AspectJ aspects found in t
16、he contextSimply define the aspect as a Spring bean,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,User Extension Points,The Spring IoC container provides many extension points Can easily modify the behavior of the conta
17、iner to do custom annotation processing, specific callbacks, validation etc. or even to wrap managed objects in proxies For example: FactoryBeanobject configured by the container that creates a component, introducing a level of indirection BeanPostProcessorcalled on every bean instantiation BeanFact
18、oryPostProcessorcan modify container metadata BeanDefinitionprovides ability to add custom metadata for processing by post processors,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,User Extension Point Example,Example: I
19、ntroducing a LogAware interface Components implementing this are given a log instanceThe AccountService bean needs a Log:public class AccountService implements LogAwareprivate Log log;public void setLog(Log log) this.log = log;/ Business methods omitted. ,Copyright 2004-2007, Interface21 Ltd. Copyin
20、g, publishing, or distributing without expressed written permission is prohibited.,User Extension Point Example (Cont.),public class LogInjectingBeanPostProcessor implements BeanPostProcessor public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException if (bean i
21、nstanceof LogAware) injectLog(LogAware) bean);return bean;public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException return bean; ,Called as every bean is initialized,Return value can be any object that is type compatible with the bean being processed,Copyright
22、2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Activating a BeanPostProcessor,Like most Spring IoC extension points, simply define as a bean Automatically gets applied to all other beans Customizes the behavior of the container,Co
23、pyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Agenda,Spring component model fundamentals Value adds out of the box User extension points Spring 2.0 configuration extensions Dynamic language support Scaling out the Spring
24、component model,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,XML Configuration Extensions in Spring 2.0: Important New Extension Point,A bean definition is a recipe for creating one object Spring 2.0 added the ability
25、to define new XML tags to produce zero or more Spring bean definitions Important new extension point, offering: Higher level of abstraction can simplify many tasks Enables group beans that need to work together into a single configuration unit Can allow existing XML configuration formats to be used
26、to build Spring configuration,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,How Are XML Configuration Extensions Used?,Tags out of the box for common configuration tasks Many third party products integrating with Spring
27、 Including Java API for XML Web Services (JAX-WS) Reference Implementation User Extensions: Problem-specific configuration Makes it easier to develop and maintain applications Allow XML schema validation Better out of the box tool support Code completion for free Exploit the full power of XML Namesp
28、aces, schema, tooling,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,XML Configuration in Spring 2.0,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is proh
29、ibited.,XML Configuration in Spring 2.0,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Transaction Simplification,Specialized tags for making objects transactional Benefit from code assistCode assist for transaction attr
30、ibutes,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Annotation Driven Transactions,Transactional(readOnly=true) interface TestService Transactional(readOnly=false,rollbackFor=DuplicateOrderIdException.class)void create
31、Order(Order order)throws DuplicateOrderIdException;List queryByCriteria(Order criteria); ,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Annotation Driven Transactions,Copyright 2004-2007, Interface21 Ltd. Copying, publi
32、shing, or distributing without expressed written permission is prohibited.,Out-of-the-Box Namespaces,AOP JMX API Remoting Scheduling MVC Suggestions and contributions welcome A rich library will build over time,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expres
33、sed written permission is prohibited.,Authoring Custom Extensions: Step 1,Write an XSD to define element content Allows sophisticated validation, well beyond DTD Amenable to tool support during development Author with XML tools XML Spy,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or di
34、stributing without expressed written permission is prohibited.,Authoring Custom Extensions: Step 2,Implement a NamespaceHandler to generate Spring BeanDefinitions from element content Helper classes such as BeanDefinitionBuilder to make this easypublic interface NamespaceHandler void init();BeanDefi
35、nition parse(Element element, ParserContext parserContext);BeanDefinitionHolder decorate(Node source,BeanDefinitionHolder definition, ParserContext parserContext); ,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Authorin
36、g Custom Extensions: Step 3,Add a mapping in a META-INF/spring.handlers filehttp:/www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler http:/www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler http:/www.springframework.org/s
37、chema/jndi=org.springframework.jndi.config.JndiNamespaceHandler http:/www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler http:/www.springframework.org/schema/mvc=org.springframework.web.servlet.config.MvcNamespaceHandler,Copyright 2004-2007, Interface21 Ltd.
38、Copying, publishing, or distributing without expressed written permission is prohibited.,Using Custom Extensions,Import relevant XSD Use the new elements,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Agenda,Spring compo
39、nent model fundamentals Value adds out of the box User extension points Spring 2.0 configuration extensions Spring 2.1 Scaling out the Spring component model,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Whats New in Sp
40、ring 2.1?,Spring 2.1 Allows use of annotations for configuration, as well as XML Can mix and match annotations and XML JCA 1.5 support Further enhancements in JPA supportAims: Make Spring still easier to use,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed
41、 written permission is prohibited.,Java code: Annotations autoscanned,Component public class FooServiceImpl implements FooService Autowired private FooDao fooDao; public String foo(int id) return fooDao.findFoo(id); ,Aspect public class ServiceInvocationCounter private int useCount; Pointcut(“execut
42、ion(* demo.FooService+.*()“) public void serviceExecution() Before(“serviceExecution()“) public void countUse() this.useCount+; public int getCount() return this.useCount; ,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,
43、Bootstrap configuration,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,One component model,Contributions from different sources of configuration XML Java codeInternal Java metadata not coupled to configuration format,Cop
44、yright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Agenda,Spring component model fundamentals Value adds out of the box User extension points Spring 2.0 configuration extensions Spring 2.1 Scaling out the Spring component model
45、,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Scaling Out Spring,Spring can consume objects written in dynamic languages It can also make it easier to scale out applications by deploying and exposing POJOs in new model
46、s Examples Other deployment models such as grid SOA with SCA and ESBs OSGi dynamic modularization,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,Scaling POJOs Out to Grid Deployment,Spring enables applications to be impl
47、emented in POJOs Avoids assumptions about environment in application code Environment changes over time Ignorance is bliss: What your objects dont know cant break them if it changes Hence deployment model can change without breaking code Servlet Application Server Standalone client Grid distribution
48、 technology,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,SCA (Service Component Architecture): A Standard for SOA,Assembly model Service components, references, wires Policy framework QoS etc. Service implementation an
49、d client API Spring Java API C+ BPEL EJB architecture Bindings Web Services, Java Message Service (JMS), Java Cryptography Architecture (JCA),Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,The Open SOA Collaboration,BEA IBM Oracle SAP Sun Interface21 Red Hat Cape Clear Software IONA Primeton Technologies,Sybase Siemens Software AG TIBCO Rogue Wave Software,Copyright 2004-2007, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.,