[b]1.-----依赖注入----[/b]
关系注入
基本注入
集合注入
[b]2.-----自动装配----[/b]
<bean>元素的autowire属性,可使用byName、byType、constructor等
[b]3.-----注解应用------[/b]
注入注解:
a. @Autowired(required=false) @Qualifier("bean名称")
b. @Resource默认按照名称、之后按照类型@Resource(name="bean名称")
c. XML配置文件使用<context:annotation-config></context:annotation-config>
自动扫描
a. @Controller、@Service、@Repository、@Component
当容器将组件扫描后,组件对象的名称默认类名首字母小写,
也可以使用@Service("bean名称")
b. XML配置文件使用<context:component-scan base-package="tarena.annotation"/>
c. 使用范围和声明周期@Scope("singleton")、@PostConstruct、@PreDestroy
关系注入
基本注入
集合注入
[b]2.-----自动装配----[/b]
<bean>元素的autowire属性,可使用byName、byType、constructor等
[b]3.-----注解应用------[/b]
注入注解:
a. @Autowired(required=false) @Qualifier("bean名称")
b. @Resource默认按照名称、之后按照类型@Resource(name="bean名称")
c. XML配置文件使用<context:annotation-config></context:annotation-config>
自动扫描
a. @Controller、@Service、@Repository、@Component
当容器将组件扫描后,组件对象的名称默认类名首字母小写,
也可以使用@Service("bean名称")
b. XML配置文件使用<context:component-scan base-package="tarena.annotation"/>
c. 使用范围和声明周期@Scope("singleton")、@PostConstruct、@PreDestroy
本文详细介绍了Spring框架中依赖注入的概念及实现方式,包括基本的关系注入、集合注入、自动装配及注解的应用。此外还讲解了如何通过XML配置文件启用注解配置及组件扫描。
631

被折叠的 条评论
为什么被折叠?



