1

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.

1
  • The close vote is even weirder. Commented Jun 25, 2013 at 21:13

1 Answer 1

1

You need to use the Struts 2 Spring plugin, otherwise Spring has no idea it should be doing anything with the S2 actions. Once you drop the S2 plugin in (using Maven so the other dependencies come automagically) you're basically done after configuring the Spring context loader listener.

You do not need to define your actions in the Spring config file, but you may. Whether or not you need to depends on how you're doing your wiring, e.g., if you're auto-wiring by name, you can skip it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Dave! I downloaded and added the .jar to the project and it instantly worked! For anyone who comes across this later and also needs the jar, get it here: mvnrepository.com/artifact/org.apache.struts/…
@user2507377 I'd recommend strongly against downloading random libraries: please use a dependency management tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.