【Spring】学习笔记08-使用注解开发
在Spring4之后,要使用注解开发,必须要保证aop包的导入
使用注解
1.bean
2.属性如何注入
import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; //等价于<bean id='user' class="com.wang.pojo.User"/> @Component @Data public class User { @Value("王广元") //等价于<property name="name" value="kuangshen"/> private String name; }
3.衍生的注解
@Component有几个衍生注解,在web开发中,会按照mvc三层架构分层
Dao层:@Repository
Service层:@Service
Controller层:@Controller
4.自动装配置
5.作用域
@Scope("prototype"):原型模式
@Scope("Singloten"):单例模式
6.小结
xml与注解
- xml更加万能,适合于任何场合!维护简单方法!
- 注解不是自己类使用不了,维护相对复杂!
xml与注解最佳实践:
- xml用来管理bean;
- 注解只负责完成属性的注入;
- 我们在使用的过程中,只需要注意一个问题:必须要注解生效,就必须开启注解的支持;
<context:component-scan base-package="com.wang"/> <context:annotation-config/>
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术