在spring4之后,要使用注解开发,必须要保证aop的包导入了
1,bean
2,属性如何注入
@Component
public class User {
public String name;
//等价于<property name="name" value="小明"/>
@Value("小明")
public void setName(String name) {
this.name = name;
}
}
3,衍生的注解
@Component有几个衍生的注解,我们在web开发中,会按照MVC三层架构分层
DAO层:---@Repository
Serivce层 --@Service
Controller层--@Controller
5,作用域
@Scope("prototype")
6,小结
xml与注解
。xml更加万能,适用于任何场景。维护简单方便
。注解:不是自己的类使用不了,维护相对复杂。
xml与注解实践
。xml用来管理bean
。注解只负责完成属性的注入
。我们在使用的过程中,只需要注意一个问题: 必须让注解生效,就需要在配置文件中开启注解的支持。
<!-- 指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.tan"/>
<!-- 开启注解的支持-->
<context:annotation-config/>
原文:https://www.cnblogs.com/xinchengTan/p/13099885.html