java—如何在SpringBoot中创建不可变和单例类?

zzzyeukh  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(596)

我有一个person模型类,它需要是不可变的和单例的。我需要从我的服务类自动连线,但得到一个错误。
那么,有没有办法在springboot中创建一个单例不可变类呢?我可以亲自设置值(通过autowire)并在整个应用程序中使用相同的值。
我刚开始学春靴。我在google上搜索,找到了lombok的解决方案,但不知怎么的,它对我不起作用。
我的人模:

  1. @Value
  2. @Builder
  3. @AllArgsConstructor(access = AccessLevel.PRIVATE)
  4. @Component
  5. public final class Person {
  6. private final String firstName;
  7. private final String lastName;
  8. }
  9. //My service class
  10. @Component
  11. public class ArgumentReadingService {
  12. @Autowired
  13. Person person;
  14. public void readArguments() {
  15. person = Person.builder().firstName("Hello").build();
  16. System.out.println("name : "+person.getFirstName());
  17. }

错误:

  1. Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  2. 2021-03-10 17:52:29.197 ERROR 8824 --- [ main] o.s.boot.SpringApplication : Application run failed
  3. 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>()
  4. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1316) ~[spring-beans-5.3.4.jar:5.3.4]
  5. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-5.3.4.jar:5.3.4]
  6. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.4.jar:5.3.4]
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar:5.3.4]
  8. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar:5.3.4]
  9. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar:5.3.4]
  10. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar:5.3.4]
  11. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
  12. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.4.jar:5.3.4]
  13. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917) ~[spring-context-5.3.4.jar:5.3.4]
  14. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582) ~[spring-context-5.3.4.jar:5.3.4]
  15. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) [spring-boot-2.4.3.jar:2.4.3]
  16. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.4.3.jar:2.4.3]
  17. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) [spring-boot-2.4.3.jar:2.4.3]
  18. at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) [spring-boot-2.4.3.jar:2.4.3]
  19. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) [spring-boot-2.4.3.jar:2.4.3]
  20. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) [spring-boot-2.4.3.jar:2.4.3]
  21. at com.orion.Application.main(Application.java:20) [classes/:na]
  22. 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>()
  23. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.3.4.jar:5.3.4]
  24. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-5.3.4.jar:5.3.4]
  25. ... 17 common frames omitted
  26. Caused by: java.lang.NoSuchMethodException: orion.model.Person.<init>()
  27. at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_191]
  28. at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_191]
  29. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.3.4.jar:5.3.4]
  30. ... 18 common frames omitted
ncgqoxb0

ncgqoxb01#

这个配置类和那个注解将始终为您提供personbean

  1. @Configuration
  2. public class ServerConfig extends
  3. {
  4. @Bean
  5. private Person getPerson(){
  6. return new Person("name", "lastName");
  7. }

现在,它将可用于任何地方的自动布线。
然而,正如评论所指出的,使用模型类作为单例似乎很奇怪。

相关问题