|
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
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
|
|
Message #184694
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Article: The Spring Loaded Observer Pattern
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 #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!
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 #184725
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Another way
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
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>
|
|
Message #184737
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Further
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???
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
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
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
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
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 #184827
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Not a good example on Spring usage
There is some reason they're called "bean definitions" instead of "bean scripts", you know.
|
|
Message #184906
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Scary
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
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
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
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
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!
|
|
Message #187297
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
I fail to see the point...
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()
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"));
|
|
 |
New content on TheServerSide.comNew content on TheServerSide.comNew content on TheServerSide.com |
 |
 |
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 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)
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)
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)
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)
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, CTO of Terracotta, Inc., talks about the new features in Terracotta 3.1, announced during JavaOne and available now.
(June 15, Tech Talk)
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)
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)
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)
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)
Gil demonstrates how new, aggressive uses of already abundant compute capacity by common applications offer competitive value for application designers.
(May 21, Tech Talk)
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)
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)
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)
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)
|
|