• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,667
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
108
Comments
0
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Spring Introduction
  • 2. Preface
    • Inversion of Control (IoC)
    • 3. Resource
    • 4. Aspect Oriented Programming (AOP)
    • 5. Middle Tier Data Access
    • 6. Spring MVC
    • 7. J2EE(and related) Integration
    • 8. Testing
  • 9. Preface
  • 10. Inversion of Control (IoC)
    • BeanFactory
    • 11. The BeanFactory interface is the central IoC container interface in Spring. Its responsibilities include instantiating or sourcing application objects, configuring such objects, and assembling the dependencies between these objects.
    • 12. ApplicationContext
    • 13. The ApplicationContext interface builds on top of the BeanFactory and adds other functionality. Such as WebApplicationContext, ClassPathXmlApplicationContext,
  • 14. Inversion of Control (IoC)
      XML base configuration
    <? xml version = &quot;1.0&quot; encoding = &quot;UTF-8&quot; ?> < beans xmlns = &quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi = &quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation = &quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&quot; > < bean id = &quot;...&quot; class = &quot;...&quot; > <!-- collaborators and configuration for this bean go here --> </ bean > < bean id = &quot;...&quot; class = &quot;...&quot; > <!-- collaborators and configuration for this bean go here --> </ bean > <!-- more bean definitions go here -> </beans>
  • 15. Inversion of Control (IoC)
      XML base configuration
    • Instantiation using a constructor:
    • 16. Instantiation using a static factory method:
    • 17. Instantiation using an instance factory method:
    < bean id = &quot;exampleBean&quot; class = &quot;examples.ExampleBean&quot; /> < bean name = &quot;anotherExample&quot; class = &quot;examples.ExampleBeanTwo&quot; /> < bean id = &quot;exampleBean&quot; class = &quot;examples.ExampleBean2&quot; factory-method = &quot;createInstance&quot; /> <!-- the factory bean, which contains a method called createInstance() --> < bean id = &quot;serviceLocator&quot; class = &quot;com.foo.DefaultServiceLocator&quot; > <!-- inject any dependencies required by this locator bean --> </ bean > <!-- the bean to be created via the factory bean --> < bean id = &quot;exampleBean&quot; factory-bean = &quot;serviceLocator&quot; factory-method = &quot;createInstance&quot; />
  • 18. Inversion of Control (IoC)
      XML base configuration Constructor Injection:
    package x.y; public class Foo { public Foo(Bar bar, Baz baz) { // ... } } < beans > < bean name = &quot;foo&quot; class = &quot;x.y.Foo&quot; > < constructor-arg > < bean class = &quot;x.y.Bar&quot; /> </ constructor-arg > < constructor-arg > < bean class = &quot;x.y.Baz&quot; /> </ constructor-arg > </ bean > </ beans >
  • 19. Inversion of Control (IoC)
      XML base configuration
    • Constructor Argument Type Matching:
    • 20. Constructor Argument Index:
    < bean id = &quot;exampleBean&quot; class = &quot;examples.ExampleBean&quot; > < constructor-arg type = &quot;int&quot; value = &quot;7500000&quot; /> < constructor-arg type = &quot;java.lang.String&quot; value = &quot;42&quot; /> </ bean > < bean id = &quot;exampleBean&quot; class = &quot;examples.ExampleBean&quot; > < constructor-arg index = &quot;0&quot; value = &quot;7500000&quot; /> < constructor-arg index = &quot;1&quot; value = &quot;42&quot; /> </ bean >
  • 21. Inversion of Control (IoC)
      XML base configuration Setter Injection:
    public class ExampleBean { private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; public void setBeanOne(AnotherBean beanOne) { this .beanOne = beanOne; } public void setBeanTwo(YetAnotherBean beanTwo) { this .beanTwo = beanTwo; } public void setIntegerProperty( int i) { this .i = i; } } < bean id = &quot;exampleBean&quot; class = &quot;examples.ExampleBean&quot; > <!-- setter injection using the nested <ref/> element --> < property name = &quot;beanOne&quot; >< ref bean = &quot;anotherExampleBean&quot; /></ property > <!-- setter injection using the neater 'ref' attribute --> < property name = &quot;beanTwo&quot; ref = &quot;yetAnotherBean&quot; /> < property name = &quot;integerProperty&quot; value = &quot;1&quot; /> </ bean >
  • 22. Inversion of Control (IoC)
      Bean scopes
    • singleton - Scopes a single bean definition to a single object instance per Spring IoC container.
    • 23. prototype - Scopes a single bean definition to any number of object instances.
    • 24. request - Scopes a single bean definition to the lifecycle of a single HTTP request.
    • 25. session - Scopes a single bean definition to the lifecycle of a HTTP Session.
    • 26. global session - Scopes a single bean definition to the lifecycle of a global HTTP Session .
  • 27. Inversion of Control (IoC)
    • Singleton
  • 28. Inversion of Control (IoC)
    • Prototype
  • 29. Resource
      Built-in Resource implementations
    • UrlResource
    • 30. ClassPathResource
    • 31. FileSystemResource
    • 32. ServletContextResource
    • 33. InputStreamResource
    • 34. ByteArrayResource
    Resource template = ctx.getResource( &quot;some/resource/path/myTemplate.txt); Resource template = ctx.getResource( &quot;file:/some/resource/path/myTemplate.txt); Resource template = ctx.getResource( &quot;http://myhost.com/resource/path/myTemplate.txt);
  • 35. Aspect Oriented Programming
      AOP concepts
    • Aspect: a modularization of a concern that cuts across multiple classes. In Spring AOP, aspects are implemented using regular classes or regular classes annotated with @Aspect.
    • 36. Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.
    • 37. Advice: action taken by an aspect at a particular join point. Different types of advice include &quot;around&quot;, &quot;before&quot; and &quot;after&quot; advice.
    • 38. Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut. Spring uses the AspectJ pointcut expression language by default.
    • 39. Introduction: declaring additional methods or fields on behalf of a type.
  • 40. Aspect Oriented Programming
      AOP concepts
    • Target object: object being advised by one or more aspects. Also referred to as the advised object. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.
    • 41. AOP proxy: an object created by the AOP framework in order to implement the aspect contracts. In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.
    • 42. Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime .
  • 43. Aspect Oriented Programming
      Types of advice
    • Before advice: Advice that executes before a join point.
    • 44. After returning advice: Advice to be executed after a join point completes normally.
    • 45. After throwing advice: Advice to be executed if a method exits by throwing an exception.
    • 46. After (finally) advice: Advice to be executed regardless of the means by which a join point exits.
    • 47. Around advice: Advice that surrounds a join point such as a method invocation. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
  • 48. Aspect Oriented Programming
      Supported Pointcut Designators - execution For matching method execution join points, this is the primary pointcut designator you will use when working with Spring AOP. The format of anexecution expression is: execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?) the execution of any public method: the execution of any method with a name beginning with &quot;set&quot;: the execution of any method defined by the AccountService interface:
    execution( public * *(..)) execution(* set*(..)) execution(* com.xyz.service.AccountService.*(..))
  • 49. Aspect Oriented Programming
      Supported Pointcut Designators – within Limits matching to join points within certain types. any join point within the service package: any join point within the service package or a sub-package:
    within(com.xyz.service.*) within(com.xyz.service..*)
  • 50. Aspect Oriented Programming
      Other Supported Pointcut Designators
    • this - limits matching to join points where the bean reference is an instance of the given type.
    • 51. target - limits matching to join points where the target object is an instance of the given type.
    • 52. args - limits matching to join points where the arguments are instances of the given types.
    • 53. @target - limits matching to join points where the class of the executing object has an annotation of the given type.
    • 54. @args - limits matching to join points where the runtime type of the actual arguments passed have annotations of the given type(s).
    • 55. @within - limits matching to join points within types that have the given annotation.
  • 56. Aspect Oriented Programming
      Declaring advice
    @ Aspect public class BeforeExample { @ Before( &quot;execution(* com.xyz.myapp.dao.*.*(..))&quot; ) public void doAccessCheck() { // ... } } @ Aspect public class AfterReturningExample { @ AfterReturning( &quot;com.xyz.myapp.SystemArchitecture.dataAccessOperation()&quot; ) public void doAccessCheck() { // ... } } @ Aspect public class AfterThrowingExample { @ AfterThrowing(pointcut = &quot;com.xyz.SystemArchitecture.dataAccessOperation()&quot; , throwing = &quot;ex&quot; ) public void doRecoveryActions(DataAccessException ex) { // ... } }
  • 57. Middle Tier Data Access
      JDBC DataSource and transaction definition
    < bean id = &quot;dataSource&quot; class = &quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method = &quot;close&quot; > < property name = &quot;driverClassName&quot; value = &quot;${jdbc.driverClassName}&quot; /> < property name = &quot;url&quot; value = &quot;${jdbc.url}&quot; /> < property name = &quot;username&quot; value = &quot;${jdbc.username}&quot; /> < property name = &quot;password&quot; value = &quot;${jdbc.password}&quot; /> </ bean > < bean id = &quot;txManager&quot; class = &quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot; > < property name = &quot;dataSource&quot; ref = &quot;dataSource&quot; /> </ bean >
  • 58. Middle Tier Data Access
      Declarative transaction management
  • 59. Middle Tier Data Access
      Declarative transaction management with XML
    <? xml version = &quot;1.0&quot; encoding = &quot;UTF-8&quot; ?> < beans ...... > <!-- this is the service object that we want to make transactional --> < bean id = &quot;fooService&quot; class = &quot;x.y.service.DefaultFooService&quot; /> <!-- the transactional advice --> < tx:advice id = &quot;txAdvice&quot; transaction-manager = &quot;txManager&quot; > <!-- the transactional semantics... --> < tx:attributes > <!-- all methods starting with 'get' are read-only --> < tx:method name = &quot;get*&quot; read-only = &quot;true&quot; /> <!-- other methods use the default transaction settings (see below) --> < tx:method name = &quot;*&quot; /> </ tx:attributes > </ tx:advice > <!-- ensure that the above transactional advice runs for any execution of an operation defined by the FooService interface --> < aop:config > < aop:pointcut id = &quot;fooServiceOperation&quot; expression = &quot;execution(* x.y.service.FooService.*(..))&quot; /> < aop:advisor advice-ref = &quot;txAdvice&quot; pointcut-ref = &quot;fooServiceOperation&quot; /> </ aop:config > </ beans >
  • 60. Middle Tier Data Access
      Declarative transaction management with annotation
    <!-- enable the configuration of transactional behavior based on annotations --> < tx:annotation-driven transaction-manager = &quot;txManager&quot; /> @ Transactional(readOnly = true ) public class DefaultFooService implements FooService { public Foo getFoo(String fooName) { // do something } // these settings have precedence for this method @ Transactional(readOnly = false , propagation = Propagation.REQUIRES_NEW) public void updateFoo(Foo foo) { // do something } }
  • 61. Middle Tier Data Access
      Programmatic transaction management Using the TransactionTemplate:
    public class SimpleService implements Service { // single TransactionTemplate shared amongst all methods in this instance private final TransactionTemplate transactionTemplate; // use constructor-injection to supply the PlatformTransactionManager public SimpleService(PlatformTransactionManager transactionManager) { Assert.notNull(transactionManager, &quot;transactionManager must not be null.&quot; ); this .transactionTemplate = new TransactionTemplate(transactionManager); } public Object someServiceMethod() { return transactionTemplate.execute( new TransactionCallback() { // the code in this method executes in a transactional context public Object doInTransaction(TransactionStatus status) { updateOperation1(); return resultOfUpdateOperation2(); } }); } }
  • 62. Middle Tier Data Access
      Data access using JDBC A simple query for getting the number of rows in a relation: A simple query using a bind variable: Querying and populating a single domain object:
    int rowCount = this .jdbcTemplate.queryForInt( &quot;select count(0) from t_accrual&quot; ); int countOfActorsNamedJoe = this .jdbcTemplate.queryForInt( &quot;select count(0) from t_actors where first_name = ?&quot; , new Object[]{ &quot;Joe&quot; }); Actor actor = (Actor) this .jdbcTemplate.queryForObject( &quot;select first_name, surname from t_actor where id = ?&quot; , new Object[]{ new Long(1212)}, new RowMapper() { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Actor actor = new Actor(); actor.setFirstName(rs.getString( &quot;first_name&quot; )); actor.setSurname(rs.getString( &quot;surname&quot; )); return actor; } });
  • 63. Middle Tier Data Access
      Data access using Hibernate
    < beans > < bean id = &quot;myDataSource&quot; class = &quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method = &quot;close&quot; > <!-- DataSource configration --> </ bean > < bean id = &quot;mySessionFactory&quot; class = &quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot; > < property name = &quot;dataSource&quot; ref = &quot;myDataSource&quot; /> < property name = &quot;mappingResources&quot; > < list > < value > product.hbm.xml </ value > </ list > </ property > < property name = &quot;hibernateProperties&quot; > < value > hibernate.dialect=org.hibernate.dialect.HSQLDialect </ value > </ property > </ bean > </ beans >
  • 64. Middle Tier Data Access
      Data access using Hibernate
    public class ProductDaoImpl implements ProductDao { private HibernateTemplate hibernateTemplate; public void setSessionFactory(SessionFactory sessionFactory) { this .hibernateTemplate = new HibernateTemplate(sessionFactory); } public Collection loadProductsByCategory(String category) throws DataAccessException { return this .hibernateTemplate.find( &quot;from test.Product product where product.category=?&quot; , category); } } public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao { public Collection loadProductsByCategory(String category) throws DataAccessException { return this .getHibernateTemplate().find( &quot;from test.Product product where product.category=?&quot; , category); } }
  • 65. Middle Tier Data Access
  • 69. Spring MVC
  • 70. J2EE(and related) Integration
    • Remoting and web services
    • 71. Enterprise Java Beans (EJB) integration
    • 72. JMS (Java Message Service)
    • 73. JMX
    • 74. Email
    • 75. Scheduling and Thread Pooling
    • 76. Dynamic language support
  • 77. J2EE(and related) Integration
      Remoting and web services Currently, Spring supports following remoting technologies:
    • Remote Method Invocation (RMI) . Through the use of the RmiProxyFactoryBean and the RmiServiceExporter Spring supports both traditional RMI and transparent remoting via RMI invokers.
    • 78. Spring's HTTP invoker . Spring provides a special remoting strategy which allows for Java serialization via HTTP, supporting any Java interface.
    • 79. Hessian . By using Spring's HessianProxyFactoryBean and the HessianServiceExporter you can transparently expose your services using the lightweight binary HTTP-based protocol provided by Caucho.
    • 80. Burlap . Burlap is Caucho's XML-based alternative to Hessian. Spring provides support classes such as BurlapProxyFactoryBean and BurlapServiceExporter.
    • 81. JAX-RPC . Spring provides remoting support for web services via JAX-RPC (J2EE 1.4's web service API).
    • 82. JAX-WS . Spring provides remoting support for web services via JAX-WS
  • 83. J2EE(and related) Integration
      Remote Method Invocation (RMI) Exporting the service using the RmiServiceExporter:
    < bean id = &quot;accountService&quot; class = &quot;example.AccountServiceImpl&quot; > <!-- any additional properties, maybe a DAO? --> </ bean > < bean class = &quot;org.springframework.remoting.rmi.RmiServiceExporter&quot; > <!-- does not necessarily have to be the same name as the bean to be exported --> < property name = &quot;serviceName&quot; value = &quot;AccountService&quot; /> < property name = &quot;service&quot; ref = &quot;accountService&quot; /> < property name = &quot;serviceInterface&quot; value = &quot;example.AccountService&quot; /> <!-- defaults to 1099 --> < property name = &quot;registryPort&quot; value = &quot;1199&quot; /> </ bean >
  • 84. J2EE(and related) Integration
      Remote Method Invocation (RMI) Linking in the service at the client:
    < bean class = &quot;example.SimpleObject&quot; > < property name = &quot;accountService&quot; ref = &quot;accountService&quot; /> </ bean > < bean id = &quot;accountService&quot; class = &quot;org.springframework.remoting.rmi.RmiProxyFactoryBean&quot; > < property name = &quot;serviceUrl&quot; value = &quot;rmi://HOST:1199/AccountService&quot; /> < property name = &quot;serviceInterface&quot; value = &quot;example.AccountService&quot; /> </ bean >
  • 85. J2EE(and related) Integration
      Enterprise Java Beans (EJB) integration
    • XML base injection:
    • 86. Annotation base injection:
    < jee:local-slsb id = &quot;myComponent&quot; jndi-name = &quot;ejb/myBean&quot; usiness-interface = &quot;com.mycom.MyComponent&quot; /> < bean id = &quot;myController&quot; class = &quot;com.mycom.myController&quot; > < property name = &quot;myComponent&quot; ref = &quot;myComponent&quot; /> </ bean > @ Stateless @ Interceptors(SpringBeanAutowiringInterceptor. class ) public class MyFacadeEJB implements MyFacadeLocal { // automatically injected with a matching Spring bean @ Autowired private MyComponent myComp; // for business method, delegate to POJO service impl. public String myFacadeMethod(...) { return myComp.myMethod(...); } ... }
  • 87. J2EE(and related) Integration
      JMS (Java Message Service)
    < beans ...... > < bean id = &quot;jndiTemplate&quot; class = &quot;org.springframework.jndi.JndiTemplate&quot; > < property name = &quot;environment&quot; > < props > < prop key = &quot;java.naming.factory.initial&quot; > org.jnp.interfaces.NamingContextFactory </ prop > < prop key = &quot;java.naming.provider.url&quot; > localhost </ prop > < prop key = &quot;java.naming.factory.url.pkgs&quot; > org.jnp.interfaces:org.jboss.naming </ prop > </ props > </ property > </ bean > < bean id = &quot;jmsConnectionFactory&quot; class = &quot;org.springframework.jndi.JndiObjectFactoryBean&quot; > < property name = &quot;jndiTemplate&quot; > < ref bean = &quot;jndiTemplate&quot; /> </ property > < property name = &quot;jndiName&quot; > < value > UIL2ConnectionFactory </ value > </ property > </ bean > < bean id = &quot;jmsTemplate&quot; class = &quot;org.springframework.jms.core.JmsTemplate&quot; > < property name = &quot;connectionFactory&quot; > < bean class = &quot;org.springframework.jms.connection.SingleConnectionFactory&quot; > < property name = &quot;targetConnectionFactory&quot; ref = &quot;jmsConnectionFactory&quot; /> </ bean > </ property > </ bean > </ beans >
  • 88. J2EE(and related) Integration
      JMS (Java Message Service)
    < bean id = &quot;indexDestination&quot; class = &quot;org.springframework.jndi.JndiObjectFactoryBean&quot; > < property name = &quot;jndiTemplate&quot; > < ref bean = &quot;jndiTemplate&quot; /> </ property > < property name = &quot;jndiName&quot; > < value > queue/HRRSIndexQueue </ value > </ property > </ bean > < bean id = &quot;indexMessageSender&quot; class = &quot;com.augmentum.hrrs.mdb.IndexMessageSender&quot; > < property name = &quot;template&quot; ref = &quot;jmsTemplate&quot; /> < property name = &quot;destination&quot; ref = &quot;indexDestination&quot; /> </ bean > < bean id = &quot;indexMessageListener&quot; class = &quot;com.augmentum.hrrs.mdb.IndexMessageListener&quot; /> < bean id = &quot;indexJMSContainer&quot; class = &quot;org.springframework.jms.listener.DefaultMessageListenerContainer&quot; > < property name = &quot;connectionFactory&quot; ref = &quot;jmsConnectionFactory&quot; /> < property name = &quot;destination&quot; ref = &quot;indexDestination&quot; /> < property name = &quot;messageListener&quot; ref = &quot;indexMessageListener&quot; /> </ bean >
  • 89. J2EE(and related) Integration
      JMS Sender
    public class IndexMessageSender { private JmsTemplate template; private Destination destination; public void sendBatchImportCandidate( final Map<String, String> params) { template.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { MapMessage msg = session.createMapMessage(); ...... return msg; } }); } public void setDestination(Destination destination) { this .destination = destination; } public void setTemplate(JmsTemplate template) { this .template = template; } }
  • 90. J2EE(and related) Integration
      JMS Listener
    public class IndexMessageListener implements MessageListener { public void onMessage(Message message) { ...... } }
  • 91. J2EE(and related) Integration
      Email - Velocity based example Velocity email template:
    # in the com/foo/package < html > < body > < h3 > Hi ${user.userName}, welcome! </ h3 > < div > Your email address is < a href = &quot;mailto:${user.emailAddress}&quot; > ${user.emailAddress} </ a > </ div > </ body > </ html >
  • 92. J2EE(and related) Integration
      Email - Velocity based example
    public class SimpleRegistrationService implements RegistrationService { private JavaMailSender mailSender; private VelocityEngine velocityEngine; public void sendConfirmationEmail( final User user) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(user.getEmailAddress()); message.setFrom( &quot;webmaster@csonth.gov.uk&quot; ); Map model = new HashMap(); model.put( &quot;user&quot; , user); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, &quot;com/dns/registration-confirmation.vm&quot; , model); message.setText(text, true ); } }; this .mailSender.send(preparator); } ...... }
  • 93. J2EE(and related) Integration
      Email - Velocity based example
    < beans ...... > < bean id = &quot;mailSender&quot; class = &quot;org.springframework.mail.javamail.JavaMailSenderImpl&quot; > < property name = &quot;host&quot; value = &quot;mail.csonth.gov.uk&quot; /> </ bean > < bean id = &quot;registrationService&quot; class = &quot;com.foo.SimpleRegistrationService&quot; > < property name = &quot;mailSender&quot; ref = &quot;mailSender&quot; /> < property name = &quot;velocityEngine&quot; ref = &quot;velocityEngine&quot; /> </ bean > < bean id = &quot;velocityEngine&quot; class = &quot;org.springframework.ui.velocity.VelocityEngineFactoryBean&quot; > < property name = &quot;velocityProperties&quot; > < value > resource.loader = class class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader </ value > </ property > </ bean > </ beans >
  • 94. J2EE(and related) Integration
      Scheduling and Thread Pooling
    • OpenSymphony Quartz Scheduler
    • 95. JDK Timer support
    • 96. Spring TaskExecutor abstraction
  • 97. J2EE(and related) Integration
      Using the OpenSymphony Quartz Scheduler Using the JobDetailBean:
    < bean name = &quot;exampleJob&quot; class = &quot;org.springframework.scheduling.quartz.JobDetailBean&quot; > < property name = &quot;jobClass&quot; value = &quot;example.ExampleJob&quot; /> < property name = &quot;jobDataAsMap&quot; > < map > < entry key = &quot;timeout&quot; value = &quot;5&quot; /> </ map > </ property > </ bean > public class ExampleJob extends QuartzJobBean { private int timeout ; protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException { // do the actual work } public void setTimeout( int timeout) { this . timeout = timeout; } }
  • 98. J2EE(and related) Integration
      Using the OpenSymphony Quartz Scheduler Wiring up jobs using triggers and the SchedulerFactoryBean:
    < bean id = &quot;simpleTrigger&quot; class = &quot;org.springframework.scheduling.quartz.SimpleTriggerBean&quot; > <!-- see the example of method invoking job above --> < property name = &quot;jobDetail&quot; ref = &quot;jobDetail&quot; /> <!-- 10 seconds --> < property name = &quot;startDelay&quot; value = &quot;10000&quot; /> <!-- repeat every 50 seconds --> < property name = &quot;repeatInterval&quot; value = &quot;50000&quot; /> </ bean > < bean id = &quot;cronTrigger&quot; class = &quot;org.springframework.scheduling.quartz.CronTriggerBean&quot; > < property name = &quot;jobDetail&quot; ref = &quot;exampleJob&quot; /> <!-- run every morning at 6 AM --> < property name = &quot;cronExpression&quot; value = &quot;0 0 6 * * ?&quot; /> </ bean > < bean class = &quot;org.springframework.scheduling.quartz.SchedulerFactoryBean&quot; > < property name = &quot;triggers&quot; > < list > < ref bean = &quot;cronTrigger&quot; /> < ref bean = &quot;simpleTrigger&quot; /> </ list > </ property > </ bean >
  • 99. Testing
      Integration testing The Spring integration testing support frameworks share several common goals, including:
    • Spring IoC container caching between test execution.
    • 100. Dependency Injection of test fixture instances.
    • 101. Transaction management appropriate to integration testing.
    • 102. Spring-specific support classes that are really useful when writing integration tests.
  • 103. Testing
      Context management and caching
    @ RunWith(SpringJUnit4ClassRunner. class ) // ApplicationContext will be loaded from &quot;/applicationContext.xml&quot; and // &quot; applicationContext-test.xml&quot; in the root of the classpath @ ContextConfiguration(locations = { &quot;/applicationContext.xml&quot; , &quot;/applicationContext-test.xml&quot; }) public class MyTest { // class body... }
  • 104. Testing
      Dependency Injection of test fixtures
    @ RunWith(SpringJUnit4ClassRunner. class ) // specifies the Spring configuration to load for this test fixture @ ContextConfiguration(locations = { &quot;daos.xml&quot; }) public final class HibernateTitleDaoTests { // this instance will be dependency injected by type @ Autowired private HibernateTitleDao titleDao; public void testLoadTitle() throws Exception { Title title = this .titleDao.loadTitle( new Long(10)); assertNotNull(title); } }
  • 105. Testing
      Transaction management
    @ RunWith(SpringJUnit4ClassRunner. class ) @ ContextConfiguration @ TransactionConfiguration(transactionManager = &quot;txMgr&quot; , defaultRollback = false ) @ Transactional public class FictitiousTransactionalTest { // logic to verify the initial state before a transaction is started @ BeforeTransaction public void verifyInitialDatabaseState() {} // set up test data within the transaction @ Before public void setUpTestDataWithinTransaction() {} @ Test // overrides the class-level defaultRollback setting @ Rollback( true ) public void modifyDatabaseWithinTransaction() {} // execute &quot;tear down&quot; logic within the transaction @ After public void tearDownWithinTransaction() {} // logic to verify the final state after transaction has rolled back @ AfterTransaction public void verifyFinalDatabaseState() {} @ Test // logic which does not modify database state @ NotTransactional public void performNonDatabaseRelatedAction() {} }
  • 106. Thanks