Spring 5 Java Config Set Default Profile - java

In my older Spring 4 web-app, I used an applicationContext.xml file, and my default spring profile was as follows;
<beans profile="default">
<context:property-placeholder location="file:/opt/myapp/myapp-ws.properties" />
</beans>
And now I am using Spring 5 Framework, but NOT Spring Boot 2.x, and I want to do this in my Java Config class.
My main configuration class looks like this;
#Configuration
#ComponentScan(basePackages = "com.tomholmes.myapp")
#EnableWebMvc
public class MyAppConfig
{
}
And I have the AppInitializer as follows;
public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
private static final Log logger = LogFactory.getLog(ApplicationInitializer.class);
#Override
protected Class<?>[] getRootConfigClasses()
{
return new Class[]
{ MyAppConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses()
{
return new Class[]{};
}
#Override
protected String[] getServletMappings()
{
return new String[]
{ "/api/*" };
}
}
I've been doing some research on the Net since there is a lot of information on this, but a lot of it conflates Spring Boot,and I just want a Spring 5 without Spring Boot solution. I'll keep looking, I am sure this is a simple issue.
Thanks!

I believe something like this might do the trick:
#Configuration
public class PropertiesConfig {
#Bean
public PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
final List<Resource> resources = new ArrayList<>();
resources.add(new FileSystemResource("/etc/app/application-{profile_1}.properties"));
resources.add(new FileSystemResource("/etc/app/application-{profile_2}.properties"));
ppc.setLocations(resourceLst.toArray(new Resource[]{}));
return ppc;
}
Please note, that this is just the suggestion, this code is not tested.
Profile specific application properties should be resolved automatically by current active profile.

I haven't tested this, but a #Configuration class with both #Profile and #PropertySource should work:
#Configuration
#Profile("default")
#PropertySource("file:/opt/myapp/myapp-ws.properties")
public class MyappWebservicePropertyConfig {
}

Related

Spring Boot: substitute #Configuration class with another

I have a custom configuration class that I am loading using spring factories during bootstrap. The problem is that it is being overwritten by another similar configuration class coming from a spring ** starter package. I've tried excluding the second one, but it still loads. Also tried to set priorities, but that didn't work too.
Here's a snippet of my custom configuration class:
#Slf4j
#Configuration
#RequiredArgsConstructor
public class CustomAwsParamStorePropertySourceLocatorConfig implements PropertySourceLocator
...
And the one I'm trying to exclude that is coming from spring boot aws starter:
public class AwsParamStorePropertySourceLocator implements PropertySourceLocator {
The AwsParamStoreBootstrapConfiguration class has the ConditionalOnProperty annotation at the class level...
#Configuration(proxyBeanMethods = false)
#EnableConfigurationProperties(AwsParamStoreProperties.class)
#ConditionalOnClass({ AWSSimpleSystemsManagement.class, AwsParamStorePropertySourceLocator.class })
#ConditionalOnProperty(prefix = AwsParamStoreProperties.CONFIG_PREFIX, name = "enabled", matchIfMissing = true)
public class AwsParamStoreBootstrapConfiguration {
private final Environment environment;
public AwsParamStoreBootstrapConfiguration(Environment environment) {
this.environment = environment;
}
#Bean
AwsParamStorePropertySourceLocator awsParamStorePropertySourceLocator(AWSSimpleSystemsManagement ssmClient,
AwsParamStoreProperties properties) {
if (StringUtils.isNullOrEmpty(properties.getName())) {
properties.setName(this.environment.getProperty("spring.application.name"));
}
return new AwsParamStorePropertySourceLocator(ssmClient, properties);
}
So if you configured the property aws.paramstore.enabled=false it should stop that configuration from creating the AwsParamStorePropertySourceLocator bean.
It's important to note, that would also stop the creation of the AWSSimpleSystemsManagement bean which is also created in the AwsParamStoreBootstrapConfiguration class, so if you require that bean, you may need to also create it in your custom Configuration class.
#Bean
#ConditionalOnMissingBean
AWSSimpleSystemsManagement ssmClient(AwsParamStoreProperties properties) {
return createSimpleSystemManagementClient(properties);
}
public static AWSSimpleSystemsManagement createSimpleSystemManagementClient(AwsParamStoreProperties properties) {
AWSSimpleSystemsManagementClientBuilder builder = AWSSimpleSystemsManagementClientBuilder.standard()
.withClientConfiguration(SpringCloudClientConfiguration.getClientConfiguration());
if (!StringUtils.isNullOrEmpty(properties.getRegion())) {
builder.withRegion(properties.getRegion());
}
if (properties.getEndpoint() != null) {
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
properties.getEndpoint().toString(), null);
builder.withEndpointConfiguration(endpointConfiguration);
}
return builder.build();
}

Swagger2 + Spring REST API not working

I have a Spring Rest controller and its NOT spring boot application. Its just a REST API Project. I want to integrate swagger2 in my project. I tried all the examples in Net and in demo but with no luck. When i try to execute http://localhost:8085/context/swagger-ui.html i get 404 error. Please find my confugration below and let me know if there is any discrepencies. Any help is highly appreciated.
jars - under /WEB-INF/lib
google-collections-1.0.jar
springfox-core-2.2.2.jar
springfox-schema-2.2.2.jar
springfox-spi-2.2.2.jar
springfox-spring-web-2.2.2.jar
springfox-staticdocs-2.2.2.jar
springfox-swagger-common-2.2.2.jar
springfox-swagger-ui-2.2.2.jar
springfox-swagger2-2.2.2.jar
My swagger config class -
#EnableSwagger2
public class SwaggerConfiguration {
}
My springconfig class
#EnableWebMvc
#ComponentScan(basePackageClasses = controller.class)
#Import(SwaggerConfiguration.class)
public class SpringConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
My Application initializer class below as per springfox-java demo . I tried with and without the below class and its not working either way.
Application Initializer class
public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{controller.class};
}
#Override
protected String[] getServletMappings() {
return new String[]{"/*"};
}
}
I can access my rest urls but not swagger-ui.html in the same context.
Please let me know what i am missing here?
I add the manual selection of controllers:
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("my.package.to.api"))
.paths(regex("/product.*")) //optional
.build();
}
}
given details are not sufficient to reproduce/analyse the issue.
But, today I faced similar problem, ofcourse used SpringBoot, and solved myself as below:
as my sample have one controller class and an application class having main method, I created packages as below, and it got solved:
hello
controllers
HelloController
swagger
SwaggerConfig2
HelloApplication

Spring Boot, CXF Spring MVC. Not exposing web service on JBoss

I've been googling for the last 2 days and still can't figure out why it wont expose the SOAP web service. It works when I run it locally using Tomcat and Spring Boot. In the jboss-deployment-structure, jpa, javaee-api and webservices has been excluded. Also, there are no exceptions in sysout or logs. Only the message specified further down (HTTP 405). Even checked through TRACE logs.
JBoss Version: 6.4.4 (or 6.4.0)
Spring Boot: 1.3.0
CXF: 3.0.4
Spring MVC: From Spring Boot parent
I get this message:
HTTP Status 405 - Request method 'POST' not supported.
When debugging, I placed a breakpoint in class: DispatcherServlet, which then in getHandlerAdapter returned HttpRequestHandlerAdapter. Further along, it calls the HttpRequestHandlerAdapter.handle which again calls handleRequest on a WebContentGenerator. This only supports GET or HEAD requests.
Any ideas?
Code:
#Configuration
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {
ApplicationConfig.class
};
}
#Override
protected String[] getServletMappings() {
return new String[] {
"/*"
};
}
}
#Configuration
public class ApplicationServletInitializer extends SpringBootServletInitializer {
}
#Configuration
#EnableAutoConfiguration
#Import({
RepositoryConfig.class,
WebConfig.class,
WsProviderConfig.class
})
public class ApplicationConfig {
}
#Configuration
#EnableConfigurationProperties
public class WebConfig {
#Bean
public SelftestController selftestController() {
return new SelftestController();
}
}
#Configuration
public class WsProviderConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(WsProviderConfig.class);
#Autowired
private ApplicationContext context;
#Bean
ServletRegistrationBean messageDispatcherServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/soap/*");
bean.setLoadOnStartup(1);
return bean;
}
#Bean(name = Bus.DEFAULT_BUS_ID)
Bus bus() {
SpringBus bus = new SpringBus();
bus.getFeatures().addAll(Arrays.asList(new WSAddressingFeature(), new LoggingFeature()));
return bus;
}
#Bean
PlayerServiceV1 defaultPlayerService() {
return new DefaultPlayerService();
}
#PostConstruct
void publishWebServices() {
LOGGER.info("Publishing WebServices");
DefaultPlayerService defaultPlayerService = context.getBean(DefaultPlayerService.class);
Bus bus = context.getBean(Bus.DEFAULT_BUS_ID, Bus.class);
EndpointImpl endpoint = new EndpointImpl(bus, defaultPlayerService);
endpoint.publish("/test/players");
}
}

How to get DispatcherServeletInitializer functionality in Spring Boot

We are looking to migrate our project to Spring Boot. However it is unclear how to replicate the functionality of AbstractAnnotationConfigDispatcherServletInitializer in Spring Boot?
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
#Override
protected Class<?>[] getRootConfigClasses()
{
return new Class<?>[]{AppConfig.class};
}
#Override
protected Class<?>[] getServletConfigClasses()
{
return new Class<?>[]{WebappConfig.class};
}
#Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setAsyncSupported(true);
}
#Override
protected String[] getServletMappings()
{
return new String[]{"/"};
}
#Override
protected Filter[] getServletFilters()
{
DelegatingFilterProxy shiroFilter = new DelegatingFilterProxy("shiroFilter");
shiroFilter.setTargetFilterLifecycle(true);
CompositeFilter compositeFilter = new CompositeFilter();
compositeFilter.setFilters(ImmutableList.of(new CorsFilter(),shiroFilter));
return new Filter[]{compositeFilter};
}
}
The AppConfig and WebappConfig parent/child relationship can be handled by SpringApplicationBuilder, although you might also consider a flat hierarchy.
Assuming that you are going the whole hog, and running an embedded servlet container you can register Filters and Servlets directly as beans.
You can also use ServletRegistrationBean and FilterRegistrationBean if you need to set things such as setAsyncSupported. The final option is to add a bean that implements org.springframework.boot.context.embedded.ServletContextInitializer then do the registration yourself.
Something like this might get you a bit further:
#Bean
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new DispatcherServlet(), "/");
registration.setAsyncSupported(true);
return registration;
}
#Bean
public Filter compositeFilter() {
CompositeFilter compositeFilter = new CompositeFilter();
compositeFilter.setFilters(ImmutableList.of(new CorsFilter(), shiroFilter));
return compositeFilter
}
Also, take a look at this section in the reference manual http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-embedded-container
Well there is nothing special like just mark your AppInitializer with Boot annotations:
#Configuration
#EnableAutoConfiguration
#ComponentScan
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
...
}
I haven't tried it, but just combined the documentation:
Normally all the code from an existing WebApplicationInitializer can be moved into a SpringBootServletInitializer. If your existing application has more than one ApplicationContext (e.g. if it uses AbstractDispatcherServletInitializer) then you might be able to squash all your context sources into a single SpringApplication.
And SpringBootServletInitializer JavaDocs:
If your application is more complicated consider using one of the
other WebApplicationInitializers.

Combining Spring MVC 3.2 with Coda Hales metrics

i am trying to combine a Spring Web Application (completed Annotation Based configuration, no xml configuration) with metrics 3.0.
I am running the application inside a jetty.
This is my current configuration for the default DispatcherServlet:
public class WebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
#Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
return new Filter[] { characterEncodingFilter };
}
}
This is the WebConfig:
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com.rebuy.silo.amqpredelivery")
#EnableJpaRepositories(basePackages = "com.rebuy.silo.amqpredelivery.domain")
#EnableAspectJAutoProxy
#EnableTransactionManagement
public class WebConfig extends WebMvcConfigurerAdapter {
#Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
#Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
jacksonConverter.setObjectMapper(objectMapper());
converters.add(jacksonConverter);
super.configureMessageConverters(converters);
}
#Bean
public ObjectMapper objectMapper() {
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssXXX");
format.setTimeZone(TimeZone.getTimeZone("GMT+1"));
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(format);
return mapper;
}
}
I want to add these two Servlets:
https://github.com/codahale/metrics/blob/master/metrics-servlets/src/main/java/com/codahale/metrics/servlets/HealthCheckServlet.java
https://github.com/codahale/metrics/blob/master/metrics-servlets/src/main/java/com/codahale/metrics/servlets/MetricsServlet.java
What is the best way to do this? I think there should be some spring magic to make this extremly easy to do! But I was not able to find it :(
Thanks in advance
Björn
You can follow this codebase https://github.com/spiritedtechie/metrics-examples.
Or use this library called metrics-spring http://ryantenney.github.io/metrics-spring/
If you are using Spring and Metrics you should also be using #RyanTenney's Metrics-Spring module. It will simplify your Java config and make your Metrics usage much cleaner.
Take a look at the code behind the MetricsServlet and HealthCheckServlet. In my opinion its easier to just write your own Spring Controller to do the same thing than to figure out how to embed and wrap those old servlets.
Its easy!
Create a metrics specific config:
#Configuration
#EnableMetrics
public class MetricsConfig extends MetricsConfigurerAdapter {
#Override
public void configureReporters(MetricRegistry metricRegistry) {
registerReporter(ConsoleReporter
.forRegistry(metricRegistry)
.build()).start(5, TimeUnit.MINUTES);
}
}
And include it from your existing config by adding:
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com.rebuy.silo.amqpredelivery")
#EnableJpaRepositories(basePackages = "com.rebuy.silo.amqpredelivery.domain")
#EnableAspectJAutoProxy
#EnableTransactionManagement
#Import({MetricsConfig.class})
public class WebConfig extends WebMvcConfigurerAdapter {
...
The above config changes make it trivial to inject a MetricRegistry in any Spring component. All the MetricsServlet does is send the registry in response to the request. That is really easy to accomplish in a simple controller. For example:
#Controller
public class AdminMetricsController
{
#Autowired
MetricRegistry metricRegistry;
#RequestMapping(value = "/admin/metrics/", produces = {APPLICATION_JSON_VALUE})
public #ResponseBody MetricRegistry getMetrics(final HttpServletResponse response)
{
response.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");
return metricRegistry;
}
}
A HealthCheckRegistry can be injected in a similar way and another method added which would respond to /admin/health/ or whatever url you wanted.
Take a look at the following answer. It explains how to register a Servlet via JavaConfig:
Spring JavaConfig: Add mapping for custom Servlet

Categories

Resources