Closed altro3 closed 3 years ago
That's probably because SimpleService init
run before DataInitializer
@dstepanov Hi!
Yes, logically I understand what the problem is, but I don't understand such a strange behavior of the system. See, I am creating a service bean that links to a repository bean. Accordingly, when the init method (@PostConstruct) is called, I expect to see that my bean is completely ready for use, including all the beans it refers to (at least this is the behavior of the spring). And now, it turns out that I cannot use the @PostConstruct annotation at all, because there is no guarantee that all dependencies are already initialized and ready to go.
For clarity, I will write out a little more basic dependency structure:
Service:
@Singleton
@RequiredArgsConstructor
public class SimpleService {
private final SimpleRepo simpleRepo;
@PostConstruct
public void init() {
simpleRepo.findAll();
}
}
Repository:
@JdbcRepository(dialect = Dialect.POSTGRES)
public interface SimpleRepo extends CrudRepository<SimpleEntity, Integer> {
}
Entity:
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@MappedEntity
public class SimpleEntity {
@Id
@GeneratedValue(value = GeneratedValue.Type.SEQUENCE, ref = "simple_entity_id_seq")
private Integer id;
private String data;
private LocalDateTime updateTime;
}
I agree this should fixed. As a workaround you can probably use an event listener instead of @PostConstruct
:
@EventListener
public void init(StartupEvent event) {
simpleRepo.findAll();
}
@graemerocher thank you! Yes, I know that option. The difference is that if I use this construction with lazy beans, then my bean is always (!) Constructed at the start and makes a request to the database at the start, i.e. the logic of work changes (I do not want to make a request at the start always if I have selected lazy initialization). It turns out that I cannot use the same code for lazy and eager options.
As I said, I decided to experiment with the settings. now I am "probing" the micronaut in order to transfer the system to its use. I try various cases using one of the simple services as an example.
Thanks for reporting an issue, please review the task list below before submitting the issue. Your issue report will be closed if the issue is incomplete and the below tasks not completed.
NOTE: If you are unsure about something and the issue is more of a question a better place to ask questions is on Stack Overflow (https://stackoverflow.com/tags/micronaut) or Gitter (https://gitter.im/micronautfw/). DO NOT use the issue tracker to ask questions.
Task List
I decided to play with the micronaut's settings and try different modes in my small service. There was a problem with the selection
eagerInitSingletons
mode.In one of my service classes, there is data loading from the database after the bean is created:
My Application:
Ok, when I try to start application I get the exception:
I want to say that this error does not exist with the lazy mode of creating beans.
Environment Information
Example Application