使用注解注入
在Spring4之后,要使用注解开发,必须要保证AOP的包导入了
-
bean
-
属性如何注入
-
在applicationContext.xml中指定要扫描的包,才能使用注解
-
<!--指定要扫描的包,这个包小的注解就会生效--> <context:component-scan base-package="com.haoyun.POJO"/>
-
-
衍生的注解
-
@Component有几个衍生注解,我们在web开发中,会按照MVC三层架构分层
- 添加后应该看得到叶子
-
dao【@Repository】
-
@Repository public class User { @Value("haoyun") private String name ; }
-
-
service【@Service】
-
@Service
-
-
controller【@Controller】
-
@Controller
-
四个注解功能是一样的,都是代表将某个类注册到Spring中,装配Bean
-
-
自动装配
- 之前说过
-
作用域
-
@Scope()
-
@Scope("singleton")
-
标记什么模式
-
-
小结
- XML与注解
- XML适用于任何场合,维护简单
- 注解:不是自己的类使用不了,维护相对复杂,
- 最佳配合方法
- xml管理Bean
- 注解只负责完成属性注入
- 还是觉得xml好用,配合lambox好用