小编QD_*_*LDN的帖子

如何在Spring boot中创建不可变和单例类?

我有一个人物模型类,它需要是不可变的和单例的。我需要从我的服务类自动连接,但出现错误。

那么有没有办法在Spring Boot中创建单例不可变类呢?我可以亲自设置值(通过自动装配)并在整个应用程序中使用相同的值。

我刚刚开始学习spring-boot。我在谷歌上搜索并找到了 Lombok 的解决方案,但不知何故它对我不起作用。

我的人物模型:

    @Value
    @Builder
    @AllArgsConstructor(access = AccessLevel.PRIVATE) 
    @Component
    public final class Person {
        private final String firstName;
        private final String lastName;
    }


//My service class
@Component
public class ArgumentReadingService {
    
    @Autowired
    Person person;
    
    public void readArguments() {
        
        person = Person.builder().firstName("Hello").build();
        
        System.out.println("name : "+person.getFirstName());
        
    }
Run Code Online (Sandbox Code Playgroud)

错误 :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-10 17:52:29.197 ERROR 8824 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating …
Run Code Online (Sandbox Code Playgroud)

java spring autowired lombok spring-boot

3
推荐指数
1
解决办法
5263
查看次数

如何仅使用 @DateTimeFormat 将日期 @ConfigurationProperties 与时间绑定?

spring-boot 的新手。

我正在尝试使用注释@ConfigurationProperties 解析文件中的属性。我能够解析日期字段以外的字段。

问题是我的属性文件只有时间而没有日期。即日期=09:30:00。

我可以用 @DateTimeFormat(pattern = "HH:mm:ss") 解析它。但问题是,它给出的日期为 date=Thu Jan 01 09:30:00 GST 1970。

我想获取今天的日期,时间为 09:30:00。是否可以 ?

@ConfigurationProperties
public class Config {

    private int id;
    
    private int version;
        
    @DateTimeFormat(pattern = "HH:mm:ss")
    private Date date;
    
}
Run Code Online (Sandbox Code Playgroud)

财产

id=12
version=2
date=09:30:00
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot dateformatter

0
推荐指数
1
解决办法
3494
查看次数

标签 统计

java ×2

spring ×2

spring-boot ×2

autowired ×1

dateformatter ×1

lombok ×1