Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#scan()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#scan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SyndesisCommand.java    From syndesis with Apache License 2.0 6 votes vote down vote up
private AbstractApplicationContext createContext() {
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

    final YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader();
    final List<PropertySource<?>> yamlPropertySources;
    try {
        yamlPropertySources = propertySourceLoader.load(name, context.getResource("classpath:" + name + ".yml"));
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }

    final StandardEnvironment environment = new StandardEnvironment();
    final MutablePropertySources propertySources = environment.getPropertySources();
    propertySources.addFirst(new MapPropertySource("parameters", parameters));
    yamlPropertySources.forEach(propertySources::addLast);

    context.setEnvironment(environment);

    final String packageName = getClass().getPackage().getName();
    context.scan(packageName);

    context.refresh();

    return context;
}
 
Example 2
Source File: SpringMainClass.java    From journaldev with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan("com.journaldev.spring");
	context.refresh();
	System.out.println("Spring Context Refreshed");
	// Getting Bean by Class
	MyDAOBean myDAOBean = context.getBean(MyDAOBean.class);
	System.out.println(myDAOBean);

	MyFileSystemBean myFileSystemBean = (MyFileSystemBean) context.getBean("getMyFileSystemBean");
	System.out.println(myFileSystemBean);

	MyFileSystemBean myFileSystemBean1 = (MyFileSystemBean) context.getBean("MyFileSystemBean");
	System.out.println(myFileSystemBean1);

	context.close();
}
 
Example 3
Source File: SpringContainer.java    From ace with Apache License 2.0 5 votes vote down vote up
@Override
public void init(String... packages) {
    applicationContext = new AnnotationConfigApplicationContext();
    Config config= DefaultConfig.INSTANCE;
    ConfigBeanFactoryPostProcessor configBeanFactoryPostProcessor = new ConfigBeanFactoryPostProcessor(config);
    applicationContext.addBeanFactoryPostProcessor(configBeanFactoryPostProcessor);
    applicationContext.scan(packages);

}
 
Example 4
Source File: SpringMainClass.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) throws SQLException {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan("com.journaldev.spring");
	context.refresh();
	System.out.println("Refreshing the spring context");
	DBConnection dbConnection = context.getBean(DBConnection.class);

	dbConnection.printDBConfigs();
	
	// close the spring context
	context.close();
}
 
Example 5
Source File: Spr8761Tests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Prior to the fix for SPR-8761, this test threw because the nested MyComponent
 * annotation was being falsely considered as a 'lite' Configuration class candidate.
 */
@Test
public void repro() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.scan(getClass().getPackage().getName());
	ctx.refresh();
	assertThat(ctx.containsBean("withNestedAnnotation"), is(true));
}
 
Example 6
Source File: Spr12334Tests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotScanTwice() {
	TestImport.scanned = false;

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan(TestImport.class.getPackage().getName());
	context.refresh();
	context.getBean(TestConfiguration.class);
}
 
Example 7
Source File: ExtensionInitializer.java    From circus-train with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(AnnotationConfigApplicationContext context) {
  Set<String> packageNames = provider.getPackageNames(context.getEnvironment());
  if (packageNames.size() > 0) {
    LOG.info("Adding packageNames '{}' to component scan.", packageNames);
    context.scan(packageNames.toArray(new String[packageNames.size()]));
  }
}
 
Example 8
Source File: SpringMainClass.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) throws SQLException {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan("com.journaldev.spring");
	context.refresh();

	DBConnection dbConnection = context.getBean(DBConnection.class);

	Connection con = dbConnection.getConnection();

	System.out.println(con.getMetaData().getDatabaseProductName());
	System.out.println(con.getMetaData().getDatabaseProductVersion());

	// close the spring context
	context.close();
}
 
Example 9
Source File: Spr12334Tests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void shouldNotScanTwice() {
	TestImport.scanned = false;

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan(TestImport.class.getPackage().getName());
	context.refresh();
	context.getBean(TestConfiguration.class);
}
 
Example 10
Source File: NetstrapBootApplication.java    From netstrap with Apache License 2.0 5 votes vote down vote up
/**
 * 创建Spring容器
 */
private ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(new ClassPathXmlApplicationContext(configLocations));
    context.scan(DEFAULT_SCAN);
    context.refresh();
    return context;
}
 
Example 11
Source File: TenantScopeIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void whenComponentScan_thenContextContainsFooAndBar() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    try {
        ctx.scan("com.baeldung.customscope");
        ctx.refresh();

        TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
        foo.sayHello();
        TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
        bar.sayHello();
        Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);

        assertThat(foo, not(equalTo(bar)));
        assertThat(foos.size(), equalTo(2));
        assertTrue(foos.containsValue(foo));
        assertTrue(foos.containsValue(bar));

        BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
        BeanDefinition barDefinition = ctx.getBeanDefinition("bar");

        assertThat(fooDefinition.getScope(), equalTo("tenant"));
        assertThat(barDefinition.getScope(), equalTo("tenant"));
    } finally {
        ctx.close();
    }
}
 
Example 12
Source File: Spr8761Tests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Prior to the fix for SPR-8761, this test threw because the nested MyComponent
 * annotation was being falsely considered as a 'lite' Configuration class candidate.
 */
@Test
public void repro() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.scan(getClass().getPackage().getName());
	ctx.refresh();
	assertThat(ctx.containsBean("withNestedAnnotation"), is(true));
}
 
Example 13
Source File: ComponentScanInJava.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void main(String[] args) {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.scan("io.github.dunwu.spring.ioc");
	ctx.refresh();
	Teacher teacher = (Teacher) ctx.getBean("teacher");
	System.out.println(teacher.work());
}
 
Example 14
Source File: SpringMainClass.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan("com.journaldev.spring");
	context.refresh();

	MathComponent ms = context.getBean(MathComponent.class);

	int result = ms.add(1, 2);
	System.out.println("Addition of 1 and 2 = " + result);

	context.close();
}
 
Example 15
Source File: Spr8761Tests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Prior to the fix for SPR-8761, this test threw because the nested MyComponent
 * annotation was being falsely considered as a 'lite' Configuration class candidate.
 */
@Test
public void repro() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.scan(getClass().getPackage().getName());
	ctx.refresh();
	assertThat(ctx.containsBean("withNestedAnnotation"), is(true));
}
 
Example 16
Source File: SpringMainClass.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) throws SQLException {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan("com.journaldev.spring");
	context.refresh();

	EmployeeRepository repository = context.getBean(EmployeeRepository.class);

	// store
	repository.store(new Employee(1, "Pankaj", "CEO"));
	repository.store(new Employee(2, "Anupam", "Editor"));
	repository.store(new Employee(3, "Meghna", "CFO"));

	// retrieve
	Employee emp = repository.retrieve(1);
	System.out.println(emp);

	// search
	Employee cfo = repository.search("Meghna");
	System.out.println(cfo);

	// delete
	Employee editor = repository.delete(2);
	System.out.println(editor);

	// close the spring context
	context.close();
}
 
Example 17
Source File: Spr8955Tests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void repro() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.scan("org.springframework.context.annotation.configuration.spr8955");
	ctx.refresh();
}
 
Example 18
Source File: SpringConfig.java    From mpns with Apache License 2.0 4 votes vote down vote up
static void scanHandler() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(CTX);
    context.scan("com.mpush.mpns.web.handler");
    context.refresh();
}
 
Example 19
Source File: Spr10546Tests.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Prior to fixing SPR-10546 this might have succeeded depending on the ordering the
 * classes were picked up. If they are picked up in the same order as
 * {@link #enclosingConfigFirstParentDefinesBean()} then it would fail. This test is
 * mostly for illustration purposes, but doesn't hurt to continue using it.
 *
 * <p>We purposely use the {@link AEnclosingConfig} to make it alphabetically prior to the
 * {@link AEnclosingConfig.ChildConfig} which encourages this to occur with the
 * classpath scanning implementation being used by the author of this test.
 */
@Test
public void enclosingConfigFirstParentDefinesBeanWithScanning() {
	AnnotationConfigApplicationContext ctx= new AnnotationConfigApplicationContext();
	context = ctx;
	ctx.scan(AEnclosingConfig.class.getPackage().getName());
	ctx.refresh();
	assertThat(context.getBean("myBean",String.class), equalTo("myBean"));
}
 
Example 20
Source File: Spr10546Tests.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Prior to fixing SPR-10546 this might have succeeded depending on the ordering the
 * classes were picked up. If they are picked up in the same order as
 * {@link #enclosingConfigFirstParentDefinesBean()} then it would fail. This test is
 * mostly for illustration purposes, but doesn't hurt to continue using it.
 *
 * <p>We purposely use the {@link AEnclosingConfig} to make it alphabetically prior to the
 * {@link AEnclosingConfig.ChildConfig} which encourages this to occur with the
 * classpath scanning implementation being used by the author of this test.
 */
@Test
public void enclosingConfigFirstParentDefinesBeanWithScanning() {
	AnnotationConfigApplicationContext ctx= new AnnotationConfigApplicationContext();
	context = ctx;
	ctx.scan(AEnclosingConfig.class.getPackage().getName());
	ctx.refresh();
	assertThat(context.getBean("myBean",String.class), equalTo("myBean"));
}