2013-07-11 1 views
0

를 주입 : -멀티 모듈 받는다는 스프링 내가 두 받는다는 모듈이 콩

  • 웹 (봄 자연과 angel이라는 프로젝트를) .JAR하기 위해 포장

    1. 로직 (콩 등 봄 프로젝트) - 패킷을 .war

    웹 .pom에서 나는 논리에 의존하고있다. 자바 코드 autowired 등 괜찮습니다. 웹 프로젝트의 로직에서 빈을 사용하고 싶습니다.

    내 web.xml 파일 (웹 프로젝트) : 경로 : ../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은 로직 모듈 콩에 대한 구성을 포함한다. 기본 패키지 이름은 b2b입니다. UI 클래스에서

    내가 가진 :

    @Autowired 
    private CompanyService companyService; 
    

    나는 콩을 얻기 위해 여러 가지 방법으로 노력하지만, 항상 companyService은 시작 웹 후 null입니다.

    로직 모듈에서 bean을 웹 모듈에 표시하려면 무엇을 추가해야합니까?

    UI 클래스 :

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

    angel이라는 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); 
    } 
    

    } 당신은 당신의 UI 클래스를 발표하지 않았다

  • 답변

    0

    . 나는 UI가 Spring 관리 빈이 아닌 것으로 의심해 autowiring이 꺼져있다. 또는 귀하의 UI는 봄 콩으로 주석 처리되지만, 새로운 연산자로 생성되며 스프링 컨텍스트에서 가져 오지 않습니다.

    글쎄, 만약 당신이 설명했다, 그것을 작동해야합니다. 무자비. 간단한 프로젝트를 만들었습니다. https://github.com/michaldo/multi-module-vaadin-spring 그리고 작동합니다.

    0

    뷰 (UI)와 컨트롤러 (뷰와 로직 클래스 사이의 중간 클래스)로 UI를 나눌 수 있습니다. 컨트롤러는 표준 방식, 즉 연산자로보기에서 생성 할 수 있습니다.

    그런 다음 @Configurable와 컨트롤러 클래스에 주석 :

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

    구성은 Spring 컨텍스트에서 만든이 콩은 여전히 ​​의존성 주입의 대상이되어야한다고 봄을 알려줍니다.이 일을하려면, 당신은 당신의 클래스 패스에 AspectJ를 런타임이 필요합니다 + 빌드에 AspectJ를 컴파일 단계를 추가 - 변화의 pom.xml을 UI 프로젝트에 대해 다음과 같이

    <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> 
    
    내 프로젝트에서 나는이 방법을 사용하고

    과 꽤 잘 작동합니다.