首页 > 其他分享 >Spring自动装配

Spring自动装配

时间:2024-08-15 17:27:11浏览次数:8  
标签:装配 xml Spring 配置 list 自动 注解 class

Spring自动装配

  1. 手动装配实现属性注入

<bean id="studentDao" class="com.xz.dao.impl.StudentDaoImpl"></bean>

<bean id="studentService" class="com.xz.service.impl.StudentServiceImpl">

      <!--手动装配:设值注入,将studentDao对象注入给service对象的dao属性 -->

      <property name="studentDao" ref="studentDao"></property>

</bean>

  缺点:当维护bean组件或bean属性增加时,需要大量工作量完成配置。

  1. 自动装配实现属性注入
  1. 基于xml配置实现自动装配
  2. 基于注解+xml配置实现自动装配
  3. 基于注解+配置类实现自动装配

  1. 基于xml配置实现自动装配

   3.1 在bean标签上加上autowire自动装配属性

  <bean id="studentDao" class="com.xz.dao.impl.StudentDaoImpl"></bean>

  <bean id="studentService" class="com.xz.service.impl.StudentServiceImpl"

         autowire="byName">

  </bean>

   3.2  autowire属性值

属性值

描述

byName

根据名称自动装配。

要求:对象id标识名和对象属性的set方法名相同。

byType

根据类型自动装配

要求:对象的class类型和对象属性的类型相同。

注意:如果spring容器中有多个与对象属性相同的类型的bean,会报错。

constructor

类似于byType,区别在于走构造方法。

no

不自动装配。必须手动装配进行配置

default

装配方式和全局自动装配default-autowire的值一致。

如果autowire和default-autowrie都为default,那么就是不自动装配

  3.3 全局的自动装配

<beans default-autowire="byName"/> 适用于所有的bean

  1. 基于注解+XML配置管理 Bean实现方式
    1. Spring管理对象的注解,等价于<bean/>

注解

描述

@Component

创建对象注解,没有语义

默认id标识名:类名首字母小写

自定义id标识名:@Service("studentService")

@Service

放在service层类上注解

@Repository

放在dao层类上注解

@Controller

放在控制层Controller类上注解

    1. 自动注入注解,等价于autowire属性

注解

描述

@Resource

Jdk提供注解,不需要提供set方法。

先根据byName进行注入,如果没有name一样的,

再根据byType进行注入。

@Resource注解属于JDK扩展包,所以不在JDK当中,需要额外引入以下依赖:

<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>2.1.1</version>
</dependency>

@Autowired

Spring提供注解,不需要提供set方法。

根据byType进行注入。

如果想根据名称装配,需要配合@Qualifier注解一起用。

@Qualifier(value = "名称") 根据指定的名称作为 bean 的id进行匹配注入

    1. 实现步骤
  1. Service

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    //@Resource
    StudentDao studentDao;

    @Override
    public List<Student> getStudents() {
        return studentDao.selectAll();
    }
}

  1. Dao

@Repository
public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> selectAll() {
        List<Student> list=new ArrayList<>();
        list.add(new Student("1001","小张",18));
        list.add(new Student("1002","小李",38));
        return list;
    }
}

  1. applicationContext.xml 配置文件

<!--开启注解扫描-->
<context:component-scan base-package="com.bdqn.service.impl,com.bdqn.dao.impl"/>

  1. 测试

/*基于注解+xml配置*/
@Test
public void test01(){
    ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    StudentService studentService=ac.getBean("studentServiceImpl",StudentService.class);
    List<Student> list=studentService.getStudents();
    System.out.println(list);
}

  1. 基于注解+配置类方式管理 Bean实现自动装配
  1. 相关注解

注解

描述

@Configuration

指定一个类为配置类,可以添加配置注解,替代配置xml文件

@ComponentScan(basePackages = {"包","包"})

开启注解扫描,替代<context:component-scan标签实现注解扫描

@PropertySource("classpath:配置文件地址")

加载读取配置文件,替代 <context:property-placeholder标签

  1. 实现步骤
  1. Service

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    //@Resource
    StudentDao studentDao;

    @Override
    public List<Student> getStudents() {
        return studentDao.selectAll();
    }
}

  1. Dao

@Repository
public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> selectAll() {
        List<Student> list=new ArrayList<>();
        list.add(new Student("1001","小张",18));
        list.add(new Student("1002","小李",38));
        return list;
    }
}

  1. 创建一个SpringConfig.java的java配置类,替换掉Spring的配置文件

      注意:删除applicationContext.xml配置文件

//标注当前类是配置类,替代applicationContext.xml
@Configuration
//使用@ComponentScan注解,可以配置扫描包,替代<context:component-scan标签
@ComponentScan(basePackages = {"com.bdqn.dao.impl","com.bdqn.service.impl"})
public class MyConfiguration {

}

  1. 测试

/*基于注解+配置*/
@Test
public void test01(){
    ApplicationContext ac=new AnnotationConfigApplicationContext(MyConfiguration.class);
    StudentService studentService=ac.getBean("studentServiceImpl",StudentService.class);
    List<Student> list=studentService.getStudents();
    System.out.println(list);
}

6. XML、注解+XML、完全注解 配置类 方式管理 Bean的区别

XMLIoC方式问题总结:

  1. 注入的属性必须添加setter方法、代码结构乱!

  2. 配置文件和Java代码分离、编写不是很方便!

  3. XML配置文件解析效率低

注解+XML IoC方式问题总结

    1. 自定义类可以使用注解方式,但是第三方依赖的类依然使用XML方式!

    2. XML格式解析效率低!

Spring 完全注解配置(Fully Annotation-based Configuration)

   是指通过 Java配置类 代码来配置 Spring 应用程序,

   使用注解来替代原本在 XML 配置文件中的配置。

   相对于 XML 配置,完全注解配置具有更强的类型安全性和更好的可读性。

7. Spring5+Test5搭建测试环境

   1. 整合测试环境作用

       好处1:不需要自己创建IOC容器对象了

       好处2:任何需要的bean都可以在测试类中直接享受自动装配

2. 导入相关依赖

<!--junit5测试-->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>

<!--Spring-test-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>6.0.6</version>
</dependency>

  1. 整合测试注解使用

/**
 * Spring5+Test5搭建测试环境
 */
//@SpringJUnitConfig(locations = {"applicationContext.xml"}) //指定配置文件
@SpringJUnitConfig(value = {MyConfiguration.class}) //指定配置类
public class TestDemo02 {
    @Autowired
    StudentService  studentService;
    
    @Test
    public void test01(){
        List<Student> list=studentService.getStudents();
        System.out.println(list);
    }
}

标签:装配,xml,Spring,配置,list,自动,注解,class
From: https://blog.csdn.net/KLKing2/article/details/141227572

相关文章

  • Spring中接口注入和实现类注入的区别
    一、依赖注入的背景在Spring框架中,依赖注入(DependencyInjection,DI)是一种通过外部控制来为类提供其依赖对象的机制。Spring通过IoC容器管理这些依赖,减少了组件之间的耦合度,使得代码更加灵活和易于测试。二、接口注入1.定义接口注入是指在代码中依赖的是接口类型,而不是接口......
  • Spring使用实现类注入为什么会导致高耦合度(举例)
    场景描述假设我们要开发一个日志记录器组件,记录日志的方式可能有多种实现:控制台输出、文件输出、甚至是发送到远程服务器。为了实现这个功能,我们可以定义一个Logger接口来抽象日志记录功能,然后根据不同的需求创建不同的实现类。1.接口注入的实现方式首先,我们定义一个Logger......
  • SpringBoot修改内置tomcat版本的操作步骤
    一:由于Tomcat高危漏洞影响,本文介绍了如何查询和修改Springboot内嵌的Tomcat版本,包括通过POM文件或mvnrepository查询版本,以及通过添加properties配置更改版本。此外,还提到了遇到缺少tomcat-juli依赖时的解决办法。最近Tomcat爆出高危漏洞,基本影响所有的Tomcat版本,故需要对sprin......
  • AC自动机
    AC自动机AC自动机是以\(Trie\)的结构为基础,结合\(KMP\)的思想建立的自动机,用于解决多模式串(作为子串的串)匹配等任务。建\(tire\)树,正常操作即可建\(fail\)树,如果当前节点失配,可以通过跳\(fail\)快速转到一个可能有答案的位置,相当于\(kmp\)但是在树上考虑所有模式串......
  • SpringBoot优雅的封装不同研发环境下(环境隔离)RocketMq自动ack和手动ack
    1.RocketMq的maven依赖版本:<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>2.3.0</version></dependenc......
  • Java面试题学习(Spring & SpringBoot)
    1.Java基础2.Spring&SpringBoot(正在浏览)目录一、Spring1.谈谈你对Spring的理解?/什么是Spring?2.Spring有什么特点?3.Spring框架中都用到了哪些设计模式?二、SpringIOC4.什么是SpringIOC?什么是SpringIOC容器?有什么作用?5.SpringIOC的实现机制是什么?6.什么是S......
  • 自学[vue+SpringCloud]-011-新建SpringCloud工程demo
    文章目录前言一、新建demo1.新建Maven项目2.填写项目信息二、初始化文件1.父工程的pom.xml2.子工程bztc-study01的pom.xml3.子工程的application.properties4.子工程的启动类三、启动总结前言新建SpringCloud工程demo,让工程能够启动起来。一、新建demo1.......
  • 基于Java Springboot音乐播放器系统
    一、作品包含源码+数据库+设计文档万字+PPT+全套环境和工具资源+部署教程二、项目技术前端技术:Html、Css、Js、Vue、Element-ui数据库:MySQL后端技术:Java、SpringBoot、MyBatis三、运行环境开发工具:IDEA/eclipse数据库:MySQL5.7数据库管理工具:Navicat10以上版本环境......
  • 基于Java Springboot北京医疗企业固定资产管理系统
    一、作品包含源码+数据库+设计文档万字+PPT+全套环境和工具资源+部署教程二、项目技术前端技术:Html、Css、Js、Vue、Element-ui数据库:MySQL后端技术:Java、SpringBoot、MyBatis三、运行环境开发工具:IDEA/eclipse数据库:MySQL5.7数据库管理工具:Navicat10以上版本环境......
  • 基于Java Springboot畅游游戏销售平台
    一、作品包含源码+数据库+设计文档万字+PPT+全套环境和工具资源+部署教程二、项目技术前端技术:Html、Css、Js、Vue、Element-ui数据库:MySQL后端技术:Java、SpringBoot、MyBatis三、运行环境开发工具:IDEA/eclipse数据库:MySQL5.7数据库管理工具:Navicat10以上版本环境......