0

更新

我发现了一个可疑的日志条目:

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。

根据一些教程,我分别构建了它们,但是当我合并它们时,我得到了以下内容(参见第一个代码片段 App.java - logger.info(">>mainWindow is null");)

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

我尝试了几种配置变体,例如在 applicationContext 等中。

所以我的问题是:我怎样才能找出真正的问题?

提前感谢您的时间和精力。

乔巴

应用程序.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 基于此!教程链接!

...
<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>
4

2 回答 2

2

你为什么使用@Configurable?你的意思是改用@Component 吗?

@Configurable 通常用于不是 Spring 管理的对象的域对象(也称为实体),以允许它们从 Spring 容器接收依赖注入。这似乎不是您的目标。您可能应该简单地将您的“App”类连接为另一个@Bean 方法,或者用@Component 标记它并通过组件扫描(例如使用@ComponentScan)来获取它。查看这些注释的相关 Javadoc 和参考文档以获取更多信息。

于 2011-09-27T02:48:25.810 回答
1

如果您没有启用它们,您的注释将不起作用。

您的项目 spring 上下文 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" />

更新:看看这个骨架

于 2011-09-29T14:44:31.743 回答