I am trying to use spring application context and spring di in my scala vaadin app, but can't get a datasource to be injected

i am new to scala and vaadin, i'm just experimenting. I am trying to use spring application context and spring di in my scala vaadin app, but can't get a datasource to be injected. I dont know, maybe i'm doing something fundamentally wrong but here's my code:

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
    </param-value>
</context-param>

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

<context-param>
    <description>
    Vaadin production mode</description>
    <param-name>productionMode</param-name>
    <param-value>false</param-value>
</context-param>
<servlet>
    <servlet-name>Scalatest Application</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.example.scalatest.ScalaApp</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Scalatest Application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

application context:

<bean id="main" class="com.example.scalatest.ScalaApp">
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="url"/>
    <property name="username" value="root"/>
    <property name="password" value="pass"/>
</bean>

and in my scala class

var dataSource:DataSource = _;  
def setDataSource(datasource:DataSource){
dataSource = datasource;
}

Isn't working, ds is null at launch. Can anyone guide me please?

Answers


The main problem you have is your application (ScalaApp) is not instantiated by Spring container but by VaadinServlet - make sure it does. There are several strategies. Here is the example project that can help you: https://github.com/archiecobbs/dellroad-stuff-vaadin-spring-demo3

Few more pieces of advice...

Instead of writing a setter yourself, add @BeanProperty annotation to your variable. Scala compiler will generate setter and getter for your variable:

@BeanProperty private var dataSource:DataSource = _

There is even better way - using Spring's annotation based container configuration. If you have only one bean of type DataSource in your context, simply add @Autowired to your variable (no need for xml definition in the context file - your class should be annotated as a @Component ):

@Component
class ScalaApp {
   @Autowired private var dataSource:DataSource = _ 
}

Here is more information: http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-annotation-config


Need Your Help

How can I create unique id's for jQuery widget instances?

jquery widget unique instance

I am creating a rich text editor based on jQuery widget, and it can have multiple instances on a page. The first instance should generate a toolbar like this: