简体   繁体   中英

Cache Loading in Spring Boot

I have a cache class that is loading properties from a.properties file before the application startup, in a simple java project . It logs out everything.I need to convert this project to springboot application.

What annotations can i use to achieve the Cache Loading??

Currently I wrote the code like my spring boot app starts with the @postconstruct, since i am not using web.xml for loading servlets.

@RestController
public class ConfigServlet {

    @PostConstruct
        public void init() {
    //business logic
    }
}

And this servlet starts up first. So how can i load cache along this??

It is supposed to load the Cache even before this servlet class loads. How can i achieve this concept??

Suppose you have below properties in your application properties. You can load them as below. The properties will be loaded during the application startup.

application.properties
    test.a = 10
    test.b =20
    test1.a = 30
    test1.b = 40

@Configuration
@ConfigurationProperties
Class CacheProperties {

Map<String,String> test;
Map<String,String> test1;

public String getTestB() {

return test.get("b");

}

public String getTestA() {

return test.get("a");

}

public String getTest1B() {

return test1.get("b");

}

public String getTest1A() {

return test1.get("a");

}

//setters


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM