Spring 的 Bean 管理:( 注解方式)
Spring 的 Bean 管理:( 注解方式)
1. Spring 的 Bean 管理 :(注解方式)
步骤一: 下载 Spring 的开发包:
步骤二: 创建 web 项目, 引入 Spring 的开发包
步骤三: 引入相关配置文件
applicationContent.xml
步骤四: 编写相关的类
步骤五: 配置注解扫描
<!-- Spring 的注解开发:组件扫描(类上注解: 可以直接使用属性注入的注解) -->
在相关的类上添加注解:
编写测试类:
效果图:
Spring 集成junit4 测试
导入spring-test.jar包
@RunWith(SpringJUnit4ClassRunner.class) 使用junit4进行测试
@ContextConfiguration() 加载spring相关的配置文件
Spring 的 Bean 管理的中常用的注解: :
@Component: 组件.( 作用在类上)
属性注入的注解:( 使用注解注入的方式, 可以不用提供 set 方法.)
Bean 的作用范围的注解:
Bean 的生命周期的配置
Spring 的 Bean 管理的方式的比较: