2011-09-23 82 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