春季:注射字段为空
我有一个Spring(非启动)应用程序,我正在尝试将字段自动到类中:
@Route("login")
public class LoginView extends VerticalLayout {
private LoginOverlay login;
@Autowired
private UiSecurityService uiSecurityService;
public LoginView() {
createContents();
}
private void createContents() {
// create Layout
}
private void onLoginPressed(LoginEvent e) {
// handle login
}
}
但是,uisecurityService
始终是null
。
uisecurityService
应该正确注释:
@Component
public class UiSecurityService {
@Autowired
private AuthenticationManager authenticationManager;
public LoginResult login(String username, String password) {
// handle login
}
}
另外,@componentscan
注释(据我所知)也正确设置:
@Configuration
@Import({ SomeConfiguration.class, SecurityConfiguration.class })
@ImportResource("classpath:/path/to/config/*-context.xml")
@ComponentScan("parent.package.where.beans.are.located")
public class WebConfiguration extends WebMvcConfigurationSupport {
}
我缺少什么?我已经尝试通过设置器,相同的结果注入uisecurityService
。我不能使用构造函数注入,因为那时瓦丁会引发异常。
I have a spring (non-boot) application in which I'm trying to autowire a field into a class as such:
@Route("login")
public class LoginView extends VerticalLayout {
private LoginOverlay login;
@Autowired
private UiSecurityService uiSecurityService;
public LoginView() {
createContents();
}
private void createContents() {
// create Layout
}
private void onLoginPressed(LoginEvent e) {
// handle login
}
}
However, UiSecurityService
is always null
.
UiSecurityService
should be annoted correctly:
@Component
public class UiSecurityService {
@Autowired
private AuthenticationManager authenticationManager;
public LoginResult login(String username, String password) {
// handle login
}
}
Also, the @ComponentScan
annotation is (as far as I know) set correctly as well:
@Configuration
@Import({ SomeConfiguration.class, SecurityConfiguration.class })
@ImportResource("classpath:/path/to/config/*-context.xml")
@ComponentScan("parent.package.where.beans.are.located")
public class WebConfiguration extends WebMvcConfigurationSupport {
}
What am I missing? I have tried injecting UiSecurityService
via a setter, same result. I can't use constructor injection because then vaadin throws an exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Router
中没有@component
。@Autowired
Spring Bean注入只能在Spring Bean组件中使用,并且只能与Spring Bean组件一起使用。如果要注入Spring Bean,则必须在
@component
上使用loginview
如下所示。There is no
@Component
in@Router
.@Autowired
spring bean injection can use only in spring bean component and can use only with spring bean component.If you want to inject spring bean, you must use
@Component
onLoginView
like below.