2011-09-23 84 views
0

修訂JavaConfig問題的Web應用程序(Vaadin +春季)

我發現了一個可疑的日誌條目:

org.springframework.beans.factory.wiring.BeanConfigurerSupport: 

BeanFactory has not been set on BeanConfigurerSupport: Make sure this configurer runs in a Spring container. 
Unable to configure bean of type [com.mycompany.projectname.App]. Proceeding without injection. 

/更新

我工作的一個Vaadin + Spring應用程序,我希望使用JavaConfig。

據一些教程,我建起來分開,但是當我將它們合併,我得到了如下(見第一codesnipet App.java - logger.info(「>>主窗口爲空」);)

app postconstruct --------- 
mainWindow is null 

我嘗試了幾個變體的配置,例如在applicationContext等等。

所以我的問題是:我怎麼才能找到真正的問題?

感謝您的時間和精力提前。

喬鮑

App.java

@Configurable 
public class App extends Application 
{ 
    public MainWindow mainWindow;  
    public MainWindow getMainWindow(... 
    public void setMainWindow(Main.... 


    @PostConstruct 
    public void init() 
    { 
     logger.info(">>app postconstruct --------- "); 
     if(mainWindow==null) logger.info(">>mainWindow is null"); 
     else logger.info(">>mainWindow is not null"); 
    } 

AppConfig.java

@Configuration 
    public class AppConfig { 
    ... 
    @Bean 
    public MainWindow mainWindow(){ 
      return new MainWindow(); 
    } 
    ....  
    } 

的web.xml在此基礎上!tutorial link!

... 
<context-param> 
<param-name>contextClass</param-name> 
<param-value> 
org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
</param-value> 
</context-param> 

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>com.mycompany.projectname.config.AppConfig</param-value> 
</context-param> 
... 

<servlet> 
    <servlet-name>Vaadin Application Servlet</servlet-name> 
    <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class> 

    <init-param> 
    <description>Vaadin application class to start</description> 
    <param-name>application</param-name> 
    <param-value>com.mycompany.projectname.App</param-value> 
    </init-param> 

    <init-param> 
    <param-name>contextClass</param-name> 
    <param-value> 
    org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
    </param-value> 
    </init-param> 

    <init-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>com.mycompany.projectname.config.AppConfig</param-value> 
    </init-param>  
</servlet> 

回答

2

你爲什麼要使用@Configurabl è?你的意思是要使用@Component嗎?

@Configurable通常用於域對象(又名實體),它們不是Spring管理的對象,它們允許它們從Spring容器接收依賴注入。這似乎不是你的目標。您應該簡單地將您的「App」類作爲另一個@Bean方法進行接線,或者用@Component標記它並通過組件掃描(例如使用@ComponentScan)進行拾取。查看這些註釋的相關Javadoc和參考文檔以獲取更多信息。

+0

你好,** 1。**我跟着一個教程,使得Vaadin應用程序類成爲Spring容器。所以應用程序類關注於Vaadin和Spring之間的聯繫。而這個類是由atConfigurable註釋的,這就是爲什麼我不敢去除:) ** 2。**我根據spring doc [link](http://goo.gl/qlpQf)做了一個測試項目, atComponent沒有註釋類,儘管在那裏使用了組件掃描。另外,測試項目不需要atComponent註解[pic](http://img684.imageshack.us/img684/4163/image000m.png)。 – cscsaba

+0

雖然在嘗試多次嘗試使用atComponent註解之後,類必須連線。但它沒有奏效。很明顯我做錯了。我接受你的話,並感謝你的建議。 – cscsaba

+0

(我的javaconfig測試用例中的一個類[link](http://test-workbench.googlecode.com/svn/projects/vs2/tags/0/src/main/java/com/mycompany/MavenVaadinSpring/service /UserManagerImpl.java)) – cscsaba

1

如果您未啓用註釋,則註釋將不起作用。

在項目的spring context xml中是否有以下內容?

<!-- Activate Spring annotation support --> 
<context:spring-configured/> 

<!-- Turn on @Autowired, @PostConstruct etc support --> 
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> 

<!-- Scans the classpath of this application for @Components to deploy as beans --> 
<context:component-scan base-package="com.tc.poc.vaddinspring" /> 

更新:看this skeleton

+0

您好弗拉基米爾,對於遲到的回覆感到抱歉,但正如我所知,組件掃描足以Autowire組件。但我認爲真正的問題是web.xml的錯誤設置,其中org.springframework.web.servlet.DispatcherServlet可以處理contextClass,contextConfigLocation參數[**參見**](http://static.springsource.org/spring/文檔/ 3.0.x的/彈簧的框架參考/ HTML/beans.html#豆-java的)。在我的情況下(見上面的web.xml片段)com.vaadin.terminal.gwt.server.ApplicationServlet不能使用它們。 – cscsaba

+0

糾正我,如果我錯了。 – cscsaba

+0

你好弗拉基米爾,儘管我以前的觀點是必要的,對不起。我要檢查你提到的skelton。感謝你的努力,真的。 – cscsaba