[英]@Autowired doesn't work in Spring and Vaadin integration
我正在尝试将Spring
与Vaadin
集成在一起,但是不能在我的Vaadin类中使用@Autowired
批注。
首先,我创建了以下Maven结构
这是我的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");
}
}
最后,我有我的面板要在其中注入服务
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>
我所知道的,
Injector.inject(this);
不是Spring提供的东西,您可能将其与其他框架混合使用。
相反,我将使UserPanel
成为一个原型范围内的Spring组件(这意味着它可以由Spring实例化并自动装配,但是您仍需对其生命周期负责)。
@Component
@Scope(SCOPE_PROTOTYPE)
public class UserPanel extends VerticalLayout {
请注意, UserPanel
从上下文中检索到UserPanel
(例如,自动装配到另一个对象中),就会创建一个新实例。 最好通过显式调用自己控制实例的创建
context.getBean(UserPanel.class)
在正确的时间。
AnnotationConfigWebApplicationContext
不能与XML配置一起使用。 它应该与@Configuration
注释的类一起使用。
由于您在Spring配置中使用的是xml文件,因此您应该改为从web.xml中删除这些行:
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
然后默认情况下,Spring将使用XmlWebApplicationContext
并拾取您的xml文件。
我尝试过与您相同的情况。 最后,解决方案在Vaadin文档中 。
似乎问题在于我们使用的是Vaadin上下文而不是Spring上下文,因此我们需要执行文档(SpringHelper)中显示的技巧来获取任何bean。 在Spring上下文之外,自动装配无法正常工作。
有一个用于通过注释启用自动装配的插件: http : //vaadin.xpoft.ru/ 。 这是Vaadin网站的链接: http : //vaadin.com/addon/springvaadinintegration 。 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.