Spring——使用注解开发

在Spring4之后,要使用注解开发,必须保证aop的包被导入了,导入context的约束,增加注解的支持

1.bean

@Component

2.属性如何注入

@value

 package com.yl.pojo;
 
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
 //等价于<bean id = "user" class = "com.yl.pojo.User">
 @Component
 public class User {
     @Value("lulu")//属性注入
     public String name;
 }
 

3.衍生注解

@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层。

  • dao:@Repository

  • service:@Service

  • controller:@Controller

    这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean

4.作用域

@Scope:@Scope("singleton")

 

xml与注解:

  • xml更加万能,适用于任何场合,维护简单方便

  • 注解:不是自己类使用不了,维护相对复杂

  • 推荐xml用来管理bean,注解负责属性的注入

posted @ 2020-09-02 15:27  Fabulo  阅读(118)  评论(0)    收藏  举报