I have been experimenting with domain objects dependency-injection(DI) but without much success. I am using the latest spring 3.0.0. Here are the codes:
Account.java
Teller.javaCode: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; } }
AccountService.javaCode: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); } }
Main.javaCode: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 + "]"); } }
simple-di.xmlCode: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)); } }
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


Reply With Quote
