0

私は2つのmavenモジュールを持っています:

  1. ロジック (Bean などを使用した Spring プロジェクト) - .jar にパック

  2. Web (春の性質を持つ Vaadin プロジェクト) - .war へのパケット

Web .pom では、ロジックに依存しています。Java コードでは autowired などは問題ありません。Web プロジェクトでロジックから Bean を使用したい。

私の web.xml (Web プロジェクト): パス: ../src/main/webapp/WEB-INF

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml

    </param-value>
</context-param>

私のアプリケーション コンテキスト: (パス: ../src/main/webapp/WEB-INF)

<beans ...
<context:annotation-config />

<import resource="logic.xml"/>

<context:component-scan base-package="b2b"
    annotation-config="true" />

logic.xml には、ロジック モジュールの Bean の構成が含まれています。基本パッケージ名は b2b です。

UIクラスには次のものがあります:

@Autowired
private CompanyService companyService;

私はさまざまな方法で Bean を取得しようとしますが、Web を開始した後は常に companyService が null になります。

ロジック モジュールの Bean を Web モジュールで表示するには、何を追加すればよいですか?

UI クラス:

@Theme("mytheme")
@SuppressWarnings("serial")
@Controller
public class MyVaadinUI extends UI
{ }

ここで vaadin V7 についても言及しています。ここにリンクの説明を入力してください

しかし、助けにはなりません。

これは私のUIクラスです:

enter code here
@Theme("mytheme")
@SuppressWarnings("serial")
@Controller
public class MyVaadinUI extends UI
{

 SpringContextHelper helper = new SpringContextHelper(VaadinServlet.getCurrent().getServletContext());

private static Logger LOGGER = Logger.getLogger(MyVaadinUI.class);

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "b2b.frontend.AppWidgetSet")
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {
    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);
    final CompanyService companyService = (CompanyService) helper.getBean("companyService");

    Button button = new Button("Click Me");
    button.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            //layout.addComponent(new Label("Thank you for clicking"));

            LOGGER.info("pushed the button");
            layout.addComponent(new Label("aa " +companyService +" Thank you for clicking"));

        }
    });
    layout.addComponent(button);
}

}

4

2 に答える 2

0

UI をビュー (UI) とコントローラー (ビューとロジック クラスの中間クラス) の 2 つのレイヤーに分割できます。コントローラーは、ビューから標準的な方法で、つまりnew演算子で作成できます。

次に、コントローラー クラスに @Configurable のアノテーションを付けます。

@Configurable
public class MyController 
{
   @Autowired
   private MyService service;
}

Configurable は、Spring のコンテキストから作成されたこの Bean は引き続き依存性注入の対象にする必要があることを Spring に伝えます。これを機能させるには、クラスパスに AspectJ ランタイムが必要です + ビルドに aspectj コンパイル ステージを追加します - UI プロジェクトの pom.xml を次のように変更します。

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${aspectj.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- Aspectj plugin to make Spring aware of non-managed
        beans and support autowiring -->
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.4</version>
      <configuration>
        <!-- Required as the plugin does not resolve this by default -->
        <source>1.6</source>
        <target>1.6</target>
        <aspectLibraries>
          <aspectLibrary>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
          </aspectLibrary>
        </aspectLibraries>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <goal>test-compile</goal>
          </goals>
        </execution>
      </executions>
    </plugin> 

私は自分のプロジェクトでこのアプローチを使用しており、かなりうまく機能しています。

于 2013-07-22T18:49:05.483 に答える
0

UI クラスを提示していません。UI は Spring 管理の Bean ではないので、自動配線がオフになっていると思われます。または、UI に Spring Bean として注釈が付けられていますが、Spring コンテキストから取得されたのではなく、新しい演算子で作成されています。

まあ、あなたが説明したようにそれを作ったなら、それはうまくいくはずです. 容赦ない。簡単なプロジェクトhttps://github.com/michaldo/multi-module-vaadin-springを作成しましたが、動作します

于 2013-07-11T20:57:59.430 に答える