春天到了/斯卡拉:可能自动装配豆的依赖? [英] spring/scala: Possible to autowire bean dependencies?
问题描述
我试着做下面没有xml配置在春季(Scala中)
<豆...>
##(1)
##汽车通过创建一个类名豆
## x.y.Bar的自动线性质
<豆的id =巴兹级=x.y.Baz/> ##(2)
##创建x.y.Foo和自动线的财产
<豆的id =富类=x.y.Foo>
<属性名=BREF =巴兹/>
< /豆>
< /豆>
在那里我们有:
类巴兹{} 类Foo {
@Autowired //?
@BeanProperty
VAL巴兹:巴兹= NULL
}
我有以下测试设置:
@Configuration
类配置{
// @自动装配//似乎并没有帮助
@Bean //(自动装配=阵列(classOf [Autowire.BY_TYPE]))
高清foo的:美孚= NULL
}类BeanWithAutowiredPropertiesTest { @测试
抛出:[异常]高清beanWithAutowiredPropertiesTest():单位= {
VAR CTX =新AnnotationConfigApplicationContext(classOf [配置]);
VAL富= ctx.getBean(classOf [美孚])
assertTrue(富!= NULL)
assertTrue(ctx.getBean(classOf [美孚])。巴兹!= NULL)
}
}
据我了解几个简单的替代品:
-
@ComponentScan - 这种方法有几个问题:
- IM precision - 可以有很多类在包匹配的自动连接类型
- 它不(本身)许可证的属性选择特定值
- 扫描是针对大型项目十分缓慢
-
无法添加到@Component第三方班
(如果我能报名考生自动线类型,通过名字,这将有很大的帮助!)
- 实施@Bean声明作为一种方法:
@Bean
高清foo的:美孚= {
VAL F =新的Foo()
f.baz =?哎呀!来自哪里?在这种配置不可用
˚F
}
不过:</ P>
-
这八九不离十规避自动布线点。如果我明确选择了一个参数,巴兹设置,然后我需要真正得到它的引用来做到这一点摆在首位。经常这是很困难的,特别是如果要使用的实际巴兹可能在另一@Configuration指定。
-
因为我创建对象,没有我需要自动装配所有的依赖?如果巴兹有100个属性和我唯一的办法明确指定1,并有其余的自动连接?
据我所知,基于XML的配置没有任何这些问题 - 但我不知所措,因为我的春天手册说,你可以通过注释做同样的事情。
NB。我还看到:
@Bean(自动装配=阵列(classOf [Autowire.BY_TYPE]))
是可能的。我不能在网上找到的例子,和Scala抱怨(标注参数不是一个常数)。
类的ApplicationController @Inject()(messagesApi:MessagesApi){
... code这里...
}
messagesApi是注入成员
看到更多的<一个href=\"https://github.com/mohiva/play-silhouette-seed/blob/master/app/controllers/ApplicationController.scala\" rel=\"nofollow\">https://github.com/mohiva/play-silhouette-seed/blob/master/app/controllers/ApplicationController.scala
[编辑之前】
由Joshua.Suereth回答。
这是短版(丑,但工程):
VAR服务:服务= _;
@Autowired高清setService(服务:服务)= this.service =服务
I'm trying do the following without xml configuration in spring (in scala)
<beans ... >
## (1)
## Auto create a bean by classname
## Auto-wires properties of x.y.Bar
<bean id="baz" class="x.y.Baz"/>
## (2)
## Create a x.y.Foo and auto-wire the property
<bean id="foo" class="x.y.Foo">
<property name="b" ref="baz"/>
</bean>
</beans>
where we have:
class Baz {}
class Foo {
@Autowired //?
@BeanProperty
val baz:Baz = null
}
I have the following test setup:
@Configuration
class Config {
//@Autowired // Seem not to help
@Bean //( autowire=Array( classOf[Autowire.BY_TYPE ]) )
def foo: Foo = null
}
class BeanWithAutowiredPropertiesTest {
@Test
@throws[Exception] def beanWithAutowiredPropertiesTest(): Unit = {
var ctx = new AnnotationConfigApplicationContext(classOf[Config]);
val foo = ctx.getBean(classOf[Foo])
assertTrue(foo != null)
assertTrue(ctx.getBean(classOf[Foo]).baz != null)
}
}
I understand a couple of simple alternatives:
@ComponentScan -- this approach has several issues:
- imprecision - there can be many classes matching an auto-wired type in a package
- it doesn't (in itself) permit selecting specific values for properties
- scanning is painfully slow for large projects
Can't add @Component to 3rd party classes
(If I could register candidate auto-wire types, by name, that would help a lot!)
- implementing the @Bean declaration as a method:
.
@Bean
def foo:Foo = {
val f = new Foo()
f.baz = ?? grrr! where from? Not available in this Config
f
}
However:
this sorta circumvents the point of auto-wiring. If I explicitly chose a parameter, baz to set, then I need to actually get a reference to it to do that in the first place. Frequently this can be difficult, especially if the actual baz to be used might be specified in another @Configuration.
because I'm creating the object, don't I need to auto-wiring all of its dependencies? What if Baz has 100 properties and I only way to specify 1 explicitly and have the rest auto-wired?
AFAIK, the xml based configuration doesn't have any of these problems - but I'm at a loss because the spring manual says you can do all the same things via annotations.
NB. I also see:
@Bean( autowire=Array( classOf[Autowire.BY_TYPE ]) )
might be possible. I can't find example online, and scala complains (annotation parameter is not a constant).
[edited]
class ApplicationController @Inject() (messagesApi: MessagesApi){
... code here ...
}
messagesApi is an injected member
see more in https://github.com/mohiva/play-silhouette-seed/blob/master/app/controllers/ApplicationController.scala
[before edit]
Answered by Joshua.Suereth. This is the short version ("ugly but works"):
var service: Service = _;
@Autowired def setService(service: Service) = this.service = service
这篇关于春天到了/斯卡拉:可能自动装配豆的依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!