============== Spring组件扫描 ============== 1 组件注解和组件扫描注解 借助组件注解和组件扫描注解,可以让Spring自动扫描包空间,创建Bean对象。 package cn.tedu; @Component public class HelloComponent { @Override public String toString() { return "Hello Component"; } } @Configuration @ComponentScan(basePackages="cn.tedu") public class Config { } AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); HelloComponent helloComponent = ctx.getBean( "helloComponent", HelloComponent.class); System.out.println(helloComponent); 注意:Bean对象的ID取Bean类的名字首字母小写。 例程:HelloComponent 2 多种组件注解 不同用途的组件使用不同的组件注解。 - Controller:控制器组件 - Service:业务层组件 - Component:通用组件 这些注解的作用都是一样的,区分的目的仅为增加代码的可读性。 例程:HelloComponent