【发布时间】:2019-08-03 12:38:53
【问题描述】:
我有一个为我的应用程序创建 bean 的配置类。我看到虽然我在创建适配器 bean 时设置了 bean 属性,但不知何故,这些属性在我的控制器类中被清除并设置为 null。经过2个小时的调试,我画了一个空白。请大家指点。
@RequiredArgsConstructor//lombok annotation to generate the constructor.
public class MyAdapter {//Trying to create a bean of this type
@NonNull private final MyPropertyObj prop;
@NonNull private final Integer timeout;
}
@Configuration
@Profile("!test")
class MyConfigClass{
@Bean
public MyAdapter adapter(){
MyPropertyObj prop= new MyPropertyObj();
return new MyAdapter(prop, 10);//Here I am setting prop and 10, but when I check auto wired adapter they are null.
}
}
public class MyController {
@Autowired private MyAdapter adapter;
//adapter gets injected, but adapter.prop and the adapter.timeout are null.
}
【问题讨论】:
-
你的类
myAdapter的构造函数在哪里? -
@Augustas 我看了那个问题。这个问题是关于手动创建一个新对象并期望它内部的属性是自动连接的。我想要做的是不同的。我在实例化过程中创建的 bean 以某种方式改变了状态并清除了它的属性。
-
@Jens RequiredArgsConstructor 生成构造函数
-
RequiredArgsConstructor 来自 Lombok lib 你可能应该添加 lombok 标签并在那里尝试projectlombok.org/features/Constructor.html
标签: java spring-boot autowired