Commit 05fe1752 by Dave Syer

Move field injection into @Configuration

parent cee619d4
......@@ -7,7 +7,6 @@ import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.filters.FilterRegistry;
......@@ -23,9 +22,12 @@ public class FilterInitializer implements ServletContextListener {
private static final Logger LOGGER = LoggerFactory.getLogger(FilterInitializer.class);
@Autowired
private Map<String, ZuulFilter> filters;
public FilterInitializer(Map<String, ZuulFilter> filters) {
this.filters = filters;
}
@Override
public void contextInitialized(ServletContextEvent sce) {
......
package org.springframework.cloud.netflix.zuul;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.PropertySourceUtils;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.*;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author Spencer Gibb
*/
@Slf4j
public class RouteLocator implements ApplicationListener<EnvironmentChangeEvent> {
public class RouteLocator implements ApplicationListener<EnvironmentChangeEvent>, EnvironmentAware {
public static final String DEFAULT_ROUTE = "/";
@Autowired
protected ConfigurableEnvironment env;
private ConfigurableEnvironment env = new StandardEnvironment();
@Autowired
protected DiscoveryClient discovery;
private DiscoveryClient discovery;
@Autowired
protected ZuulProperties properties;
private ZuulProperties properties;
private Field propertySourcesField;
private AtomicReference<LinkedHashMap<String, String>> routes = new AtomicReference<>();
public RouteLocator() {
@Override
public void setEnvironment(Environment environment) {
env = (ConfigurableEnvironment) environment;
}
public RouteLocator(DiscoveryClient discovery, ZuulProperties properties) {
this.discovery = discovery;
this.properties = properties;
initField();
}
......
package org.springframework.cloud.netflix.zuul;
import java.util.Map;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.http.ZuulServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter;
import org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter;
import org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter;
......@@ -19,7 +24,7 @@ import org.springframework.context.annotation.Configuration;
* @author Spencer Gibb
*/
@Configuration
@EnableConfigurationProperties()
@EnableConfigurationProperties(ZuulProperties.class)
@ConditionalOnClass(ZuulServlet.class)
@ConditionalOnExpression("${zuul.enabled:true}")
public class ZuulConfiguration {
......@@ -27,14 +32,18 @@ public class ZuulConfiguration {
@Autowired(required=false)
private TraceRepository traces;
@Bean
public ZuulProperties zuulProperties() {
return new ZuulProperties();
}
@Autowired
private DiscoveryClient discovery;
@Autowired
private ZuulProperties zuulProperties;
@Autowired
private Map<String, ZuulFilter> filters;
@Bean
public RouteLocator routes(){
return new RouteLocator();
return new RouteLocator(discovery, zuulProperties);
}
@Bean
......@@ -44,12 +53,12 @@ public class ZuulConfiguration {
@Bean
public ZuulHandlerMapping zuulHandlerMapping() {
return new ZuulHandlerMapping();
return new ZuulHandlerMapping(routes(), zuulController(), zuulProperties);
}
@Bean
public FilterInitializer zuulFilterInitializer() {
return new FilterInitializer();
return new FilterInitializer(filters);
}
// pre filters
......
package org.springframework.cloud.netflix.zuul;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.InstanceRegisteredEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
import javax.annotation.PostConstruct;
import java.util.Map;
/**
* @author Spencer Gibb
*/
@Slf4j
public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements ApplicationListener<InstanceRegisteredEvent> {
@Autowired
protected RouteLocator routeLocator;
private RouteLocator routeLocator;
@Autowired
protected ZuulController zuul;
private ZuulController zuul;
@Autowired
protected ZuulProperties properties;
private ZuulProperties properties;
public ZuulHandlerMapping() {
@Autowired
public ZuulHandlerMapping(RouteLocator routeLocator, ZuulController zuul, ZuulProperties properties) {
this.routeLocator = routeLocator;
this.zuul = zuul;
this.properties = properties;
setOrder(-200);
}
@PostConstruct
public void init() {
}
@Override
public void onApplicationEvent(InstanceRegisteredEvent event) {
registerHandlers(routeLocator.getRoutes());
......
......@@ -29,7 +29,6 @@ public class RouteLocatorTests {
@Mock
DiscoveryClient discovery;
@Before
public void init() {
initMocks(this);
......@@ -37,16 +36,17 @@ public class RouteLocatorTests {
@Test
public void testGetRoutes() {
RouteLocator routeLocator = new RouteLocator();
routeLocator.properties = new ZuulProperties();
routeLocator.properties.setIgnoredServices(Lists.newArrayList(IGNOREDSERVICE));
routeLocator.discovery = this.discovery;
routeLocator.env = this.env;
ZuulProperties properties = new ZuulProperties();
RouteLocator routeLocator = new RouteLocator(this.discovery, properties);
properties.setIgnoredServices(Lists.newArrayList(IGNOREDSERVICE));
routeLocator.setEnvironment(this.env);
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("zuul.route."+ ASERVICE, getMapping(ASERVICE)));
propertySources.addFirst(new MockPropertySource().withProperty("zuul.route."
+ ASERVICE, getMapping(ASERVICE)));
when(env.getPropertySources()).thenReturn(propertySources);
when(discovery.getServices()).thenReturn(Lists.newArrayList(MYSERVICE, IGNOREDSERVICE));
when(discovery.getServices()).thenReturn(
Lists.newArrayList(MYSERVICE, IGNOREDSERVICE));
Map<String, String> routesMap = routeLocator.getRoutes();
......@@ -56,16 +56,17 @@ public class RouteLocatorTests {
assertMapping(routesMap, ASERVICE);
String serviceId = routesMap.get(getMapping(IGNOREDSERVICE));
assertNull("routes did not ignore "+IGNOREDSERVICE, serviceId);
assertNull("routes did not ignore " + IGNOREDSERVICE, serviceId);
}
protected void assertMapping(Map<String, String> routesMap, String expectedServiceId) {
String mapping = getMapping(expectedServiceId);
String serviceId = routesMap.get(mapping);
assertEquals("routesMap had wrong value for "+mapping, expectedServiceId, serviceId);
assertEquals("routesMap had wrong value for " + mapping, expectedServiceId,
serviceId);
}
private String getMapping(String serviceId) {
return "/"+ serviceId +"/**";
return "/" + serviceId + "/**";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment