org.springframework.context.annotation.AnnotationConfigApplicationContext#scan ( )源码实例Demo

下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#scan ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: journaldev   文件: SpringMainClass.java
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();
}
 
源代码2 项目: syndesis   文件: SyndesisCommand.java
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;
}
 
源代码3 项目: spring-analysis-note   文件: Spr12334Tests.java
@Test
public void shouldNotScanTwice() {
	TestImport.scanned = false;

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan(TestImport.class.getPackage().getName());
	context.refresh();
	context.getBean(TestConfiguration.class);
}
 
源代码4 项目: journaldev   文件: SpringMainClass.java
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();
}
 
源代码5 项目: spring-analysis-note   文件: Spr8761Tests.java
/**
 * 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));
}
 
源代码6 项目: journaldev   文件: SpringMainClass.java
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();
}
 
源代码7 项目: spring-tutorial   文件: ComponentScanInJava.java
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());
}
 
源代码8 项目: java-technology-stack   文件: Spr8761Tests.java
/**
 * 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));
}
 
源代码9 项目: tutorials   文件: TenantScopeIntegrationTest.java
@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();
    }
}
 
源代码10 项目: netstrap   文件: NetstrapBootApplication.java
/**
 * 创建Spring容器
 */
private ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(new ClassPathXmlApplicationContext(configLocations));
    context.scan(DEFAULT_SCAN);
    context.refresh();
    return context;
}
 
源代码11 项目: ace   文件: SpringContainer.java
@Override
public void init(String... packages) {
    applicationContext = new AnnotationConfigApplicationContext();
    Config config= DefaultConfig.INSTANCE;
    ConfigBeanFactoryPostProcessor configBeanFactoryPostProcessor = new ConfigBeanFactoryPostProcessor(config);
    applicationContext.addBeanFactoryPostProcessor(configBeanFactoryPostProcessor);
    applicationContext.scan(packages);

}
 
源代码12 项目: journaldev   文件: SpringMainClass.java
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();
}
 
源代码13 项目: circus-train   文件: ExtensionInitializer.java
@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()]));
  }
}
 
源代码14 项目: spring4-understanding   文件: Spr12334Tests.java
@Test
public void shouldNotScanTwice() {
	TestImport.scanned = false;

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.scan(TestImport.class.getPackage().getName());
	context.refresh();
	context.getBean(TestConfiguration.class);
}
 
源代码15 项目: spring4-understanding   文件: Spr8761Tests.java
/**
 * 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));
}
 
源代码16 项目: journaldev   文件: SpringMainClass.java
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();
}
 
源代码17 项目: spring4-understanding   文件: Spr8955Tests.java
@Test
public void repro() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.scan("org.springframework.context.annotation.configuration.spr8955");
	ctx.refresh();
}
 
源代码18 项目: mpns   文件: SpringConfig.java
static void scanHandler() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(CTX);
    context.scan("com.mpush.mpns.web.handler");
    context.refresh();
}
 
源代码19 项目: spring-analysis-note   文件: Spr10546Tests.java
/**
 * 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"));
}
 
源代码20 项目: spring4-understanding   文件: Spr10546Tests.java
/**
 * 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"));
}