Vaadin7 与 mybatis 和 Spring

Vaadin7 with mybatis and Spring

我是 Vaadin 和 Mybatis 的新手-Spring.

我的 UI class 无法使用 @Autowired 注释调用服务 class。我已经为服务 class.

添加了@Service

我的看法:

@SuppressWarnings("serial")
@Theme("test")
@Component
@Scope("prototype")
@SpringView(name = TestUI.ID)
public class TestUI extends UI {

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = TestUI.class)
    public static class Servlet extends VaadinServlet {
    }
@Autowired 
    UserService service;    
    UserModel model;

我的应用程序 context.xml 在资源文件夹下。

<ct:annotation-config/>
<ct:component-scan base-package="com.example.test" />
<ct:component-scan base-package="com.example.service" />
<ct:component-scan base-package="com.example.model" />
<ct:component-scan base-package="com.example.mapper" />

DevContext.xml

<context:annotation-config />
<bean id="DEVdataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url"
        value="" />
    <property name="" value="" />
    <property name="" value="" />
    <property name="" value="" />
    <property name="maxActive" value="50" />
    <property name="validationQuery" value="select 1 from dual"/>
    <property name="testOnBorrow" value="true"/>
    <property name="connectionInitSqls">
        <list>
            <value>ALTER SESSION SET CURRENT_SCHEMA=null</value>
        </list>
    </property>
</bean>

<!-- setup mybatis bean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="DEVdataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="com.example.mapper.Mapper" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

<!-- automatically find mappers -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.example.mapper.**.mapper" />
</bean>

服务DevContext.xml

<context:component-scan base-package="com.example"></context:component-scan>

Web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:resources/applicationContext.xml</param-value>
</context-param>

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
           org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </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>

代码无效,请帮我弄清楚我错过了什么

尝试在 UI subclass 上使用 @SpringUI 而不是 @SpringView,并且您还需要使用 SpringVaadinServlet 而不是 [=13] =],最好的方法可能只是删除继承自 VaadinServlet 的静态 Servlet class,spring 插件将改用正确的 servlet。

我还建议去掉 xml-config,除非你碰巧喜欢它。 Spring start.spring.io 支持使用 Vaadin 引导,这是 bootstrap 项目文件和配置的一种非常简单的方法。