Announcement Announcement Module
Collapse
No announcement yet.
Problem with Domain Object DI Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Domain Object DI

    I have been experimenting with domain objects dependency-injection(DI) but without much success. Here are the codes:

    Account.java
    Code:
    package com.simple.busobj;
    @Component
    public class Account
    {
    	private String lastName = null;
    	private String firstName = null;
    	private int age = 0;
    	private String position = null;
    	private String level = null;
    
    	public Account (String lastName, String firstName, int age,
    			String position, String level) 
    	{
    		this.lastName = lastName;
    		this.firstName = firstName;
    		this.age = age;
    		this.position = position;
    		this.level = level;
    	}
    }
    Teller.java
    Code:
    package com.simple.busobj;
    @Configurable(dependencyCheck=true)
    public class Teller
    {
    	@Autowired
    	private AccountService accountService;
    
    	public Teller()
    	{
    	}
    
    	public void debit(Account account, Double amount)
    	{
    		accountService.debit(account, amount);
    	}
    
    	public void deposit(Account account, Double amount)
    	{
    		accountService.deposit(account, amount);
    	}
    }
    AccountService.java
    Code:
    package com.simple.service;
    @Service
    public class AccountService
    {
    	private static Log log = LogFactory.getLog(AccountService.class);
    
    
    	public AccountService()
    	{
    	}
    
    	public void debit(Account account, Double amount)
    	{
    	 log.info("Debiting Account: [" + account + 
                   "] with Amount: [" + amount + "]");		
    	}
    
    	public void deposit(Account account, Double amount)
    	{
    		log.info("Depositing Account: [" + account + 
                     "] with Amount: [" + amount + "]");
    	}
    }
    Main.java
    Code:
    public class Main
    {
    	public static void main (String [] args) throws Exception
    	{
    	     ApplicationContext ctx = new 
                   ClassPathXmlApplicationContext("classpath:simple-di.xml");
    
    		Account account = new Account("Smith", "John", 50, "Manager", "Senior");
    
    
    		// expected dependency injection (DI) 
    		// AccountService object into
    		// the teller object, but didn't happen
    		Teller teller = new Teller();
    
    		teller.debit(account, new Double(50.99));
    	}
    }
    simple-di.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<bean id="accountService" class="com.fundserv.simple.service.AccountService" scope="prototype"/>
    
    	<context:spring-configured/>
    
    </beans>

    Here is the runtime error (NullPointerException)

    run:
    [java] [2010-02-03 21:43:40,297][support.ClassPathXmlApplicationContext][INFO ] Refreshing org.springframework.context.support.ClassPathXmlAp plicationContext@1df38fd: startup date [Wed Feb 03 21:43:40 EST 2010]; root of context hierarchy
    [java] [2010-02-03 21:43:40,368][xml.XmlBeanDefinitionReader][INFO ] Loading XML bean definitions from class path resource [simple-di.xml]
    [java] [2010-02-03 21:43:40,560][support.DefaultListableBeanFactory][INFO ] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@56f631: defining beans [accountService,org.springframework.context.config. internalBeanConfigurerAspect]; root of factory hierarchy
    [java] Exception in thread "main" java.lang.NullPointerException
    [java] at com.fundserv.simple.busobj.Teller.debit(Teller.jav a:22)
    [java] at Main.main(Main.java:21)

    [java] Java Result: 1

    BUILD SUCCESSFUL
    Total time: 0 seconds

  • #2
    I am using the latest spring release 3.0.0

    Comment


    • #3
      Hi

      First, your log should look like this:
      Code:
      07:20:46.683 INFO  {main} [xml.XmlBeanDefinitionReader] : Loading XML bean definitions from class path resource [grgr/test/spring/_4_aop_instrumentation/_2_instrumentation/AspectJTest-context.xml]
      07:20:46.834 INFO  {main} [support.GenericApplicationContext] : Refreshing [email protected]71e0: startup date [Thu Feb 04 07:20:46 CET 2010]; root of context hierarchy
      07:20:46.904 INFO  {main} [weaving.DefaultContextLoadTimeWeaver] : Found Spring's JVM agent for instrumentation
      [AppClassLoader@11b86e7] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified
      07:20:47.527 INFO  {main} [annotation.AutowiredAnnotationBeanPostProcessor] : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
      07:20:47.573 INFO  {main} [support.DefaultListableBeanFactory] : Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8e32e7: defining beans [org.springframework.context.weaving.AspectJWeavingEnabler#0,org.springframework.context.config.internalBeanConfigurerAspect,loadTimeWeaver,bean2,bean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
      this means, you have to run your application with the following option:
      Code:
      -javaagent:spring-instrument-3.0.0.RELEASE.jar
      Second - your code has two definitions of "accountService" - one with @Service and the second with <bean id="accountService" /> definition.

      Third - when you use scope="prototype" for beans which are injected to other beans, you use single instance. To use different instance for every teller.debit/deposit invocation, you should use the following definition of accountService:
      Code:
      <bean id="accountService" class="com.fundserv.simple.service.AccountService" scope="prototype">
         <aop:scoped-proxy />
      </bean>
      regards
      Grzegorz Grzybek

      Comment


      • #4
        ... and add the following to your XML definition:
        Code:
        <context:load-time-weaver />

        Comment


        • #5
          Thanks for the info.

          I followed your instructions by implementing LTW, but I got the following error: (my spring and aspectj-weaver are the latest releases)

          Code:
          run:
               [java] [2010-02-04 21:46:00,283][support.ClassPathXmlApplicationContext][INFO ] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@116ab4e: startup date [Thu Feb 04 21:46:00 EST 2010]; root of context hierarchy
               [java] [2010-02-04 21:46:00,354][xml.XmlBeanDefinitionReader][INFO ] Loading XML bean definitions from class path resource [simple-di.xml]
               [java] [2010-02-04 21:46:00,578][weaving.DefaultContextLoadTimeWeaver][INFO ] Found Spring's JVM agent for instrumentation
               [java] 4-Feb-2010 9:46:00 PM org.aspectj.weaver.tools.Jdk14Trace error
               [java] SEVERE: register definition failed
               [java] org.aspectj.weaver.BCException: Unable to continue, this version of AspectJ supports classes built with weaver version 3.0 but the class org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect is version 6.0
               [java] 
               [java] 	at org.aspectj.weaver.bcel.BcelAttributes.readAjAttributes(BcelAttributes.java:53)
               [java] 	at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:312)
               [java] 	at org.aspectj.weaver.bcel.BcelObjectType.<init>(BcelObjectType.java:142)
               [java] 	at org.aspectj.weaver.bcel.BcelWorld.buildBcelDelegate(BcelWorld.java:360)
               [java] 	at org.aspectj.weaver.bcel.BcelWorld.resolveDelegate(BcelWorld.java:343)
               [java] 	at org.aspectj.weaver.ltw.LTWWorld.resolveDelegate(LTWWorld.java:97)
               [java] 	at org.aspectj.weaver.World.resolveToReferenceType(World.java:378)
               [java] 	at org.aspectj.weaver.World.resolve(World.java:271)
               [java] 	at org.aspectj.weaver.bcel.BcelWeaver.addLibraryAspect(BcelWeaver.java:165)
               [java] 	at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.registerAspects(ClassLoaderWeavingAdaptor.java:399)
               [java] 	at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.registerDefinitions(ClassLoaderWeavingAdaptor.java:240)
               [java] 	at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.initialize(ClassLoaderWeavingAdaptor.java:152)
               [java] 	at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.initialize(Aj.java:151)
               [java] 	at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.getWeavingAdaptor(Aj.java:156)
               [java] 	at org.aspectj.weaver.loadtime.Aj$WeaverContainer.getWeaver(Aj.java:122)
               [java] 	at org.aspectj.weaver.loadtime.Aj.preProcess(Aj.java:73)
               [java] 	at org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter.transform(ClassPreProcessorAgentAdapter.java:52)
               [java] 	at org.springframework.context.weaving.AspectJWeavingEnabler$AspectJClassBypassingClassFileTransformer.transform(AspectJWeavingEnabler.java:97)
               [java] 	at org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver$FilteringClassFileTransformer.transform(InstrumentationLoadTimeWeaver.java:181)
               [java] 	at sun.instrument.TransformerManager.transform(TransformerManager.java:169)
               [java] 	at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:365)
               [java] 	at java.lang.ClassLoader.defineClass1(Native Method)
               [java] 	at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
               [java] 	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
               [java] 	at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
               [java] 	at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
               [java] 	at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
               [java] 	at java.security.AccessController.doPrivileged(Native Method)
               [java] 	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
               [java] 	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
               [java] 	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
               [java] 	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
               [java] 	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
               [java] 	at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:640)
               [java] 	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:401)
               [java] 	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
               [java] 	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
               [java] 	at Main.main(Main.java:11)
          Last edited by zollen; Feb 4th, 2010, 09:10 PM. Reason: [SOLVED]

          Comment


          • #6
            org.springframework.context-3.0.0.RELEASE.jar
            org.springframework.orm-3.0.0.RELEASE.jar
            org.springframework.core-3.0.0.RELEASE.jar
            org.springframework.context.support-3.0.0.RELEASE.jar
            org.springframework.beans-3.0.0.RELEASE.jar
            cglib-2.2.jar
            org.springframework.web.servlet-3.0.0.RELEASE.jar
            org.springframework.oxm-3.0.0.RELEASE.jar
            org.springframework.jdbc-3.0.0.RELEASE.jar
            org.springframework.expression-3.0.0.RELEASE.jar
            org.springframework.aspects-3.0.0.RELEASE.jar
            asm-3.2.jar
            org.springframework.web-3.0.0.RELEASE.jar
            org.springframework.asm-3.0.0.RELEASE.jar
            org.springframework.aop-3.0.0.RELEASE.jar
            log4j-1.2.15.jar
            commons-dbcp-1.2.2.jar
            commons-pool-1.5.4.jar
            commons-logging-1.1.1.jar
            org.springframework.transaction-3.0.0.RELEASE.jar
            jstl.jar
            standard.jar
            org.springframework.instrument-3.0.0.RELEASE.jar
            com.springsource.org.aopalliance-1.0.0.jar
            aspectjrt.jar (version: 1.5.4-1)
            aspectjweaver.jar (veresion: 1.5.4-1)

            Comment


            • #7
              my mistake: aspectj 1.6.8 is the latest.

              I have been using a 2 years old library....

              Comment


              • #8
                Does this means it works now?

                Comment


                • #9
                  Originally posted by Grzegorz Grzybek View Post
                  Does this means it works now?
                  Yep. it works now. Many thanks!

                  Comment

                  Working...
                  X