I am new to Spring and trying to get things rolling. I would like to use xml setter-based dependency injection and avoid '@whatever' markup in the Java.
I'm using NetBeans 7 with Spring 3.1.1 and I have some Struts 2 code in the project as well that works fine.
The problem I have is that the dependency injection doesn't seem to work at all (accountService remains NULL), so I'm guessing I'm not configured properly. Any help would be appreciated!
BaseActionUserAware is the class that needs the injection:
public class BaseActionUserAware extends BaseAction
{
protected AccountService accountService;
protected String username;
@Override
public String execute() {
super.execute();
// accountService = new AccountServiceImpl();
Object accountID = session.get(SessionProperties.ACCOUNT_ID);
if(null != accountID) {
AccountBean account = accountService.getAccount((int)accountID);
username = account.getUsername();
}
return SUCCESS;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
public String getUsername() {
return username;
}
}
... the commented code indicates what I'd like to accomplish; I'd like accountService to be injected with AccountServiceImpl.
Here's my WEB-INF/applicationContext.xml:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="accountService" class="hyperbook.service.impl.AccountServiceImpl"/>
<bean id="baseActionUserAware" class="hyperbook.action.BaseActionUserAware">
<property name="accountService" ref="accountService"/>
</bean>
</beans>
... I've double checked the fully-qualified class names, those are correct.
I've recently added the following to my WEB-INF/web.xml file, but it didn't help at all:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
Again, any help would really be appreciated. Thanks!!
[EDIT] I should also mention - BaseActionUserAware is never specifically instantiated anywhere in the Java. Rather, it is used exclusively for Struts 2 action classes to extend from. Therefore, the baseActionUserAware definition in applicationContext.xml might be totally wrong/unneeded. I just don't know enough about Spring yet.