gpt4 book ai didi

java - 如何在 Spring Boot 中创建不可变类和单例类?

转载 作者:行者123 更新时间:2023-12-04 13:09:54 25 4
gpt4 key购买 nike

我有一个 person 模型类,它需要是不可变的和单例的。我需要从我的服务类 Autowiring 但出现错误。

那么有什么办法可以在spring boot中创建一个单例不可变类呢?我可以亲自设置值(通过 Autowiring )并在整个应用程序中使用相同的值。

刚开始学习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());

}

错误:

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 bean with name 'person' defined in file [C:\Users\User\Desktop\WorkSpace\Orion.Strat1.bidAsk_Stp\target\classes\orion\model\Person.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [orion.model.Person]: No default constructor found; nested exception is java.lang.NoSuchMethodException: orion.model.Person.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1316) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) [spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) [spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) [spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) [spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) [spring-boot-2.4.3.jar:2.4.3]
at com.orion.Application.main(Application.java:20) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [orion.model.Person]: No default constructor found; nested exception is java.lang.NoSuchMethodException: orion.model.Person.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-5.3.4.jar:5.3.4]
... 17 common frames omitted
Caused by: java.lang.NoSuchMethodException: orion.model.Person.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_191]
at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_191]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.3.4.jar:5.3.4]
... 18 common frames omitted

最佳答案

这个配置类和那个注释将始终为您提供那个 Person Bean

@Configuration
public class ServerConfig
{

@Bean
private Person getPerson(){
return new Person("name", "lastName");
}

现在它将随处可用以进行 Autowiring 。

然而,正如评论所指出的,将模型类用作单例似乎很奇怪。

关于java - 如何在 Spring Boot 中创建不可变类和单例类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66566251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com