7-Spring注解开发

Spring注解开发

Spring注解开发,仍然不是零配置
在Spring 4之后,要使用注解开发,必须导入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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.shang.pojo"/>
    <context:annotation-config/><!--有上面包扫描,此行可省略-->

</beans>
  1. 怎么将Bean加入到容器@Component
@Component
public class People {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                '}';
    }
}
  1. 属性如何注入?@Value
@Component
public class People {
	@Value("shang") //<property name="name" value="shang"></property>
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                '}';
    }
}
  1. 衍生的注解
    @Component有三个衍生注解,我们在web开发中,会按照mvc三层架构分层!
    @Controller@Service@Repository
    这四个注解都表示将当前类加入到容器中,也就是装配Bean
  2. 自动装配
    前面有介绍
  3. 作用域 @Scope

在SpringBoot开发中,我们会使用大量注解,我们要知道哪些注解是Spring的,哪些注解是SpringBoot特有的,到时候千万不要说你不知道!

[下一篇:基于Java的配置]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值