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:




    Vaadin production mode</description>
    <servlet-name>Scalatest Application</servlet-name>
        <description>Vaadin application class to start</description>
    <servlet-name>Scalatest Application</servlet-name>

application context:

<bean id="main" class="com.example.scalatest.ScalaApp">
    <property name="dataSource" ref="dataSource"/>
<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"/>

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?


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

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: