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
-
Article: The Spring Loaded Observer Pattern (24 messages)
- Posted by: Joseph Ottinger
- Posted on: August 17 2005 07:59 EDT
Threaded Messages (24)
- Article: The Spring Loaded Observer Pattern by Corby Page on September 15 2005 18:26 EDT
- I guess this guy would agree with you by Rodolfo de Paula on September 15 2005 19:00 EDT
-
Prefer the "mixin" approach by tm jee on September 15 2005 07:53 EDT
- Prefer the "mixin" approach: Yep but they ain't identical! by Tuan Anh Nguyen on September 15 2005 10:15 EDT
-
Prefer the "mixin" approach by Adrian Brock on September 16 2005 06:07 EDT
-
Scary by Cedric Beust on September 19 2005 01:03 EDT
- Scary by Rod Johnson on September 20 2005 02:52 EDT
- Scary by Sven Meier on September 22 2005 08:01 EDT
-
Scary by Cedric Beust on September 19 2005 01:03 EDT
-
Prefer the "mixin" approach by tm jee on September 15 2005 07:53 EDT
- Article: The Spring Loaded Observer Pattern by V v on September 16 2005 03:23 EDT
- Further by V v on September 16 2005 03:46 EDT
- Article: The Spring Loaded Observer Pattern by Corby Page on September 16 2005 10:17 EDT
- ...this nightmare of XML-as-programming-language by Carlo Luib-Finetti on September 16 2005 10:40 EDT
- PicoContainer by Archimedes Trajano on September 16 2005 12:24 EDT
-
...this nightmare of XML-as-programming-language by Nils Kilden-Pedersen on September 19 2005 08:01 EDT
- ...this nightmare of XML-as-programming-language by Rod Johnson on September 20 2005 02:49 EDT
- I guess this guy would agree with you by Rodolfo de Paula on September 15 2005 19:00 EDT
- Nice Article Scott by Muthu Ramadoss on September 15 2005 23:19 EDT
- Another way by Larry Singer on September 16 2005 01:07 EDT
- Maybe I missed something??? by Colin Yates on September 16 2005 07:51 EDT
- setting observer list by Dan Glasser on November 18 2005 16:07 EST
- Re: Article: The Spring Loaded Observer Pattern by Shaun Childers on September 16 2005 08:02 EDT
- Re: Article: The Spring Loaded Observer Pattern by Shaun Childers on September 16 2005 08:10 EDT
- Not a good example on Spring usage by Jing Xue on September 16 2005 16:28 EDT
- I fail to see the point... by Daniil S on October 07 2005 15:31 EDT
- What about publishEvent() by Steve Dodge on October 14 2005 11:18 EDT
-
Article: The Spring Loaded Observer Pattern[ Go to top ]
- Posted by: Corby Page
- Posted on: September 15 2005 18:26 EDT
- in response to Joseph Ottinger
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! -
I guess this guy would agree with you[ Go to top ]
- Posted by: Rodolfo de Paula
- Posted on: September 15 2005 19:00 EDT
- in response to Corby Page
-
Prefer the "mixin" approach[ Go to top ]
- Posted by: tm jee
- Posted on: September 15 2005 19:53 EDT
- in response to Rodolfo de Paula
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 -
Prefer the "mixin" approach: Yep but they ain't identical![ Go to top ]
- Posted by: Tuan Anh Nguyen
- Posted on: September 15 2005 22:15 EDT
- in response to tm jee
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. -
Prefer the "mixin" approach[ Go to top ]
- Posted by: Adrian Brock
- Posted on: September 16 2005 18:07 EDT
- in response to tm jee
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 -
Scary[ Go to top ]
- Posted by: Cedric Beust
- Posted on: September 19 2005 01:03 EDT
- in response to Adrian Brock
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 -
Scary[ Go to top ]
- Posted by: Rod Johnson
- Posted on: September 20 2005 14:52 EDT
- in response to Cedric Beust
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 -
Scary[ Go to top ]
- Posted by: Sven Meier
- Posted on: September 22 2005 08:01 EDT
- in response to Cedric Beust
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". -- 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! -
Article: The Spring Loaded Observer Pattern[ Go to top ]
- Posted by: V v
- Posted on: September 16 2005 03:23 EDT
- in response to Corby Page
blockquote>private Subject _townCrier;public initialize(){ _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> -
Further[ Go to top ]
- Posted by: V v
- Posted on: September 16 2005 03:46 EDT
- in response to V v
Further the TownResident class does not know about the TownCarrier, it is only through the app context xml/ -
Article: The Spring Loaded Observer Pattern[ Go to top ]
- Posted by: Corby Page
- Posted on: September 16 2005 10:17 EDT
- in response to V v
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. -
...this nightmare of XML-as-programming-language[ Go to top ]
- Posted by: Carlo Luib-Finetti
- Posted on: September 16 2005 10:40 EDT
- in response to Corby Page
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... -
PicoContainer[ Go to top ]
- Posted by: Archimedes Trajano
- Posted on: September 16 2005 12:24 EDT
- in response to Carlo Luib-Finetti
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/ -
...this nightmare of XML-as-programming-language[ Go to top ]
- Posted by: Nils Kilden-Pedersen
- Posted on: September 19 2005 20:01 EDT
- in response to Carlo Luib-Finetti
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. -
...this nightmare of XML-as-programming-language[ Go to top ]
- Posted by: Rod Johnson
- Posted on: September 20 2005 14:49 EDT
- in response to Nils Kilden-Pedersen
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? -
Nice Article Scott[ Go to top ]
- Posted by: Muthu Ramadoss
- Posted on: September 15 2005 23:19 EDT
- in response to Joseph Ottinger
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 -
Another way[ Go to top ]
- Posted by: Larry Singer
- Posted on: September 16 2005 01:07 EDT
- in response to Joseph Ottinger
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. -
Maybe I missed something???[ Go to top ]
- Posted by: Colin Yates
- Posted on: September 16 2005 07:51 EDT
- in response to Joseph Ottinger
Surely setting the list of observers would be a better solution:
[code]
<bean id="thePublisher" class="...">
<property name="observers">
<list>
<ref bean="observer1"/>
<ref bean="observer2"/>
</list>
</property>
</bean>
[/code]
???? -
setting observer list[ Go to top ]
- Posted by: Dan Glasser
- Posted on: November 18 2005 16:07 EST
- in response to Colin Yates
Colin, I agree... That is how I have done it. -
Re: Article: The Spring Loaded Observer Pattern[ Go to top ]
- Posted by: Shaun Childers
- Posted on: September 16 2005 08:02 EDT
- in response to Joseph Ottinger
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. -
Re: Article: The Spring Loaded Observer Pattern[ Go to top ]
- Posted by: Shaun Childers
- Posted on: September 16 2005 08:10 EDT
- in response to Shaun Childers
Sorry all, wrong thread...could moderator please remove. -
Not a good example on Spring usage[ Go to top ]
- Posted by: Jing Xue
- Posted on: September 16 2005 16:28 EDT
- in response to Joseph Ottinger
There is some reason they're called "bean definitions" instead of "bean scripts", you know. -
I fail to see the point...[ Go to top ]
- Posted by: Daniil S
- Posted on: October 07 2005 15:31 EDT
- in response to Joseph Ottinger
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... -
What about publishEvent()[ Go to top ]
- Posted by: Steve Dodge
- Posted on: October 14 2005 11:18 EDT
- in response to Joseph Ottinger
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"));