2

私はいくつかのサブモジュールを含むアプリケーションに取り組んでいます。これらのモジュールの1つに、次のようなヘッダー付きのサービスがあります。

@Transactional
@Service("mySimpleService")
public class MySimpleServiceImpl implements MySimpleService {}

別のモジュールには、MySimpleServiceメソッドの1つを使用したいコントローラーがあります。だから私はこれに似たものを持っています:

@Controller
public class EasyController {    

    @Autowired
    private MySimpleService mySimpleService;

    @RequestMapping("/webpage.htm")
    public ModelAndView webpage(@RequestParam,
                                @RequestParam,
                                HttpServletRequest request,
                                HttpServletResponse response) {
        ModelAndView mav = new ModelAndView(”webpage”);
        mav.addObject(”name”, mySimpleService.getObject());
        return mav;
    }
}

回線上mav.addObject(”name”, mySimpleService.getObject());で取得してNullPointerExceptionいます。どうしてか分かりません。と同様のエラーは発生しませんCould not autowire fieldが、のみ発生しますNullPointerException

また、この方法で独自のインターセプターを作成して、タスクの別のアイデアを確認しました。

public class CustomInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private MySimpleService mySimpleService;

    @Override
    public boolean preHandle(HttpServletRequest request, 
                             HttpServletResponse response,
                             Object handler)
                             throws Exception {         
        request.setAttribute("attr", mySimpleService.getAttribute());
        return true;
    }    
}

もちろん、このインターセプターはディスパッチャーサーブレットで宣言されます。私のログではNullPointerException、次の行で確認できますrequest.setAttribute("attr", mySimpleService.getAttribute());。したがって、これは別のアプリケーションモジュールからサービスにアクセスする際の問題です。

どうすればこれを達成できますか?

JNDIサービスでEJBをビルドし、 Mavenを使用または使用してEJBを共有するなどのアイデアについて聞いたことがあります。アプリケーションのビルド中に、サービスでフォルダーをターゲットモジュールにコピーします。2番目のオプションを実行しようとしましたが、機能せず、対処が成功したかどうかを確認できません。Mavenベースのソリューションを確認することは可能ですか?別の提案がありますか?

編集

これは、ターゲットモジュールディスパッチャーサーブレットからのコンポーネントスキャンです。

<context:component-scan base-package="my.package.target.module.controller" annotation-config="false"/>
4

1 に答える 1

4

あなたの問題はannotation-config="false"だと思います。annotation-config機能は、@Autowiredアノテーションをオンにする機能です。自動配線がないと、mySimpleServiceプロパティは挿入されず、nullのままになります。次に、NullPointerExceptionが発生します。

試す

<context:component-scan base-package="my.package.target.module.controller" annotation-config="true"/>

ところで-私はスキャンを行わないので、この正確な構成を使用したことはありません。私は常に別の要素を使用します:

<context:annotation-config />
于 2012-10-18T18:50:27.790 に答える