667514 members! Sign up to stay informed.

Sponsored Links


Resources

Enterprise Java
Research Library

Get Java white papers, product information, case studies and webcasts

News News News Messages: 24 Messages: 24 Messages: 24 Printer friendly Printer friendly Printer friendly Post reply Post reply Post reply XML XML XML

Article: The Spring Loaded Observer Pattern

Posted by: Joseph Ottinger on August 17, 2005 DIGG
The Observer Pattern is also known as a publisher and subscriber design pattern. The pattern is useful when you have one publisher and many subscribers (one-to-many) that are interested in the publisher's state or messages. This article by Scott Priolo describes an easy process of implementing the observer pattern within the Spring framework, as well as an easy way to start the Spring Framework in any project.

Read Spring Loaded Observer Pattern

Threaded replies

·  Article: The Spring Loaded Observer Pattern by Joseph Ottinger on Wed Aug 17 07:59:55 EDT 2005
  ·  Article: The Spring Loaded Observer Pattern by Corby Page on Thu Sep 15 18:26:22 EDT 2005
    ·  I guess this guy would agree with you by Rodolfo de Paula on Thu Sep 15 19:00:20 EDT 2005
      ·  Prefer the "mixin" approach by tm jee on Thu Sep 15 19:53:27 EDT 2005
        ·  Prefer the "mixin" approach: Yep but they ain't identical! by Tuan Anh Nguyen on Thu Sep 15 22:15:00 EDT 2005
        ·  Prefer the "mixin" approach by Adrian Brock on Fri Sep 16 18:07:14 EDT 2005
          ·  Scary by Cedric Beust on Mon Sep 19 01:03:23 EDT 2005
            ·  Scary by Rod Johnson on Tue Sep 20 14:52:51 EDT 2005
            ·  Scary by Sven Meier on Thu Sep 22 08:01:27 EDT 2005
    ·  Article: The Spring Loaded Observer Pattern by V v on Fri Sep 16 03:23:10 EDT 2005
      ·  Further by V v on Fri Sep 16 03:46:19 EDT 2005
      ·  Article: The Spring Loaded Observer Pattern by Corby Page on Fri Sep 16 10:17:50 EDT 2005
    ·  ...this nightmare of XML-as-programming-language by Carlo Luib-Finetti on Fri Sep 16 10:40:35 EDT 2005
      ·  PicoContainer by Archimedes Trajano on Fri Sep 16 12:24:12 EDT 2005
      ·  ...this nightmare of XML-as-programming-language by Nils Kilden-Pedersen on Mon Sep 19 20:01:28 EDT 2005
        ·  ...this nightmare of XML-as-programming-language by Rod Johnson on Tue Sep 20 14:49:12 EDT 2005
  ·  Nice Article Scott by Muthu Ramadoss on Thu Sep 15 23:19:07 EDT 2005
  ·  Another way by Larry Singer on Fri Sep 16 01:07:47 EDT 2005
  ·  Maybe I missed something??? by Colin Yates on Fri Sep 16 07:51:18 EDT 2005
    ·  setting observer list by Dan Glasser on Fri Nov 18 16:07:04 EST 2005
  ·  Re: Article: The Spring Loaded Observer Pattern by Shaun Childers on Fri Sep 16 08:02:32 EDT 2005
    ·  Re: Article: The Spring Loaded Observer Pattern by Shaun Childers on Fri Sep 16 08:10:42 EDT 2005
  ·  Not a good example on Spring usage by Jing Xue on Fri Sep 16 16:28:19 EDT 2005
  ·  I fail to see the point... by Daniil S on Fri Oct 07 15:31:22 EDT 2005
  ·  What about publishEvent() by Steve Dodge on Fri Oct 14 11:18:38 EDT 2005
  Message #184694 Post reply Post reply Post reply Go to top Go to top Go to top

Article: The Spring Loaded Observer Pattern

Posted by: Corby Page on September 15, 2005 in response to Message #181571
Dear God, No!

MethodInvokingFactoryBean was intended to be used to procure Spring-managed beans from legacy factory methods, not as a substitute for general-purpose computing.

The TownCrier should have been injected into the TownResident concrete class, and registration could have occurred in the constructor, like so:
private Subject _townCrier;

public initialize()
{
  _townCrier.addListener( this );
}

Instead, we are given this nightmare of XML-as-programming-language where we clumsily attempt to execute this operation in the Spring configuration file:
<bean id="registerTownResident1" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="targetObject"><ref local="townCrier"/></property>
  <property name="targetMethod"><value>addListener</value></property>
  <property name="arguments">
    <list>
      <ref bean="townResident1"/>
    </list>
  </property>
</bean>

If you weren't struggling with the size of your Spring configuration file before, you sure are now!

  Message #184698 Post reply Post reply Post reply Go to top Go to top Go to top

I guess this guy would agree with you

Posted by: Rodolfo de Paula on September 15, 2005 in response to Message #184694
http://dynaop.dev.java.net/nonav/release/1.0-beta/manual/ch02.html

  Message #184703 Post reply Post reply Post reply Go to top Go to top Go to top

Prefer the "mixin" approach

Posted by: tm jee on September 15, 2005 in response to Message #184698
http://dynaop.dev.java.net/nonav/release/1.0-beta/manual/ch02.html

I think I like this approach better. Having the implementation of a Subject "mixin" to the target of interest. Cool way and Spring could do "mixin" as well.

regards

  Message #184714 Post reply Post reply Post reply Go to top Go to top Go to top

Prefer the "mixin" approach: Yep but they ain't identical!

Posted by: Tuan Anh Nguyen on September 15, 2005 in response to Message #184703
http://dynaop.dev.java.net/nonav/release/1.0-beta/manual/ch02.html
I think I like this approach better. Having the implementation of a Subject "mixin" to the target of interest. Cool way and Spring could do "mixin" as well.regards

I think you are right when we discuss the Observer design pattern itself. In this article, Scott would like to refer to Spring strength in the aspect of applying well-known design patterns to the framework. We all see that we are freed from wiring all the objects together to form an Observer context.

  Message #184718 Post reply Post reply Post reply Go to top Go to top Go to top

Nice Article Scott

Posted by: Muthu Ramadoss on September 15, 2005 in response to Message #181571
Good one. Gets you started with spring and explains a key BeanFactory and how to use it. Live code examples are a plus.

Keep going Scott, expecting more on Spring.

Muthu Ramadoss
http://groups.google.com/group/EtoE

  Message #184725 Post reply Post reply Post reply Go to top Go to top Go to top

Another way

Posted by: Larry Singer on September 16, 2005 in response to Message #181571
Another way to do this is using a BeanPostprocessor. Whenever a class that implements the Observer interface is found the bean BeanPostprocessor can automatically add it as a listener to the Subject.

This has the advantage that it is completely transparent to the writer of the observer and observable and that it totally avoids having to write any wierd wiring instructions on a per class basis.

It has the disadvantage that there is only one Subject per Observer.

I have not tested this specific instance, but I have previously used this pattern to register objects that implement an MBean interface with JMX.

  Message #184734 Post reply Post reply Post reply Go to top Go to top Go to top

Article: The Spring Loaded Observer Pattern

Posted by: V v on September 16, 2005 in response to Message #184694
blockquote>private Subject _townCrier;public initialize(){&nbsp;&nbsp;_townCrier.addListener( this );}
This can be done however if there is another towncrier2 and the townresident1 wants to subsribe to towncrier2 then we would have to recompile code, isn't it?

With putting the details in the xml file at least we can do away with the above issue and just change the xml setting as

<!-- this is a method invoking bean that registers the first town resident with
          with the town crier -->
    <bean id="registerTownResident1"
      class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
      <property name="targetObject"><ref local="townCrier2"/></property>
      <property name="targetMethod"><value>addListener</value></property>
      <property name="arguments">
      <list>
        <ref bean="townResident1"/>
      </list>
      </property>
    </bean>

  Message #184737 Post reply Post reply Post reply Go to top Go to top Go to top

Further

Posted by: V v on September 16, 2005 in response to Message #184734
Further the TownResident class does not know about the TownCarrier, it is only through the app context xml/

  Message #184751 Post reply Post reply Post reply Go to top Go to top Go to top

Maybe I missed something???

Posted by: Colin Yates on September 16, 2005 in response to Message #181571
Surely setting the list of observers would be a better solution:

<br>  <bean id="thePublisher" class="..."><br>    <property name="observers"> <br>      <list><br>        <ref bean="observer1"/><br>        <ref bean="observer2"/><br>      </list><br>    </property><br>  </bean><br>


????

  Message #184752 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article: The Spring Loaded Observer Pattern

Posted by: Shaun Childers on September 16, 2005 in response to Message #181571
You might want to take a look at the discussion of Java training on Javalobby: http://javalobby.com/java/forums/t44476.html

Although I have my personal opinions on the matter (as you will read), I think you need to decide if you really need training in-person from someone or can get it doing your own learning and saving money.

  Message #184753 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article: The Spring Loaded Observer Pattern

Posted by: Shaun Childers on September 16, 2005 in response to Message #184752
Sorry all, wrong thread...could moderator please remove.

  Message #184773 Post reply Post reply Post reply Go to top Go to top Go to top

Article: The Spring Loaded Observer Pattern

Posted by: Corby Page on September 16, 2005 in response to Message #184734
This can be done however if there is another towncrier2 and the townresident1 wants to subsribe to towncrier2 then we would have to recompile code, isn't it? With putting the details in the xml file at least we can do away with the above issue and just change the xml setting

You can easily support multiple subjects by making the dependency a list of subjects rather than a single subject. Then you can declare any number of subjects in the xml file without recompiling.

You certainly do not need to misuse MethodInvokingFactoryBean as a generic programming language construct to accomplish this.

Good Lord, TSS posting is messed up. Joe, please post an update when you've fixed this, I'm not going to keep going through this much effort just to get a message up.

  Message #184779 Post reply Post reply Post reply Go to top Go to top Go to top

...this nightmare of XML-as-programming-language

Posted by: Carlo Luib-Finetti on September 16, 2005 in response to Message #184694
This is in fact the essence of a trend in Java programming I am watching with some suspect since several years. It's nice to bann out unwanted dependencies on, say vendor specific data sources and the like, from your java source and inject others, even mocks, at will. But beware of outsourcing your business and domain logic into an XML configuration hell! This might work on a small level; but I'm sure you will loose yourself and your co-programmers in a system, that has it's glues not in Java, but in external XML files and depends on code only available at runtime through Java reflection mechanisms.
I think sometimes it's good to remember that reflection is a nice tricky tool, but it is in fact the quite opposite of object oriented programming styles. So in fact I'm doubting whether trendsetters like Spring really are making programmers live and work easier...

  Message #184792 Post reply Post reply Post reply Go to top Go to top Go to top

PicoContainer

Posted by: Archimedes Trajano on September 16, 2005 in response to Message #184779
This looks similar to the picocontainer sample at http://www.picocontainer.org/Two+minute+tutorial which will prevent the use of XML.

Actually, I think Spring supports something similar, though its not as well publicized as their XML bean definition file.

FD: I use PicoContainer extensively on my test framework http://twiff.sf.net/

  Message #184827 Post reply Post reply Post reply Go to top Go to top Go to top

Not a good example on Spring usage

Posted by: Jing Xue on September 16, 2005 in response to Message #181571
There is some reason they're called "bean definitions" instead of "bean scripts", you know.

  Message #184835 Post reply Post reply Post reply Go to top Go to top Go to top

Prefer the "mixin" approach

Posted by: Adrian Brock on September 16, 2005 in response to Message #184703
http://dynaop.dev.java.net/nonav/release/1.0-beta/manual/ch02.html
I think I like this approach better. Having the implementation of a Subject "mixin" to the target of interest. Cool way and Spring could do "mixin" as well.regards

Yes it could, it is much less intrustive and reusable:

http://wiki.jboss.org/wiki/Wiki.jsp?page=GOFObservable
or
http://www.damnhandy.com/?page_id=17

  Message #184906 Post reply Post reply Post reply Go to top Go to top Go to top

Scary

Posted by: Cedric Beust on September 19, 2005 in response to Message #184835
I have to agree with Corby there: this article scares me.

While I see value in specifying information in XML to do some decoupling, this line concerns me:

 <property name="targetMethod"><value>addListener</value></property>

To me, a method name in an XML file is a sign of "XML gone wild". This is going too far and we have learned this lesson the hard way with EJB's assembly descriptor.

Corby provided a valid workaround to accomplish the same goal without this hack, and I would probably suggest looking at specifying this with an annotation instead (haven't really thought that one through).

--
Cedric

  Message #185054 Post reply Post reply Post reply Go to top Go to top Go to top

...this nightmare of XML-as-programming-language

Posted by: Nils Kilden-Pedersen on September 19, 2005 in response to Message #184779
So in fact I'm doubting whether trendsetters like Spring really are making programmers live and work easier...

Let me attempt to answer that: They're not.

Spring has become an application container in exactly the same way as J2EE.

  Message #185200 Post reply Post reply Post reply Go to top Go to top Go to top

...this nightmare of XML-as-programming-language

Posted by: Rod Johnson on September 20, 2005 in response to Message #185054
Let me attempt to answer that: They're not.
Thanks for volunteering to answer, but the many happy Spring users would disagree with you.
Spring has become an application container in exactly the same way as J2EE.
Would you care to elaborate?

  Message #185203 Post reply Post reply Post reply Go to top Go to top Go to top

Scary

Posted by: Rod Johnson on September 20, 2005 in response to Message #184906
I have to agree that I wouldn't do this this way, and don't regard this as an example of Spring best practice. Spring is not intended to foster programming in XML. As a previous poster pointed out, we refer to XML bean "definition" files, not XML scripts.

Rod Johnson
Interface21 - Spring from the Source Training, Consulting, Support

  Message #185365 Post reply Post reply Post reply Go to top Go to top Go to top

Scary

Posted by: Sven Meier on September 22, 2005 in response to Message #184906
this line concerns me:&nbsp;<property name="targetMethod"><value>addListener</value></property>
To me, a method name in an XML file is a sign of "XML gone wild". -- Cedric

Funny, I just spotted this on Keith's blog regarding SWF:

<action-state id="executeSearch">
  <action bean="phonebook" method="search(searchCriteria)"/>
  <transition on="success" to="displayResults"/>
</action-state>

Looks quite similar .. and scary!

  Message #187297 Post reply Post reply Post reply Go to top Go to top Go to top

I fail to see the point...

Posted by: Daniil S on October 07, 2005 in response to Message #181571
I'm relatively new to all this and I keep trying to find out what is the point of Spring or Tapestry or similar. This article just moves this points further away. I just don't get it - where would I use this complex (as it seems to me now) and hard to use code? I can't be the only one to think this... really...

  Message #187846 Post reply Post reply Post reply Go to top Go to top Go to top

What about publishEvent()

Posted by: Steve Dodge on October 14, 2005 in response to Message #181571
I would have liked to see this implemented using Spring's native event propagation support e.g. the publishEvent() method from the ApplicationContext. (See chapter 3 of Spring Reference) The ACEGI Security framework relies Spring event propagation and it works great.

ApplicationContext context = new ClassPathXmlContext("appContext.xml");
context.publishEvent(new SillyEvent("Hello"));

  Message #191621 Post reply Post reply Post reply Go to top Go to top Go to top

setting observer list

Posted by: Dan Glasser on November 18, 2005 in response to Message #184751
Colin, I agree... That is how I have done it.

New content on TheServerSide.comNew content on TheServerSide.comNew content on TheServerSide.com

Dependency Injection in Java EE 6 - Part 1

Reza Rahman explores the features of the proposed JSR 299, Contexts and Dependency Injection for Java EE (CDI). When approved, it promises to be a key feature of Java EE 6. (November 2, Article)

SAML: It's Not just for Web services

SAML is an XML-based standard for exchanging authentication and authorization data between security domains. The single most important problem that SAML was created to solve is the Web browser Single Sign-On problem. Many organizations are debating whether to stay with version 1.1 or move to 2.0. This article makes observations about both options. (September 28, Article)

Programming is Also Teaching Your Team

Joe Ottinger takes a look at how people learn, and applies it to the practice of programming. He notes that understanding how people learn is an essential part of working in a programming team. (September 22, Article)

Can Java EE Deliver The Asynchronous Web?

Stephen Maryka gave us an article about the Asynchronous Web and posed a number of questions that get examined like an approach to delivering Asynchronous Web capabilities through extensions to existing Java EE technologies. (July 14, Article)

JSF Flex

JavaServer Faces Flex goal is to provide users capability in creating standard Flex components, part of flexSDK which is open sourced through MPL license, as normal JSF components. This article by Ji Hoon Kim will provide an overview of creating a simple multilingual JSF page consisting of JSF Flex tags. (June 29, Article)

The Rules of SOA - A Road to a Successful SOA Implementation

In this session Jeff explores the key characteristics of successful SOA projects. He covers some of the patterns, and anti-patterns, tool sets, and strategies that he himself learned the hard way. Last, he provides a strategy and blueprint for achieving a high likelihood of success in your SOA project. (June 23, Tech Talk)

Ari Zilka Talks About Terracotta 3.1

Ari Zilka, CTO of Terracotta, Inc., talks about the new features in Terracotta 3.1, announced during JavaOne and available now. (June 15, Tech Talk)

Enterprise Application Integration, and Spring

In this Tech Talk, Josh Long explores an integration challenge using Spring Integration and walks through the implementation, employing and expanding on the basic patterns of Enterprise Application Integration to tie together components into a function integration solution, and then demonstrates how Spring Integration helps address the integration requirements. (June 15, Tech Talk)

Google Web Toolkit: An Introduction

In this Tech Talk, David Geary teaches you: The basics of Google Web Toolkit; How to implement Ajax-enabled applications in Java; Internationalization; Hooking into the browser history mechanism; Remote procedure calls. (June 4, Tech Talk)

Just Enough Early Architecture to Guide Development

Jon Kern discusses the best architecture/technical solutions and ensure that they are repeated by all developers. By tackling the architecture up-front in a serial manner, subsequent parallel development will be much more manageable and predictable. (May 28, Tech Talk)

Productive Programmer: On the Lam from the Furniture Police

This keynote describes the frustrations of modern knowledge workers in their quest to actually get some work done, and solutions for how to guard yourself against all those distractions. Neal Ford talks about environments, coding, acceleration, automation, and avoiding repetition as ways to defeat the misguided attempts to sap your ability to produce good work. (May 26, Tech Talk)

Auto-Scaling Your Existing Web Application

Gil demonstrates how new, aggressive uses of already abundant compute capacity by common applications offer competitive value for application designers. (May 21, Tech Talk)

Automating Hibernate Mapping and Queries For Java Web Development

Chris Keene introduces WaveMaker as a new way to automate the ability to generate Hibernate classes in order to more quickly bring OR mapping into an application. (May 19, Article)

Auto-Scaling Your Existing Web Application

In this session Nati Shalom demonstrates how to take a standard Java EE web application and scale it out or down dynamically without changes to the application code. Seeing as most web applications are over-provisioned to meet infrequent peak loads, this is a dramatic change because it enables growing your application as needed, when needed, without paying for unutilized resources. (May 19, Tech Talk)

Free Book: Jakarta-Struts Live

Download the entire book of Jakarta-Struts Live and learn about Struts MVC, Tiles, the Validator, DynaActionForms, plug-ins, internationalization, and more.
(Book PDF Download)

Application Server Matrix

The Application Server Matrix is a detailed listing of J2EE vendors and their application server products, with information on latest version numbers, J2EE spec support and licensing, pricing, platform support, and links to product downloads and reviews.
(Application Server Comparison Matrix)

News | Blogs | Discussions | Tech talks | Patterns | Reviews | White Papers | Downloads | Articles | Media kit | About
Java Solutions
All Content Copyright ©2007 TheServerSide Privacy Policy
Site Map