Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/spring config client and spring framework integration no springboot #1

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.classpath
.project
.settings
target/
pom.xml.tag
pom.xml.releaseBackup
Expand Down
22 changes: 21 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.7</jdk.version>
<spring.version>4.1.1.RELEASE</spring.version>
<spring.version>4.2.9.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<junit.version>4.11</junit.version>
<logback.version>1.0.13</logback.version>
Expand Down Expand Up @@ -37,6 +37,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
Expand Down Expand Up @@ -75,6 +81,14 @@
<scope>provided</scope>
</dependency>

<!--dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>1.4.5.RELEASE</version>
<optional>true</optional>
</dependency
-->

</dependencies>
<build>
<finalName>HelloWorld</finalName>
Expand Down Expand Up @@ -114,6 +128,12 @@
</configuration>
</plugin>

<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.3.v20170317</version>
</plugin>

</plugins>
</build>
</project>
65 changes: 38 additions & 27 deletions src/main/java/com/zenika/controller/BaseController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.zenika.controller;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -9,31 +11,40 @@

@Controller
public class BaseController {

private static int counter = 0;
private static final String VIEW_INDEX = "index";
private final static org.slf4j.Logger logger = LoggerFactory.getLogger(BaseController.class);

@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome(ModelMap model) {

model.addAttribute("message", "Welcome");
model.addAttribute("counter", ++counter);
logger.debug("[welcome] counter : {}", counter);

// Spring uses InternalResourceViewResolver and return back index.jsp
return VIEW_INDEX;

}

@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String welcomeName(@PathVariable String name, ModelMap model) {

model.addAttribute("message", "Welcome " + name);
model.addAttribute("counter", ++counter);
logger.debug("[welcomeName] counter : {}", counter);
return VIEW_INDEX;

}


private static int counter = 0;
private static final String VIEW_INDEX = "index";
private final static org.slf4j.Logger logger = LoggerFactory.getLogger(BaseController.class);

@Autowired
private GithubProperties properties;

@Autowired
private MyPropeties myProperties;

@Autowired
Environment env;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome(ModelMap model) {

model.addAttribute("message", "Welcome github prop=" + properties.getCloneBaseDir() + " envProp=" + env.getProperty("github.cloneBaseDir") + " AND MORE MORE MORE beanProp=" + myProperties.getToken());
model.addAttribute("counter", ++counter);
logger.debug("[welcome] counter : {}", counter);

// Spring uses InternalResourceViewResolver and return back index.jsp
return VIEW_INDEX;

}

@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String welcomeName(@PathVariable String name, ModelMap model) {

model.addAttribute("message", "Welcome " + name);
model.addAttribute("counter", ++counter);
logger.debug("[welcomeName] counter : {}", counter);
return VIEW_INDEX;

}

}
39 changes: 39 additions & 0 deletions src/main/java/com/zenika/controller/GithubProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.zenika.controller;

import javax.annotation.PostConstruct;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "github")
public class GithubProperties {

/**
* Oauth token used for authenticating with GitHub
*/
private String token;
/**
* Root dir path to clone the repos
*/
private String cloneBaseDir;

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public String getCloneBaseDir() {
return cloneBaseDir;
}

public void setCloneBaseDir(String cloneDir) {
this.cloneBaseDir = cloneDir;
}

@PostConstruct
public void loadedProperties() {
System.out.println("Github Properties are " + token + " with " + cloneBaseDir);
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/zenika/controller/MyPropeties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.zenika.controller;

import javax.annotation.PostConstruct;

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

@Component
public class MyPropeties {

/**
* Oauth token used for authenticating with GitHub
*/
@Value("${github.token}")
private String token;
/**
* Root dir path to clone the repos
*/
@Value("${github.cloneBaseDir}")
private String cloneBaseDir;

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public String getCloneBaseDir() {
return cloneBaseDir;
}

public void setCloneBaseDir(String cloneDir) {
this.cloneBaseDir = cloneDir;
}

@PostConstruct
public void loadedProperties() {
System.out.println("My Properties are: " + token + " with " + cloneBaseDir);
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/zenika/controller/config/CloudEnvironment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.zenika.controller.config;

import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.web.context.support.StandardServletEnvironment;

public class CloudEnvironment extends StandardServletEnvironment {

@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
super.customizePropertySources(propertySources);
try {
PropertySource<?> source = initConfigServicePropertySourceLocator(this);
propertySources.addLast(source);

} catch (

Exception ex) {
logger.warn("failed to initialize cloud config environment", ex);
}
}

private PropertySource<?> initConfigServicePropertySourceLocator(Environment environment) {

ConfigClientProperties configClientProperties = new ConfigClientProperties(environment);
configClientProperties.setUri("http://localhost:8888");
configClientProperties.setName("publisher");
configClientProperties.setLabel("master");

System.out.println("##################### will load the client configuration");
System.out.println(configClientProperties);

ConfigServicePropertySourceLocator configServicePropertySourceLocator =
new ConfigServicePropertySourceLocator(configClientProperties);

return configServicePropertySourceLocator.locate(environment);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.zenika.controller.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import com.zenika.controller.GithubProperties;

@Configuration
@EnableConfigurationProperties({GithubProperties.class})
public class PropertiesConfigurer {

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.zenika.controller.config;

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.context.support.XmlWebApplicationContext;

public class SpringCloudConfigContext extends XmlWebApplicationContext {
@Override
protected ConfigurableEnvironment createEnvironment() {
System.out.println("##################### loaded my comfigurable context");
return new CloudEnvironment();
}
}
5 changes: 5 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
additivity="false">
<appender-ref ref="STDOUT" />
</logger>

<logger name="org.springframework.cloud.config.client" level="debug"
additivity="false">
<appender-ref ref="STDOUT" />
</logger>

<root level="error">
<appender-ref ref="STDOUT" />
Expand Down
18 changes: 12 additions & 6 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextClass</param-name>
<param-value>com.zenika.controller.config.SpringCloudConfigContext</param-value>
</context-param>



<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

</web-app>