Spring学习笔记(七)。Spring使用注解开发,@Component,@Value,衍生注解@Repository,@Service,@Controller,作用域@scope

本文介绍了如何在Spring 4.0以后利用注解进行Bean的自动装配,包括@Component的使用、属性注入@Value、衍生注解@Controller、@Service、@Repository,以及作用域和组件扫描的配置。重点讲解了注解在简化配置和提高开发效率中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用注解开发

1. 说明

在spring4之后,想要使用注解形式,必须得要引入aop的包

在这里插入图片描述

在配置文件当中,还得要引入一个context约束

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

2. Bean的实现@Component

我们之前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解!

  1. 配置扫描哪些包下的注解
<!--指定注解扫描包-->
<context:component-scan base-package="com.xxc.pojo"/>
  1. 在指定包下编写类,增加注解
@Component("user")  // 不写括号的时候默认为类首字母小写名
// 相当于配置文件中 &
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傻啦猫@_@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值