From c6f7e13b5d5bbc224879f1e5d38cd80575840707 Mon Sep 17 00:00:00 2001 From: lukasgol Date: Sat, 5 Apr 2014 16:16:35 +0200 Subject: [PATCH] PropertiesResolver --- src/main/java/Config.java | 5 ++-- src/main/java/Controller_test.java | 23 +++++++++++++++++ src/main/java/utils/PropertiesSolver.java | 30 +++++++++++++++++++++++ src/main/resources/Spring-Module.xml | 8 ++++++ src/main/resources/app.properties | 2 ++ 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/main/java/utils/PropertiesSolver.java create mode 100644 src/main/resources/app.properties diff --git a/src/main/java/Config.java b/src/main/java/Config.java index 6dd044c..3945ef6 100644 --- a/src/main/java/Config.java +++ b/src/main/java/Config.java @@ -2,8 +2,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportResource; - - /** * Created by lukasgol on 01.04.14. */ @@ -11,7 +9,10 @@ import org.springframework.context.annotation.ImportResource; @EnableAutoConfiguration @ImportResource("Spring-Module.xml") public class Config { + public static void main(String[] args){ SpringApplication.run(Config.class, args); + } + } \ No newline at end of file diff --git a/src/main/java/Controller_test.java b/src/main/java/Controller_test.java index ee71c24..81acbc7 100644 --- a/src/main/java/Controller_test.java +++ b/src/main/java/Controller_test.java @@ -2,14 +2,25 @@ * Created by lukasgol on 01.04.14. */ +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import utils.PropertiesSolver; @Controller +@PropertySource("app.properties") public class Controller_test { + @SuppressWarnings("SpringJavaAutowiringInspection") + @Autowired + PropertiesSolver ps; + @Value("${my.prop}") + String val; + @RequestMapping("/action") public @ResponseBody @@ -24,5 +35,17 @@ public class Controller_test { String hello(@RequestParam(value = "action", required = false, defaultValue = "empty") String action) { return "Just String: "+action; } + + @RequestMapping("/val") + @ResponseBody + String getVal(@RequestParam(value = "param", required = false, defaultValue = "my.prop") String param){ + String recived_val; + if (param.equals("my.prop"))return "property "+param+" is: "+ val ; + else if((recived_val=ps.getProperty(param))!=null){ + return "property "+param+" is: "+ recived_val; + } + else return "not found"; + } + } diff --git a/src/main/java/utils/PropertiesSolver.java b/src/main/java/utils/PropertiesSolver.java new file mode 100644 index 0000000..74bc63d --- /dev/null +++ b/src/main/java/utils/PropertiesSolver.java @@ -0,0 +1,30 @@ +package utils; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +/** + * Created by lukasgol on 05.04.14. + */ +@Component +@PropertySource("app.properties") +public class PropertiesSolver { + + @Autowired + Environment env; + + @Value("${my.prop}") + String val; + + public PropertiesSolver() { + + } + + public String getProperty(String param){ + System.out.println(val); + return env.getProperty(param); + } +} diff --git a/src/main/resources/Spring-Module.xml b/src/main/resources/Spring-Module.xml index 2e89bab..04d692a 100644 --- a/src/main/resources/Spring-Module.xml +++ b/src/main/resources/Spring-Module.xml @@ -14,5 +14,13 @@ + + + + app.properties + + + \ No newline at end of file diff --git a/src/main/resources/app.properties b/src/main/resources/app.properties new file mode 100644 index 0000000..d36bfd0 --- /dev/null +++ b/src/main/resources/app.properties @@ -0,0 +1,2 @@ +my.prop=avalue +lala=lala \ No newline at end of file