【问题标题】:multi module maven spring inject beans多模块maven spring注入bean
【发布时间】:2013-07-10 02:40:25
【问题描述】:

我有两个 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 总是为空。

我应该添加什么才能从 Web 模块中可见的逻辑模块中获取 bean?

界面类:

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

我在这里也提到了 vaadin V7:enter link description here

但无济于事。

这是我的 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);
}

}

【问题讨论】:

    标签: spring maven dependency-injection module vaadin


    【解决方案1】:

    你没有展示你的 UI 类。我怀疑 UI 不是 Spring 托管的 bean,因此自动装配已关闭。或者您的 UI 被注释为 Spring bean,但使用 new 运算符创建,而不是取自 Spring 上下文。

    好吧,如果你按照你的解释做了,它一定能工作。没有怜悯。我做了一个简单的项目https://github.com/michaldo/multi-module-vaadin-spring,它可以工作

    【讨论】:

      【解决方案2】:

      您可以将 UI 拆分为 2 层 - 视图 (UI) 和控制器(视图和逻辑类之间的中间类)。您的控制器可以通过标准方式从视图中创建,即使用 new 运算符。

      然后用@Configurable注解你的控制器类:

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

      Configurable 告诉 Spring 这个在 Spring 上下文中创建的 bean 应该仍然是依赖注入的主题。为了让这个工作,你需要在你的类路径中添加 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> 
      

      我在我的项目中使用了这种方法并且效果很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多