我的问题类似this question。我问如何在Spring-Scala中做到这一点。我想用:
创建一个原型val applicationContext = FunctionalConfigApplicationContext(classOf[MyConfiguration])
val myService = applicationContext.getBean(classOf[MyService])
如何使用getBean
方法传递参数?
答案 0 :(得分:0)
目前还没有(Spring-Scala 1.0.0.RC1)使用FunctionalConfigApplicationContext
执行此操作的方法,因为MyConfiguration
需要属于FunctionalConfiguration类型并没有实现getBeans(name,paramsArray)
风格,这是链接问题中提出的解决方案。
您始终可以使用XML配置执行此操作。
这里的一个工作示例:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dimitrisli.scala</groupId>
<artifactId>ScalaSpringIntellijTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.scala</groupId>
<artifactId>spring-scala_2.10</artifactId>
<version>1.0.0.RC1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>repository.springsource.milestone</id>
<name>SpringSource Milestone Repository</name>
<url>http://repo.springsource.org/milestone</url>
</repository>
</repositories>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.6</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>2.10.3</scalaVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
<强> config.xml中强>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.dimitrisli.scala"/>
<bean id="person" class="com.dimitrisli.scala.Person" scope="prototype">
<constructor-arg value="John"/>
</bean>
</beans>
<强> Person.scala 强>
package com.dimitrisli.scala
class Person(val firstName: String)
<强> Main.scala 强>
package com.dimitrisli.scala
import org.springframework.context.support.ClassPathXmlApplicationContext
object Main extends App {
val applicationContext = new ClassPathXmlApplicationContext("classpath:config.xml")
val me = applicationContext.getBean("person","overridenJohn").asInstanceOf[Person]
print(me.firstName)
}