Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Accessing spring services from hibernated domain objects

  1. #1
    Join Date
    Oct 2005
    Posts
    4

    Default Accessing spring services from hibernated domain objects

    All,
    Does anyone have recommendations for getting dependency injection of spring services to work for domain objects that are persisted by hibernate?

    I have a case where it makes sense for a domain object to access a service that's managed by spring.

    My software remotely manages PCs with different operating systems. I have low level services that deal with communicating with the PCs, and these services in turn use hibernate as well.

    Let's say I have an interface called Computer, and various implementations - XPComputer, LinuxComputer, etc. which are persisted by hibernate.

    Essentially, I want to be able to write code that looks like this:
    Code:
    computer.reboot();
    and let polymorphism take care of the rest.

    The implementations of the command could look like this:
    Code:
    XPComputer:
    void reboot() {
        xpCommunicationService.reboot(ipAddress);
       //xpCommunicationService is managed by spring
    }
    
    LinuxComputer:
    void reboot() {
        linuxCommunicationService.reboot(ipAddress);
       //linuxCommunicationService is managed by spring
    }
    Any ideas/recommendations about how this could be achieved?

    Thanks in advance!

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    We believe that the best way to do this is with AspectJ, and will provide an AspectJ aspect to dependency inject objects--whether or not they are created by Spring--in the near future, when those objects are annotated with an @SpringConfigured annotation. This will work with Hibernate, any other data access framework, or indeed any object constructed in the class loader.

    You might also check out DependencyInjectionInterceptorFactoryBean in the Spring sandbox, which doesn't require AspectJ.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by Rod Johnson
    We believe that the best way to do this is with AspectJ, and will provide an AspectJ aspect to dependency inject objects--whether or not they are created by Spring--in the near future, when those objects are annotated with an @SpringConfigured annotation. This will work with Hibernate, any other data access framework, or indeed any object constructed in the class loader.
    I think this is a very good development. The fact that the most important objects in the system hava a big problem getting dependencies (this is intensified by Spring because you drop most jndi/singleton solutions to get dependencies) is a serious problem.

  4. #4
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    I agree. Adrian and I are very keen to progress this and we've also spoken to Eric Evans (of Domain-Driven Design fame) about it, to get some input from him.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  5. #5
    Join Date
    Sep 2004
    Location
    Texas
    Posts
    155

    Default

    Thank you for clarifying that this is a priority!
    Corby

  6. #6
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Corby

    I can understand your frustration here. Once Adrian starts with Interface21 next week, lots of cool things should happen soon

    Rgds
    Rod
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  7. #7
    Join Date
    Oct 2005
    Posts
    4

    Default Thanks!

    Thanks for the Tip, Rod - will certainly check out DependencyInjectionInterceptorFactoryBean - and I'm looking forward to @SpringConfigured! That would certainly be a very useful enhancement - I'm sure this isn't the first time this sort of issue has been brought up

    Cheers,
    Vivek

  8. #8
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by Rod Johnson
    I agree. Adrian and I are very keen to progress this and we've also spoken to Eric Evans (of Domain-Driven Design fame) about it, to get some input from him.
    How are you going to deal with the overhead of dependency injection in all domain objects? I can imagine loads of domainobjects are retrieved from the database, so there will be a lot over overhead. And what about other or-mappers? Can they use the same mechanism?

  9. #9
    Join Date
    Sep 2004
    Location
    Texas
    Posts
    155

    Default

    Quote Originally Posted by Alarmnummer
    How are you going to deal with the overhead of dependency injection in all domain objects?
    Setter injection in Spring involves two operations:

    1) By inspecting the structure of the class, the Spring configuration files, and the autowiring rules used (AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, etc.), the container decides which beans must be injected using which setter method.

    2) The actual setters are invoked to inject the dependencies.

    All of the 'overhead' you describe occurs in Step 1. Step 2 has negligible performance cost. The beauty of the static annotation approach is that Step 1 can be executed exactly once, on container startup, and only Step 2 has to be executed each and every time that a new domain object is instantiated.

    Probably not a trivial coding exercise, but that's why they pay the big bucks for Adrian. :wink:
    Corby

  10. #10
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    Indeed, the overhead for this functionality is pretty low. The cost of the reflective invocation to inject dependencies is near zero now and the actual process of intercepting the creation of a new domain object is very quick due to the AspectJ weaving process.

    A really early version of this code is available in this blog entry. I will most likely commit an updated version to the sandbox next week in readiness for the upcoming 1.3 development phase starting in full force.

    Rob
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Spring accessing other non Spring Web Services
    By aanecito in forum Architecture
    Replies: 1
    Last Post: Jun 17th, 2005, 04:42 AM
  3. Spring code remarks
    By Alarmnummer in forum Architecture
    Replies: 18
    Last Post: Apr 7th, 2005, 07:17 AM
  4. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  5. Replies: 0
    Last Post: Jan 6th, 2005, 08:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •