Skip to content

Instantly share code, notes, and snippets.

@bijukunjummen
Created April 8, 2015 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bijukunjummen/847456b55ae2340fff65 to your computer and use it in GitHub Desktop.
Save bijukunjummen/847456b55ae2340fff65 to your computer and use it in GitHub Desktop.
Spring Enable Annotation
package enableannot.simple;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestEnableAnnotations {
@Autowired
private String aBean1;
@Autowired
private String aBean2;
@Test
public void testEnableShouldLoadFamilyOfBeans() {
assertThat(aBean1, equalTo("aBean1"));
assertThat(aBean2, equalTo("aBean2"));
}
@Configuration
@EnableSomeBeans
public static class SpringConfig {}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(SomeBeanConfiguration.class)
@interface EnableSomeBeans {}
@Configuration
class SomeBeanConfiguration {
@Bean
public String aBean1() {
return "aBean1";
}
@Bean
public String aBean2() {
return "aBean2";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment