Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.