Web 应用程序中的 JavaConfig 问题 (Vaadin + Spring)

发布于 2024-12-06 09:08:29 字数 2871 浏览 0 评论 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。

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

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

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

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

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

Csaba

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

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

UPDATED

I found an suspicious log entry:

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.

/UPDATED

I'm working on an Vaadin + Spring application and I wish to use JavaConfig.

According to some tutorial I built them up separately, but when I merge them I got the following (see the first codesnipet App.java - logger.info(">>mainWindow is null");)

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

I tried several variation of configs for example in applicationContext and so forth.

So my question is: how can I find out real problem ?

Thanks for your time and effort in advance.

Csaba

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 based on this !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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远昼 2024-12-13 09:08:29

你为什么使用@Configurable?您的意思是使用@Component 来代替吗?

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

Why are you using @Configurable? Do you mean to be using @Component instead?

@Configurable is typically used on domain objects (aka entities) that are not otherwise Spring-managed objects to allow them to receive dependency injection from the Spring container. This does not appear to be your goal. You should probably simply be wiring up your "App" class as another @Bean method, or otherwise marking it with @Component and picking it up via component-scanning (e.g. with @ComponentScan). Check out the related Javadoc and reference documentation for these annotations for more info.

久伴你 2024-12-13 09:08:29

如果您未启用注释,您的注释将不起作用。

您的项目 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" />

更新:查看此内容骨架

Your annotations will not work if you didn't enabled them.

Do you have the following in your project 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" />

Update: Look at this skeleton.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文