我有一个实体类:
Class.java
@Entity
public class Class {
@Id
@GeneratedValue
private Long id;
@NotNull
@Range(min = 0, max = 10)
private double value;
}
Run Code Online (Sandbox Code Playgroud)
我想摆脱@Range注释中的硬编码值并从配置文件中加载它们.
constraints.properties
minVal=0
maxVal=10
Run Code Online (Sandbox Code Playgroud)
这就是我尝试过的:
@Component
@Entity
@PropertySource("classpath:/constraints.properties")
public class Class {
@Value("${minVal}")
private final long minValue;
@Value("${maxVal}")
private final long maxValue;
@Id
@GeneratedValue
private Long id;
@NotNull
@Range(min = minValue, max = maxValue)
private double value;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是attribute value must be constant.如何执行这些字段的初始化以获得我想要的结果?
我正在尝试将一个对象注入一个类。但是,该字段始终为空。我试过@Autowired和@Resource注释。我没有在new任何地方使用运算符创建对象。的构造函数Foo被正确调用。
此问题的最小示例:
Foo类
package foo.bar;
public class Foo {
Foo(){
System.out.println("Foo constructor");
}
public void func() {
System.out.println("func()");
}
}
Run Code Online (Sandbox Code Playgroud)
酒吧班
package foo.bar;
public class Bar {
@Autowired
private Foo foo;
public Bar() {
foo.func();
}
}
Run Code Online (Sandbox Code Playgroud)
入口点
package foo.bar;
public class HelloApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
}
}
Run Code Online (Sandbox Code Playgroud)
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="foo.bar"/>
<bean …Run Code Online (Sandbox Code Playgroud) 我想使用返回内容类型的 RESTful 服务text/javascript。
由于 Spring Boot 中没有HttpMessageConverter可以执行此操作的 OOTB,因此我想注册一个自定义转换器。
我发现做到这一点的一种方法是RestTemplate通过修改来自定义自身MappingJackson2HttpMessageConverter:
@Component
public class CustomRestTemplateProvider {
public RestTemplate getCustomRestTemplate(MediaType mediaType) {
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
jacksonConverter.setSupportedMediaTypes(Collections.singletonList(mediaType));
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(jacksonConverter);
return new RestTemplate(converters);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的服务中,只需致电getCustomRestTemplate(new MediaType("text", "javascript")
上面的解决方案工作正常,但我还尝试根据Spring Boot 文档 (27.1.2)创建一个新的转换器来处理这种媒体类型:
所以我创建了一个新的转换器:
@Component
public class TextJavascriptMessageConverter extends AbstractJackson2HttpMessageConverter {
public TextJavascriptMessageConverter(ObjectMapper objectMapper) {
super(objectMapper);
setTextJavascriptAsSupportedMediaType();
}
private void setTextJavascriptAsSupportedMediaType() {
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(new MediaType("text", …Run Code Online (Sandbox Code Playgroud)