Home > Blockchain >  Instantiate a POJO setting class in Spring, and make it accessible from all code
Instantiate a POJO setting class in Spring, and make it accessible from all code

Time:05-31

I'm new in Spring and I'm having problems when trying to create a class with all parameters filled by an external form. But I want to make this intance as unique, and accessible from everyclass from the rest of the project, because so far the object is being reinstantiated again, so I am not saving my settings. I don't know if there are annotations for this, and also how I am able to inyectate this instance in any other class.

@Data
@Builder
public class ProjectConfiguration {

    //InputFile
    File midiFile = null;
    private String resourcesPath;
    private Sequence sequence;

    //OutputFile
    private String exportPath;
    private String exportFile;

    //Video
    private int width;
    private int height;

    //Audio
    private int sampleRate; // Hz

    //Preview
    private IMediaViewer.Mode viewMode;
    private boolean showStats;
}

Can you please provide me some guidelines?

I'm updating the code as I followed the guidelines provided below and now I have more questions ans still is not working. If I use @Autowired ProjectConfiguration configuration; in another class to call the same configuration it returns this error

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.kadathsound.midiread.MainView': Unsatisfied dependency expressed through field 'configuration'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.kadathsound.midiread.model.config.ProjectConfiguration' available: expected single matching bean but found 2: projectConfiguration,getInstance

And if I remove the @Autowired then I getting a NullPointer Exception, as it is still creating a new instance. This the new code. Please, help me. I'm completely lost with this. All the documentation I read it doesn't help me as it is not the same case.

@Data
@Configuration
@NoArgsConstructor
public class ProjectConfiguration implements Serializable {

    private File midiFile;
    private String resourcesPath;
    private Sequence sequence;
    private String exportPath;
    private String exportFile;
    private int width;
    private int height;
    private int sampleRate;
    private IMediaViewer.Mode viewMode;
    private boolean showStats;

    private static volatile ProjectConfiguration instance = null;

    @Bean
    public static ProjectConfiguration getInstance() {
        if (instance == null) {
            synchronized(ProjectConfiguration.class) {
                if (instance == null) {
                    instance = new ProjectConfiguration();
                }
            }
        }
        return instance;
    }
}

CodePudding user response:

You can create a bean for this class and make it available for the entire application. You can create a config file annotated with @Configuration. Declare a method that returns the class that you want annotated with @Bean. Inside the method build the class with the values that you gather somehow return the builded class.

You can access the bean with the help of @Autowired.

I think this helps !!!.

References : https://www.javaguides.net/2018/09/spring-bean-annotation-with-example.html

  • Related