http://blog.csdn.net/longeremmy/article/details/8289924
在项目配置中,用注解代替配置,已经很普遍了。
在spring中,有看到Component,Repository,Service,Controller
在研究这四个注解之前,需要先了解ClassPathBeanDefinitionScanner
我们在application.xml配置的<context:component-scan base-package="a.b.c" />
后台处理的bean就是这个类。以下是源代码的描述。
其中这个类的属性BeanDefinitionDefaults beanDefinitionDefaults,管理了spring容器处理这些bean的方式。如是否支持延时加载,自动匹配规则,初始化方法,销毁方法等。
/** * A bean definition scanner that detects bean candidates on the classpath, * registering corresponding bean definitions with a given registry (BeanFactory * or ApplicationContext). * * <p>Candidate classes are detected through configurable type filters. The * default filters include classes that are annotated with Spring's * {@link org.springframework.stereotype.Component @Component}, * {@link org.springframework.stereotype.Repository @Repository}, * {@link org.springframework.stereotype.Service @Service}, or * {@link org.springframework.stereotype.Controller @Controller} stereotype. * * @author Mark Fisher * @author Juergen Hoeller * @since 2.5 * @see org.springframework.stereotype.Component * @see org.springframework.stereotype.Repository * @see org.springframework.stereotype.Service * @see org.springframework.stereotype.Controller */
package car; public interface Car { public String getCarName(); }
package car; import org.springframework.stereotype.Service; //@Service public class Audi implements Car{ @Override public String getCarName() { return "AUDI"; } }
package car; import org.springframework.stereotype.Service; @Service public class Bmw implements Car{ @Override public String getCarName() { return "BMW"; } }
package car; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class User { @Autowired //@Qualifier("audi") private Car carssssss; public String getCar() { return carssssss.getCarName(); } }
<context:component-scan base-package="car" />
package car; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { System.out.println(""); ApplicationContext context = new ClassPathXmlApplicationContext("car/test.xml"); String[] names = context.getBeanDefinitionNames(); for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } } }
上面的测试类main方法,并没有报错,至少可以看出两点:
(1)通过Service注解,注册到spring容器的名字是类名首字小写。
(2)用Autowired注解,因为User类,属性名是瞎写的,但依然可以注入成功,说明 默认是根据类型进行注入的。
(3)把Audi类中的注解打开,就会抛异常,是因为spring容器有两个相同的类型,spring容器不知道应该注入哪个类。出现这种场合,需要@Qualifier("audi")来指定用哪个类来进行注入。这时autowired注入方式,由byType转成byName.
(4)@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,面 @Resource 默认按 byName 自动注入罢了。@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。
打印结果:
bmw
user
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
从上图,用Component,Repository,Service,Controller四个注解,都可以把类注册到spring容器里,但这四个有什么区别呢。使用上没什么特别的,都可以把类注册到spring中。但是四个注解暗示类代表着不同的含义.
1、@Service用于标注业务层组件
2、@Controller用于标注控制层组件(如struts中的action)
3、@Repository用于标注数据访问组件,即DAO组件.
4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
具体信息可以参考源代码关于注解的描述。