java - 将 Handler 注入(inject)注解 Controller

标签 java spring spring-mvc

如何在不使用 @Autowire 的情况下将处理程序注入(inject)注解 Controller (@Controller)?

最佳答案

当然,您始终可以在 XML 中配置 Autowiring :

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    default-autowire="byType">

    <!-- all beans here will be autowired, including those
         found by <contect:component-scan /> -->

</beans>

但是您可能必须为属性提供 Setter 方法。

引用: Autowiring Collaborators


顺便说一句,如果你不想的话,你也可以使用 JSR-250 @Resource 注释(当然还有 JSR-330 的 @Inject,感谢@Bozho)使用 Spring 专有的 @Autowired 注释。

引用: @Resource , @Autowired and @Inject

关于java - 将 Handler 注入(inject)注解 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4721031/

相关文章:

java.util.NoSuchElementException错误,查找了可能的原因,仍然无法修复

java - TemplateEngine 未在模板文件夹中查找文件

java - 使用 Spring Boot 和 Thymeleaf 时出现 "Circular view path would dispatch back to the current handler URL."

spring - 我可以在 Micronaut 中使用 spring data jpa 吗?

java - 带有Tomcat的Spring MVC不会打开Hello World html页面

java - 返回字符串时,它会向字符串添加额外的斜杠

java - 将 JPopupMenu 添加到 TrayIcon

java - SWT 标签大小未正确更新

java - Spring 5 至少必须存在一个 JPA 元模型

java - 在 Spring REST 中自定义 Jackson XML 响应