- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 Spring 框架还很陌生,在理解 @Required
注释与 Java 配置的应用程序结合使用时遇到了问题。
这是一个示例。
配置文件
@Configuration
public class AppConfig {
@Bean
public Movie movieA() {
return new Movie();
}
@Bean
public MovieHolder holder() {
return new MovieHolder();
}
}
MovieHolder.java
public class MovieHolder {
private Movie movie;
public Movie getMovie() {
return movie;
}
@Required
public void setMovie(Movie movie) {
this.movie = movie;
}
}
上下文初始化
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MovieHolder holder = (MovieHolder) context.getBean("holder");
System.out.println("movie: " + holder.getMovie());
据我了解 @Required
注释的文档,应该会出现异常,因为电影不是直接设置的,也不是通过 Autowiring 设置的。相反,输出为 movie: null
。
我做错了什么?或者这不是 @Required
注释的正确用法?
最佳答案
在您正在实例化的 bean 中设置所需的属性是您自己的责任。 BeanPostProcessor
处理用 @Configuration
注释的类中的 bean 定义的函数称为 ConfigurationClassPostProcessor
。 BeanPostProcessor
处理您的 @Required
注释默认为 RequiredAnnotationBeanPostProcessor
,当您在配置中使用 context:annotation-config
和 context:component-scan
时,它会默认注册。如果您不使用这两个标签,您甚至可以注册自己的 RequiredAnnotationBeanPostProcessor
作为一个bean
。
现在,RequiredAnnotationBeanPostProcessor
的默认实现有一个名为 boolean shouldSkip(..)
的方法,用于检查名为 SKIP_REQUIRED_CHECK_ATTRIBUTE
的 bool 属性。 RequiredAnnotationBeanPostProcessor
在后处理期间检查每个 bean 的该属性值。 。如果返回 false
,则强制执行 @Required
约束,否则不执行。
现在,ConfigurationClassPostProcessor
在从 @Configuration
类创建 bean 定义时,将此属性的值设置为 true
(我猜是因为如果您正在定义一个 bean,您应该确保它具有所需的属性)。因此,对于此类 bean 不强制使用 @Required
。
顺便说一句,你可能会认为这是在哪里 SKIP_REQUIRED_CHECK_ATTRIBUTE
属性来自以及在哪里设置:它是在 BeanDefinition
的实例上设置的Spring 在内部使用它们来创建 bean 和进行后处理。
如果您确实想强制执行 @Required
约束,则必须覆盖 RequiredAnnotationBeanPostProcessor
,覆盖 boolean shouldSkip(..)
方法并注册此类,而不是默认的 RequiredAnnotationBeanPostProcessor
。并作为 RequiredAnnotationBeanPostProcessor
的文档说:
A default RequiredAnnotationBeanPostProcessor will be registered by the "context:annotation-config" and "context:component-scan" XML tags. Remove or turn off the default annotation configuration there if you intend to specify a custom RequiredAnnotationBeanPostProcessor bean definition.
另一种方法是在 @Bean
注释上使用 initMethod
属性。它可以执行检查以查看确实设置了所需的属性。但是,由于这是基于代码的配置,因此您也可以自己调用该 init
方法。
另外,在我看来,费尽周折去使用自己的RequiredAnnotationBeanPostProcessor
并没有多大意义。 ,如以下文档所述:
Please note that an 'init' method may still need to implemented (and may still be desirable), because all that this class does is enforce that a 'required' property has actually been configured with a value. It does not check anything else... In particular, it does not check that a configured value is not null.
总结一下: @Required
默认情况下不适用于 @Configuration
类。如果您需要确保所有属性都已设置,您也可以在 @Bean 方法中创建 bean 时自己完成(通过调用执行此类验证的某些 init
方法,或者只是自己提供所需的属性)。如果您确实需要使@Required
注释起作用,则需要使用您自己的 RequiredAnnotationBeanPostProcessor
实现,将其注册为 spring 上下文中的 bean,并放弃 context:annotation-config
的好处。
关于spring - @Required 注解如何与 JavaConfig 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41480406/
使用 ABC 加载模块 ( require )在分发的一个模块中工作,而在分发的另一个模块中失败。 加载 ABC 的原因可能是什么?与 require在一个地方失败? require Name::AB
我定义了以下方法: void Write(string fileContent, string fileName, string container = StorageBlobContainers.P
现在,Require.js是我最喜欢的Javascript编程方式。它可以使代码化整为零,并易于管理。而Require.js Optimizer能帮助我们将一个较大的应用分散成多个较小的应用,并通过
尝试开始使用 apioto http://apiato.io/A.getting-started/installation/ 如果我尝试测试 http://api.apiato.dev/registe
浏览 MDN 文档以查看提议的伪类的状态时,我遇到了 :required(并且扩展为 :optional)。这两个都已经存在很长时间了,但我现在才刚刚了解到。 此选择器与使用属性选择器 [requir
我正在尝试实现此条件:如果存在特定属性,则需要另一个属性;但如果它不存在,则不需要另一个。 另外,在 JSON 模式中,我们可以在依赖项中使用 not 吗? 这是一个示例架构 var schema =
我正在使用react-hot-loader我对其示例代码感到非常困惑: import React from 'react' import ReactDOM from 'react-dom' impor
过去几天我一直在玩 requirejs。我试图理解定义和要求之间的区别。 Define 似乎允许模块分离并允许遵守依赖关系顺序。但它会下载开始时所需的所有文件。而 require 仅在您需要时加载您需
我的项目是使用 angular cli [版本 - 6.1.3] 创建的。 我安装了 npm 模块 - is-reachable并在我的代码中使用它作为 - const isReachable = r
(有人可能会相应地更改标题)当像这样调用 javascript 的 require 方法时到底发生了什么: var xyz = require('xy')(require('z')); 谢谢 最佳答案
我一直在使用编译为 Node 代码的 Typescript 开发应用程序。因此,我更喜欢使用 import 语句来 require。 我一直在尝试将 Lodash 与 Lodash-Deep 一起使用
我在 require 中有一个奇怪的行为,我不知道如何避免(或者也许我的基础知识错误?)。 考虑以下代码: define (require) -> potoo = require "potoo"
这两种加杏仁和不加杏仁有什么区别? require('模块');require(['模块']); 编辑 嵌套: define(function() { require('module'); } def
我愿意使用 require.js 优化器优化我的 javascript 应用程序,但我现在想知道是否也可以在一个文件中包含 require.config 路径和 javascript 模块。事实上,在
我想我需要在一个页面中支持多个 require 实例,但在实现它时遇到了两个问题。我正在开发一项服务,该服务向外部客户端页面提供可嵌入的交互式对象。最重要的设计标准是易于嵌入,尽可能少地假设客户端环境
required 和 ng-required(表单验证)之间有什么区别? 最佳答案 AngularJS 表单元素查找 required属性来执行验证功能。 ng-required允许您设置requir
我有以下多选框: 0" /> 在我的 Controller 中,我在初始化时执行此操作: $scope.form.Slides = []; 如果幻灯片数组中有幻灯片,我希望表单的此元素有效。这些是动
我在 ubuntu 上运行 VPS: Distributor ID: Ubuntu Description: Ubuntu 14.04.5 LTS Release: 14.04 C
我正在使用 ArcGIS API for Javascript 3.21。我在 require() 中有一个函数。我希望在单击按钮时调用该函数,但该按钮位于 require() 之外。
我的浏览器应用程序使用 require.js。该应用程序在屏幕上显示许多不同的小部件之一。 URL 片段包含小部件的路径(require.js 路径),然后调用 require 来动态加载它: var
我是一名优秀的程序员,十分优秀!