春季:注射字段为空

发布于 2025-02-10 18:38:53 字数 1220 浏览 2 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2025-02-17 18:38:53

@Router中没有@component

@Autowired Spring Bean注入只能在Spring Bean组件中使用,并且只能与Spring Bean组件一起使用。

如果要注入Spring Bean,则必须在@component上使用loginview如下所示。

@Component
@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
    }
}

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 on LoginView like below.

@Component
@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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文