package com.richard.test.spring.annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;

/**
* @author RichardYao richardyao@tvunetworks.com
* @date Mar 10, 2017 10:41:15 AM
* 使用自定义组件，这里必须把AnnotationUse加上Configuration注解，否则会导致Spring扫描InjectClass时找不到内部类定义
*/
@Configuration
public class AnnotationUse {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
		applicationContext.register(AnnotationUse.class);
		applicationContext.refresh();
		InjectClass injectClass = applicationContext.getBean(InjectClass.class);
		injectClass.print();
		SimpleTest beanTest = applicationContext.getBean(SimpleTest.class); //这里会抛出异常：No qualifying bean of type [com.richard.test.spring.annotation.SimpleTest] is defined
		beanTest.printOut();
		applicationContext.close();
	}
	
	/**
	 * @author RichardYao
	 * 这里必须是静态类才能使得applicationContext.getBean()方法调用正常, why?
	 */
	@MyComponent
	public static class InjectClass {
		
		public void print() {
			System.out.println("Hello world!");
		}
	}

}

@@ -347,16 +347,82 @@
 %E4%B8%8D%E5%88%B0%E5%86%85%E9%83%A8%E7%B1%BB%E5%AE%9A%E4%B9%89%0A
+* %E6%88%96%E8%80%85%E4%B8%8D%E4%BD%BF%E7%94%A8%E6%B3%A8%E8%A7%A3%E8%80%8C%E5%8D%95%E7%8B%AC%E5%9C%A8applicationContext%E5%AE%B9%E5%99%A8%E4%B8%AD%E5%B0%86AnnotationUse, InjectClass%E9%83%BD%E6%B3%A8%E5%86%8C%E4%BA%86%0A
 */%0A@Conf
@@ -654,16 +654,208 @@
 .class);
+ %0A%09%09//applicationContext.register(InjectClass.class);%0A%09%09applicationContext.register(SimpleTest.class); //%E9%9C%80%E8%A6%81%E5%8D%95%E7%8B%AC%E5%9C%A8applicationContext.register(class)%E4%B8%AD%E6%B3%A8%E5%86%8C%E5%90%8E%E6%89%8D%E5%8F%AF%E4%BB%A5%E6%AD%A3%E5%B8%B8%E4%BD%BF%E7%94%A8applicationContext.getBean(class)%E6%9D%A5%E5%AE%9E%E4%BE%8B%E5%8C%96
 %0A%09%09appli
@@ -1050,104 +1050,8 @@
 ss);
- //%E8%BF%99%E9%87%8C%E4%BC%9A%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8%EF%BC%9ANo qualifying bean of type %5Bcom.richard.test.spring.annotation.SimpleTest%5D is defined
 %0A%09%09b
@@ -1141,20 +1141,8 @@
 %09 * 
-%E8%BF%99%E9%87%8C%E5%BF%85%E9%A1%BB%E6%98%AF%E9%9D%99%E6%80%81%E7%B1%BB%E6%89%8D%E8%83%BD%E4%BD%BF%E5%BE%97
 appl
@@ -1168,21 +1168,29 @@
 ean(
-)%E6%96%B9%E6%B3%95%E8%B0%83%E7%94%A8%E6%AD%A3%E5%B8%B8, why?
+Class ...)%E6%97%A0%E6%B3%95%E5%AE%9E%E4%BE%8B%E5%8C%96%E9%9D%9E%E9%9D%99%E6%80%81%E5%86%85%E9%83%A8%E7%B1%BB
 %0A%09 *

use annotation and try to understand spring application context register beans