1

This question is similar to this one : Spring: How to inject a value to static field?

but with a twist..

I need to use the injected values in the static methods @BeforeClass & @AfterClass.

I tried creating @Component and feeding the values from application.properties to the static fields on @PostConstruct

Alas... i get a null pointer exception.

So the question can be rephrased as

"How can i use values from application properties inside my @AfterClass / @BeforeClass methods ?"


The @Component :

package com.vulog.pdfgenerator.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

    @Component
    public class GlobalValue {

        @Value("${spring.thymeleaf.prefix}")
        protected String prefix;

        @Value("${template.tmpfolder}")
        private String tmpfolder;
        public static String tmpFolderStatic;

        @Value("${template.classpath}")
        private String classpath;
        public static String classpathStatic;

        /**
         * Hack! we want to use the static methods BeforeClass/AfterClass
         * so the app property values should be kept static as well.
         */

        @PostConstruct
        public void init(){
            classpathStatic = classpath;
            tmpFolderStatic = tmpfolder;
        }
    }

I autowire this to a test class and try to get the GlobalValue.classpathStatic but it throws.

@Autowired
    GlobalValue globalValue;
Orkun
  • 6,998
  • 8
  • 56
  • 103
  • By loading them yourself. Those before and after class methods run before anything has had a change to initialize the application or load a spring application context. Hence it won't work. However why do you need those values in a static method anyway... – M. Deinum Sep 05 '18 at 17:33
  • because i need those values inside the methods @ AfterClass & @ BeforeClass which HAVE TO be static – Orkun Sep 05 '18 at 17:39
  • 1
    That isn't answer my question WHY do you need them in those static methods. What is you do in those methods. As stated if you really need that load them yourself as nothing spring related has been started there. But explain WHY you need them what you try to achieve there and probably there is a beter way to achieve that. – M. Deinum Sep 05 '18 at 17:40
  • ah ok. i m copying some files from a path to another. (These are html files, used as templates for pdf generation with thymeleaf ) The unit tests call the generator, that calls the file resolvers that will look into those paths. – Orkun Sep 06 '18 at 08:16
  • Why not simply do that in a regular `@Before`? Also why are the paths different in your test as compared to your actual code? – M. Deinum Sep 06 '18 at 08:27
  • i think i ll do that then.. u re right, these methods are out of the spring context for a reason. – Orkun Sep 06 '18 at 08:30
  • 1
    Trying to shoehorn that in static just to save the overhead of copying it per test seems like overkill. Just use the regular mechanisms (you could check if the files are there or not and copy then instead of always copying). But I expect you gain very little by not copying each time. – M. Deinum Sep 06 '18 at 08:31

0 Answers0