Spring的注解说明

1 Spring注解1.1 要求开启包扫描<!-- 开启注解扫描 --><context:component-scan base-package="com.zgd.learn.spring.demo02"/>1.1 通用注解1.1.1 @Component类注解,被spring管理,beanId为anno,有下面两种写法@Component 不添加参数,默认为类名的首字母小写@Component(“anno”),id注入@Component(value =
摘要由CSDN通过智能技术生成

1 Spring注解

1.1 spring注解使用配置

<!-- 开启注解扫描 -->
<context:component-scan base-package="com.zgd.learn.spring.demo02"/>

1.1 通用注解

1.1.1 @Component

  • 类注解,被spring管理,beanId为anno,有下面两种写法
  • @Component 不添加参数,默认为类名的首字母小写
  • @Component(“anno”),id注入
  • @Component(value = “anno”),id注入

1.1.2 @Controller

  • 类注解,@Component的衍生注解,@Controller常用于注解Web的控制层,如Servlet
  • @Controller 不添加参数,默认为类名的首字母小写
  • @Controller(“anno”),id注入
  • @Controller(value = “anno”),id注入

1.1.3 @Service

  • 类注解,@Component的衍生注解,@Service常用于注解Service层
  • @Service 不添加参数,默认为类名的首字母小写
  • @Service(“anno”),id注入
  • @Service(value = “anno”),id注入

1.1.4 @Repository

  • 类注解,@Component的衍生注解,@Repository常用于注解Dao层
  • @Repository,不添加参数,默认为类名的首字母小写
  • @Repository(“anno”),id注入
  • @Repository(value = “anno”),id注入

2.1 属性注解

1.2.1 @Resource

  • 属性注解,@Resource用于对属性的注入
  • @Resource 无添加参数,则按照类型自动注入
  • @Resource(name = “anno”),id注入
  • @Resource(type = Anno.class),类型注入

1.2.2 @Autowired

  • 属性注解,@Autowired用于对属性的注入
  • @Autowired,无参按类型注入
  • 常和@Qualifier配合按照id注入

1.2.3 @Qualifier

  • 属性注入,@Qualifier不可单独使用,可和@Autowired配合使用
    @Qualifier(“anno”),id注入
    @Qualifier(value=“anno”),id注入

1.3 spring测试注解

1.3.1 @RunWith(SpringJUnit4ClassRunner.class)

  • 类注解,表明该类为spring的测试类,固定写法

1.3.2 @ContextConfiguration(“classpath:spring.xml”)

  • 类注解,表明使用spring.xml配置作为spring的配置文件
  • @ContextConfiguration(locations ={ “classpat
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值