下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#scan ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
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;
}
@Test
public void shouldNotScanTwice() {
TestImport.scanned = false;
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan(TestImport.class.getPackage().getName());
context.refresh();
context.getBean(TestConfiguration.class);
}
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();
}
/**
* 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));
}
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();
}
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());
}
/**
* 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));
}
@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();
}
}
/**
* 创建Spring容器
*/
private ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setParent(new ClassPathXmlApplicationContext(configLocations));
context.scan(DEFAULT_SCAN);
context.refresh();
return context;
}
@Override
public void init(String... packages) {
applicationContext = new AnnotationConfigApplicationContext();
Config config= DefaultConfig.INSTANCE;
ConfigBeanFactoryPostProcessor configBeanFactoryPostProcessor = new ConfigBeanFactoryPostProcessor(config);
applicationContext.addBeanFactoryPostProcessor(configBeanFactoryPostProcessor);
applicationContext.scan(packages);
}
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();
}
@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()]));
}
}
@Test
public void shouldNotScanTwice() {
TestImport.scanned = false;
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan(TestImport.class.getPackage().getName());
context.refresh();
context.getBean(TestConfiguration.class);
}
/**
* 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));
}
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();
}
@Test
public void repro() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("org.springframework.context.annotation.configuration.spr8955");
ctx.refresh();
}
static void scanHandler() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setParent(CTX);
context.scan("com.mpush.mpns.web.handler");
context.refresh();
}
/**
* 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"));
}
/**
* 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"));
}