Vitamon
Vitamon

Reputation: 548

Scala with Spring: Constructor autowiring

I'm looking for "idiomatic" way to autowire Scala classes with Spring through constructor injection. I've tried something like this:

   @Component
   class MyService @Autowired() ( val myDao: MyDao) extends Logging {
   ...
   }

But I get an error: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [MyService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: MyService.() at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:964) ~[spring-beans-3.0.7.RELEASE.jar:3.0.7.RELEASE]

Upvotes: 2

Views: 3228

Answers (2)

sksamuel
sksamuel

Reputation: 16387

I've added support for @Autowired on the constructor of a Scala object into the spring-scala project. This is only (at time of writing) in the nightly snapshot.

This allows you to do

@Component
@Autowired
class Service(dep : Dependency) { }

https://github.com/spring-projects/spring-scala

Upvotes: 6

Noah
Noah

Reputation: 13959

We use this style for a Spring MVC app:

  @Component
  class MyService extends Logging {

    @Autowired
    private val myDao: MyDao = null

  }

Upvotes: 0

Related Questions