Spring5(12) —— Spring注解开发


    注解和XML配置文件之间的选择,永远都是简单的配置可以直接使用注解,复杂的配置还是需要使用XML配置文件

    即XML配置文件适用于任何情况,注解只适用于简单情况


使用准备

在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
       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:annotation-config/>

</beans>

环境搭建

  • 创建新项目:Spring-06-Annotation
  • 创建spring容器:applicationContext.xml
    <?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:annotation-config/>
    
    </beans>
    
  • 在applicationContext.xml中添加一个新配置
    <!--扫描指定包,这个包里面的注解就会生效-->
    <context:component-scan base-package="com.thhh"/>
    <context:annotation-config/>
    
    
  • 创建完整包结构
    在这里插入图片描述

1.bean / @Component

    @Component = bean id=“XXX” class = “XXX.XXX.XXX.XXX”
    用的时候直接将这个注解写在类定义上面

    使用了@Component 注解的类表示这个类被spring管理了,这个类已经注册到了容器中了,我们可以获取对应的bean了

package com.thhh.pojo;

import org.springframework.stereotype.Component;

@Component
public class User {
    public String name = "张三";
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.属性 / @Value(“属性值”)

    @Value(“属性值”) = property name = “xxx” value = “xxx”
    用的时候直接将这个注解写在 属性/属性对应的set方法 上面
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.衍生的注解

    衍生的注解主要说的就是@Component 注解在web开发的时候,按照MVC3层架构,有3个别名

  • 【Dao】:@Repository
  • 【Service】:@Service
  • 【Controller】:@Controller

    注意@Component、@Repository、@Service、@Controller这4个注解其实都是一个注解,即@Component,所以它们代表的意思都是这个类已经被spring托管,我们可以直接在客户端按照类名小写获取对应的bean
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.自动装配

    已经在11中讲过了: Spring5(11) —— 使用注解实现自动装配.

5.作用域 / @Scope(“bean的作用域”)

    链接: Spring5(9) —— Bean的作用域.
    在学习springMVC之前,我们还是只需要使用singleton和prototype两个作用域即可,而 @Scope注解中的值就是这些作用域的名称

6.小结

xml与注解

  • xml更加万能,适用于一切场合,维护方便
  • 注解不能实现对象属性的引用,维护相对复杂

xml与注解的最佳实践/最好的配合使用方式

  • xml :只管理bean

  • 注解:只负责注入属性值

  • 我们在使用的过程中只需要注意一个问题:让注解生效,就要开启xml中对于注解的支持

        <!--扫描指定包,这个包里面的注解就会生效-->
        <context:component-scan base-package="com.thhh"/>
        <context:annotation-config/>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值