日期:2014-05-17  浏览次数:20707 次

spring配置文件问题
spring的配置文件,applicationContext.xml中有这样一条配置

<context:component-scan base-package="com.strongit.nj" />,这条配置的作用是扫描com.strongit.nj包下的所有类。
我的疑惑是扫描这些类时会做那些事?(本人猜测,会将扫码的类注册到spring容器,即实例化这些类,再者是读取被扫描的类上的spring注解)有哪位大神知道的给小弟指点一二,不胜感激!

------解决方案--------------------
根据注解将对应的类型实例化,注入到目标属性上
------解决方案--------------------
扫描这些包下有注解的,自动注入呗
------解决方案--------------------
可以看文档么, 默认扫描 @Component, @Repository, @Service, and @Controller 注解。



Scans the classpath for annotated components that will be auto-registered as
Spring beans. By default, the Spring-provided @Component, @Repository,
@Service, and @Controller stereotypes will be detected.

Note: This tag implies the effects of the 'annotation-config' tag, activating @Required,
@Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit
annotations in the component classes, which is usually desired for autodetected components
(without external configuration). Turn off the 'annotation-config' attribute to deactivate
this default behavior, for example in order to use custom BeanPostProcessor definitions
for handling those annotations.

Note: You may use placeholders in package paths, but only resolved against system
properties (analogous to resource paths). A component scan results in new bean definition
being registered; Spring's PropertyPlaceholderConfigurer will apply to those bean
definitions just like to regular bean definitions, but it won't apply to the component
scan settings themselves.

See Javadoc for org.springframework.context.annotation.ComponentScan for information