Spring IOC相关注解运用——上篇

这篇具有很好参考价值的文章主要介绍了Spring IOC相关注解运用——上篇。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

前言

一、@Component

二、@Repository、@Service、@Controller

三、@Scope

四、@Autowired

五、@Qualifier

六、@Value

1. 直接设置固定的属性值

2. 获取配置文件中的属性值

3. 测试结果

往期专栏&文章相关导读 

1. Maven系列专栏文章

2. Mybatis系列专栏文章

3. Spring系列专栏文章


前言

        注解配置和xml配置对于Spring的IOC要实现的功能都是一样的,只是配置的形式不一样。
准备工作:

  1. 创建一个新的Spring项目。
  2. 编写pojo,dao,service类。
  3. 编写空的配置文件,如果想让该文件支持注解,需要在bean.xml添加新的约束:
<?xml version="1.0" encoding="UTF-8" ?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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">

    <context:component-scan base-package="com.example"/>
    <context:property-placeholder location="db.properties"/>

</beans>

一、@Component

@Component可以代替bean标签

  1. 作用:用于创建对象,放入Spring容器,相当于 <bean id="" class="">
  2. 位置:类上方
  3. 注意:@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
// 此时bean的id为studentDaoImpl
@Component
public class StudentDaoImpl implements StudentDao{
  public Student findById(int id) {
    // 模拟根据id查询学生
    return new Student(1,"程序员","北京");
 }
// 此时bean的id为studentDao
@Component("studentDao")
public class StudentDaoImpl implements StudentDao{
  public Student findById(int id) {
    // 模拟根据id查询学生
    return new Student(1,"程序员","北京");
 }
}

二、@Repository、@Service、@Controller

作用:这三个注解和@Component的作用一样,使用它们是为了区分该类属于什么层。
位置:

  1. @Repository用于Dao层
  2. @Service用于Service层
  3. @Controller用于Controller层
@Repository
public class StudentDaoImpl implements StudentDao{}
@Service
public class StudentService {}

三、@Scope

作用:指定bean的创建策略
位置:类上方
取值:singleton prototype request session globalsession 

@Service
@Scope("singleton")
public class StudentService {}

测试一下: 

    @Test
    public void t1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
        System.out.println(studentDao);
        StudentService service1 = (StudentService) ac.getBean("studentService");
        System.out.println(service1.hashCode());
        StudentService service2 = ac.getBean("studentService",StudentService.class);
        System.out.println(service2.hashCode());
    }

Spring IOC相关注解运用——上篇

 OK,确实可以

四、@Autowired

作用:从容器中查找符合属性类型的对象自动注入属性中。用于代替 <bean> 中的依赖注入配置。
位置:属性上方、setter方法上方、构造方法上方。
注意:@Autowired 写在属性上方进行依赖注入时,可以省略setter方法。

@Component
public class StudentService {
  @Autowired
  private StudentDao studentDao;
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}

// 测试方法
@Test
public void t2(){
  ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  StudentService studentService = (StudentService) ac.getBean("studentService");
  System.out.println(studentService.findStudentById(1));
}

测试结果:

Spring IOC相关注解运用——上篇

OK,也是可以的 

五、@Qualifier

作用:在按照类型注入对象的基础上,再按照bean的id注入。
位置:属性上方
注意:@Qualifier必须和@Autowired一起使用。

如下 

@Component
public class StudentService {
  @Autowired
  @Qualifier("studentDaoImpl2")
  private StudentDao studentDao;
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}

六、@Value

作用:注入String类型和基本数据类型的属性值。
位置:属性上方

以下说明一下用法:

1. 直接设置固定的属性值

    @Value("1")
    private int count;

    @Value("hello")
    private String str;

2. 获取配置文件中的属性值

编写配置文件db.properties

jdbc.username=root
jdbc.password=123456

spring核心配置文件(bean.xml)扫描配置文件

<context:property-placeholder location="db.properties"/>

注入配置文件中的属性值

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

3. 测试结果

测试方法

    // 测试注解Value
    @Test
    public void t3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentService service = ac.getBean("studentService",StudentService.class);
        System.out.println(service);
    }

运行结果

Spring IOC相关注解运用——上篇

        OK,应该和上面设置的值一样,说明可以使用,本篇就介绍到这几个注解了,下篇会介绍完接下来的注解。

往期专栏&文章相关导读 

     大家如果对于本期内容有什么不了解的话也可以去看看往期的内容,下面列出了博主往期精心制作的Maven,Mybatis等专栏系列文章,走过路过不要错过哎!如果对您有所帮助的话就点点赞,收藏一下啪。其中Spring专栏有些正在更,所以无法查看,但是当博主全部更完之后就可以看啦。文章来源地址https://www.toymoban.com/news/detail-433863.html

1. Maven系列专栏文章

Maven系列专栏 Maven工程开发
Maven聚合开发【实例详解---5555字】

2. Mybatis系列专栏文章

Mybatis系列专栏 MyBatis入门配置
Mybatis入门案例【超详细】
MyBatis配置文件 —— 相关标签详解
Mybatis模糊查询——三种定义参数方法和聚合查询、主键回填
Mybatis动态SQL查询 --(附实战案例--8888个字--88质量分)
Mybatis分页查询——四种传参方式
Mybatis一级缓存和二级缓存(带测试方法)
Mybatis分解式查询
Mybatis关联查询【附实战案例】
MyBatis注解开发---实现增删查改和动态SQL
MyBatis注解开发---实现自定义映射关系和关联查询

3. Spring系列专栏文章

Spring系列专栏 Spring IOC 入门简介【自定义容器实例】
IOC使用Spring实现附实例详解
Spring IOC之对象的创建方式、策略及销毁时机和生命周期且获取方式
Spring DI简介及依赖注入方式和依赖注入类型
Spring IOC相关注解运用——上篇
Spring IOC相关注解运用——下篇
Spring AOP简介及相关案例
注解、原生Spring、SchemaBased三种方式实现AOP【附详细案例】
Spring事务简介及相关案例
Spring 事务管理方案和事务管理器及事务控制的API
Spring 事务的相关配置、传播行为、隔离级别及注解配置声明式事务

到了这里,关于Spring IOC相关注解运用——上篇的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 129.【Spring 注解_IOC】

    (1).无注解注入方式 在pom文件中加入spring-context依赖: xml文件和注解的包 定义一个实体类 在resource目录下的beans.xml配置文件中通过 bean /bean标签注入类实例 获取容器中通过配置文件注入的实例对象 (2).注解注入方式 1.MyConfig.java 2.Main.java (1).无注解扫描方式 只要我们加上 @Config

    2024年02月12日
    浏览(27)
  • 【Spring6】| Spring IoC注解式开发

    目录 一:Spring IoC注解式开发 1. 回顾注解 2. 声明Bean的四个注解 3. Spring注解的使用 4. 选择性实例化Bean 5. 负责注入的注解(重点) 5.1 @Value 5.2 @Autowired与@Qualifier 5.3 @Resource 6. 全注解式开发 注解的存在主要是为了简化XML的配置 ,Spring6倡导全注解开发。 我们来回顾一下:

    2023年04月12日
    浏览(35)
  • spring 详解五 IOC(注解开发)

    注解开发已经成为开发的主流选择,例如springboot都是基于注解开发使用的spring,所以学习注解开发是很有必要的,基本Bean注解, 主要是使用注解的方式替代原有xml的bean 标签及其标签属性的配置 XML 配置 注解 描述 bean id=“” class=“” @Component 被该注解标识的类,会在指定扫

    2024年02月13日
    浏览(26)
  • Spring IOC @Configuration注解分析

    在使用SpringBoot开发时,最常用的注解有@Component、@Service、@Controller、@Configuration等。当类使用这些注解标记时,类会被Spring IOC容器管理,包括创建,填充属性和实例化。 但是Spring容器如何发现并将这些类放到容器进行管理呢? 今天这篇博客主要分析Spring如何处理@Configuratio

    2024年02月08日
    浏览(28)
  • Spring学习笔记之Spring IoC注解式开发

    注解的存在主要是为了简化XML的配置。Spring6倡导全注解开发 注解怎么定义,注解中的属性怎么定义? 注解怎么使用 通过反射机制怎么读取注解 注解怎么定义,注解中的属性怎么定义? 以上是自定义了一个注解:Component 该注解上面修饰的注解包括:Target注解和Retention注解,

    2024年02月12日
    浏览(32)
  • 11Spring IoC注解式开发(下)(负责注入的注解/全注解开发)

    负责注入的注解,常见的包括四个: @Value @Autowired @Qualifier @Resource 当属性的类型是简单类型时,可以使用@Value注解进行注入。 @Value注解可以出现在属性上、setter方法上、以及构造方法的形参上, 方便起见,一般直接作用在属性上. 配置文件开启包扫描: 测试程序: 三种方法都可

    2024年01月16日
    浏览(34)
  • 【Spring篇】IOC/DI注解开发

    🍓系列专栏:Spring系列专栏 🍉个人主页:个人主页 目录 一、IOC/DI注解开发 1.注解开发定义bean  2.纯注解开发模式 1.思路分析 2.实现步骤 3.注解开发bean作用范围与生命周期管理 1.环境准备 2.Bean的作用范围 3.Bean的生命周期 4.注解开发依赖注入 1.环境准备 2.注解实现按照类型注入

    2024年02月03日
    浏览(62)
  • Spring Boot中最常用注解的使用方式(上篇)

    摘要:本文将详细介绍Spring Boot中最常用的注解的使用方式,并通过代码示例加以说明。通过学习这些注解,读者将能够更好地理解和运用Spring Boot框架,构建高效的企业级应用。 1.@RequestMapping @RequestMapping :将一个HTTP请求映射到对应的控制器方法上。可以用于类和方法级别。

    2024年02月07日
    浏览(35)
  • javaee spring 用注解的方式实现ioc

    spring核心依赖 spring配置文件

    2024年02月10日
    浏览(35)
  • IDEA项目实践——Spring框架简介,以及IOC注解

    IDEA创建项目的操作步骤以及在虚拟机里面创建Scala的项目简单介绍 IDEA项目实践——创建Java项目以及创建Maven项目案例、使用数据库连接池创建项目简介 IDEWA项目实践——mybatis的一些基本原理以及案例 IDEA项目实践——动态SQL、关系映射、注解开发 文章目录 第1章 Spring 概述

    2024年02月14日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包