java - @Autowired 在 Spring 和 Vaadin 集成中不起作用

标签 java spring vaadin

我正在尝试将 SpringVaadin 集成,但我无法在我的 Vaadin 类中使用 @Autowired 注释。

首先,我创建了以下maven结构

enter image description here

这是我的web.xml

<web-app>
<display-name>Vaadin Web Application</display-name>
<context-param>
<description>Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>

<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>/WEB-INF/application-context.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class> org.springframework.web.context.request.RequestContextListener </listener-class>
</listener>

<servlet>
<servlet-name>Vaadin Application Servlet</servlet-name>
<servlet-class>com.mycompany.config.AutowiringApplicationServlet</servlet-class>

<init-param>
<description>Vaadin UI to display</description>
<param-name>UI</param-name>
<param-value>com.mycompany.ui.MyUI</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>Vaadin Application Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

这是我的application-context.xml

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost/vaadin" />
<property name="username" value="postgres" />
<property name="password" value="tobbis" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="persistenceUnitName" value="myUnit"/>
</bean>


<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<jpa:repositories base-package="com.mycompany.repository"></jpa:repositories>

<context:annotation-config/>
<context:component-scan base-package="com.mycompany" />

现在我创建了位于 com.mycompany.services 包中的 UserService

@Service
public class UserService {

public void saveUser(User user){

    System.out.println("Test to Save");

}
}

最后,我有我想要注入(inject)服务的面板

public class UserPanel extends VerticalLayout {


@Autowired
UserService service;

public UserPanel() {
    // TODO Auto-generated constructor stub
    Injector.inject(this);

    service.saveUser();
}

}

但结果总是一样的

 Error creating bean with name 'com.mycompany.ui.UserPanel': Injection of autowired dependencies failed; 
 nested exception is org.springframework.beans.factory.BeanCreationException:
 Could not autowire field: com.mycompany.services.UserService com.mycompany.ui.UserPanel.service; 
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
 No matching bean of type [com.aiem.services.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

最佳答案

尝试在 applicationContext.xml 中声明您的UserService

<bean name="userService" class="com.mycompany.services.UserService"></bean>

关于java - @Autowired 在 Spring 和 Vaadin 集成中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345810/

相关文章:

spring - 如何从 JBoss AS 7 中的消息驱动 bean 访问 Spring bean

java - Spring中的日期时间格式

java - 如何根据 vaadin 中的操作重新加载或刷新选项卡的内容

mysql - 无论我做什么,Vaadin的combobox.select()都不起作用

java - 从 Windows 批处理文件调用 Apache Camel Spring Boot 应用程序

Java服务器socket程序一天后停止监听

java - 单击 anchor 标记时执行 JSTL 条件

java - 如何将 Spring Boot 执行器添加到 WAR 项目

java - Vaadin 流多选组合框

java - Vaadin 8 - 如何绑定(bind) RadioButtonGroup 的项目?