Skip to content

Instantly share code, notes, and snippets.

View joshlong's full-sized avatar
🍃
i'm on Twitter @starbuxman

Josh Long joshlong

🍃
i'm on Twitter @starbuxman
View GitHub Profile
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String [] args ){
SpringApplication.run( Application.class, args);
}
}
@RestController
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@joshlong
joshlong / gist:9905863
Created April 1, 2014 01:12
As long as neither of the beans is used until later, this will work. Each bean will have a proxy to the real bean which will be materialized the first time it's needed.
@Configuration
class ASolutionForCircularDependencies {
@Bean
A a( @Lazy B b) {
return new A(b);
}
@Bean
@joshlong
joshlong / gist:10964238
Created April 17, 2014 08:23
Register this EmbeddedServletContainerCustomizer @bean in a Spring Boot application to customize the embedded Tomcat instance. In particular, this sets up SSL support for the container. I quite like this because it uses Java 8 lambas to nice effect. No finals, no anonymous inner classes. it just... works.
@Bean
EmbeddedServletContainerCustomizer containerCustomizer(
@Value("${keystore.file}") Resource keystoreFile,
@Value("${keystore.pass}") String keystorePass) throws Exception {
String absoluteKeystoreFile = keystoreFile.getFile().getAbsolutePath();
return (ConfigurableEmbeddedServletContainer container) -> {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
@SpELAssert(value = "password.equals(passwordVerify)",
applyIf = "password || passwordVerify",
message = "{validator.passwords_not_same}")
public class User {
private String password;
private String passwordVerify;
}
@joshlong
joshlong / Jsr330.java
Last active August 29, 2015 14:08
This demonstrates using JSR 330 annotations and JSR 330's `@Qualifier` (on Spring)
package jsr330;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import java.lang.annotation.ElementType;
@joshlong
joshlong / Spring.java
Last active August 29, 2015 14:08
This demonstrates using Spring-native annotations and Spring's `@Qualifier`
package spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@joshlong
joshlong / Spring.java
Last active August 29, 2015 14:08
This demonstrates using the `@Component` stereotype annotation on `@Qualifier` to omit it on types.
package spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
package demo;
import co.freeside.betamax.Betamax;
import co.freeside.betamax.Recorder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
@joshlong
joshlong / Application.java
Created May 28, 2015 20:13
A simple client that recursively scans a source code tree and invokes all `cf-deploy.sh`s it finds or, absent that, any present Cloud Foundry `manifest.yml`s by calling `cf push`. Ideally for continuous integration validation that code deploys and to do smoke tests like call the application's health codes and optionally stop a production deploy.
package deployment;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cloudfoundry.client.lib.CloudCredentials;
import org.cloudfoundry.client.lib.CloudFoundryClient;
import org.cloudfoundry.client.lib.domain.CloudApplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;