1

私はSpringが初めてで、物事を進めようとしています。XML セッターベースの依存性注入を使用して、Java で「@whatever」マークアップを回避したいと考えています。

私は、Spring 3.1.1 で NetBeans 7 を使用しています。プロジェクトには、正常に動作する Struts 2 コードもいくつかあります。

私が抱えている問題は、依存性注入がまったく機能していないように見えることです (accountService は NULL のままです)。そのため、適切に構成されていないと推測しています。どんな助けでも大歓迎です!

BaseActionUserAware は、注入が必要なクラスです。

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;
    }
}

...コメントされたコードは、私が達成したいことを示しています。accountService に AccountServiceImpl を注入したいと考えています。

ここに私の 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>

... 完全修飾クラス名を再確認しましたが、正しいです。

最近、WEB-INF/web.xml ファイルに以下を追加しましたが、まったく役に立ちませんでした。

    <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>

繰り返しますが、どんな助けでも本当に感謝しています。ありがとう!!

[編集] また、言及する必要があります- BaseActionUserAware は、Java のどこでも具体的にインスタンス化されることはありません。むしろ、Struts 2 アクション クラスの拡張元として排他的に使用されます。したがって、applicationContext.xml の baseActionUserAware 定義は、完全に間違っているか不要である可能性があります。私はまだSpringについて十分に知りません。

4

1 に答える 1

1

Struts 2 Spring プラグインを使用する必要があります。そうしないと、Spring は S2 アクションで何かを行う必要があることを認識できません。S2 プラグインを (Maven を使用して他の依存関係が自動的に来るように) ドロップすると、Spring コンテキストローダーリスナーを構成した後、基本的に完了です。

Spring 構成ファイルでアクションを定義する必要はありません、 . 必要があるかどうかは、ワイヤリングをどのように行っているかによって異なります。たとえば、名前で自動ワイヤリングしている場合は、スキップできます。

于 2013-06-25T21:12:14.560 に答える