基于注解创建对象
- Component
- Service
- Controller
- Repository
- 引入aop依赖包
- 引入context命名空间
- 开启组件扫描
| 1 | <beans xmlns:context="http://www.springframework.org/schema/context" | 
- 创建UserService类
| 1 | //或者@Component(value = "userService") | 
- 测试
| 1 | UserService userService =context.getBean("userService", UserService.class); | 
基于注解注入属性
Autowired:根据类型进行自动装配
- 创建UserDaoImpl类
| 1 | 
 | 
- 创建UserService类
| 1 | 
 | 
Qualifier:根据名称注入,和Autowired一起用
- 创建UserDaoImpl类
| 1 | 
 | 
- 创建UserService类
| 1 | 
 | 
Resource:根据类型和名称注入,不建议使用
- 创建UserDaoImpl类
| 1 | 
 | 
- 创建UserService类
| 1 | 
 | 
Value:注入普通类型
- 创建UserService类
| 1 | 
 | 
完全注解开发
- 创建配置类SpringConfig
| 1 | 
 | 
- 创建UserService类
| 1 | 
 | 
- 测试
| 1 | 
 |