gor*_*tde 1 java spring spring-mvc
服务
@Service
class Foo{
Foo(@Value("${my.property}")int delay){
...
}
}
Run Code Online (Sandbox Code Playgroud)
消费者
class Bar {
@Autowire
Foo foo;
}
Run Code Online (Sandbox Code Playgroud)
bean.xml
<context:component-scan base-package="foo.*" />
<context:spring-configured />
<context:property-placeholder location="classpath:foo/internal.properties"/>
Run Code Online (Sandbox Code Playgroud)
internal.properties确实包含my.property=5000. 但似乎 spring 甚至不关心@Value注释。如果我运行该应用程序,spring 会抱怨没有找到默认构造函数。
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [foo.Foo]: No default constructor found; nested exception is java.lang.NoSuchMethodException...
Run Code Online (Sandbox Code Playgroud)
我什至尝试使用constructor-arg标记在 beans.xml 中配置参数。此方法会产生相同的错误。
为什么价值注入不起作用?
@Service
class Foo{
@Autowired
Foo(@Value("${my.property}")int delay){
...
}
}
Run Code Online (Sandbox Code Playgroud)
您忘记添加@Autowired构造函数。
从Spring 4.3 开始,@Autowired如果目标 bean 只定义了注解,我们不再需要指定注解 one constructor
| 归档时间: |
|
| 查看次数: |
3168 次 |
| 最近记录: |