Vaadin7 with mybatis and Spring

270 Views Asked by At

i am new to Vaadin and Mybatis-Spring.

My UI class could not invoke the Service class using the @Autowired annotation. I have appended @Service for the service class.

My view:

@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;

My application context.xml is under the resources folder.

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

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

The code is not working, kindly help me to figure out what i missed

1

There are 1 best solutions below

0
On BEST ANSWER

Try to use @SpringUI instead of @SpringView on the UI subclass, and you also need to use SpringVaadinServlet instead of VaadinServlet, the best is probably just to remove your static Servlet class that inherits from VaadinServlet, and the spring addon will use the correct servlet instead.

I would also recommend to get rid of the xml-config, unless you happen to like it. Using Spring Boot with Vaadin is supported on start.spring.io, it's a pretty easy way to bootstrap your project files and config.