当前位置: 首页 > news >正文

Spring设值注入

设值注入(Setter Injection)是Spring框架中依赖注入的一种方式,通过Setter方法将依赖对象注入到目标对象中。设值注入在对象创建后,通过调用Setter方法完成依赖注入。

设值注入的优点

  1. 灵活性:设值注入允许在对象创建后再设置依赖,提供了更大的灵活性。
  2. 可选依赖:可以选择性地注入某些依赖,而不是强制要求所有依赖在对象创建时就提供。
  3. 易于理解:Setter方法通常比较直观,易于理解和使用。

XML配置方式的设值注入

示例代码

以下是一个使用XML配置方式进行设值注入的示例:

XML配置文件

配置文件applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="myBean" class="com.example.MyBean"/><bean id="myService" class="com.example.MyService"><property name="myBean" ref="myBean"/></bean>
</beans>
Java代码
public class MyBean {public void doSomething() {System.out.println("Doing something...");}
}public class MyService {private MyBean myBean;public void setMyBean(MyBean myBean) {this.myBean = myBean;}public void performAction() {myBean.doSomething();}
}public class Main {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");MyService myService = context.getBean(MyService.class);myService.performAction();}
}

在这个示例中,MyService类通过Setter方法注入依赖MyBean。在XML配置文件中,通过<property>标签指定Setter方法的参数。

注解方式的设值注入

示例代码

以下是一个使用注解方式进行设值注入的示例:

Java代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Component
public class MyBean {public void doSomething() {System.out.println("Doing something...");}
}@Component
public class MyService {private MyBean myBean;@Autowiredpublic void setMyBean(MyBean myBean) {this.myBean = myBean;}public void performAction() {myBean.doSomething();}
}@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}public class Main {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);MyService myService = context.getBean(MyService.class);myService.performAction();}
}

在这个示例中,MyService类通过Setter方法注入依赖MyBean,并使用@Autowired注解标注Setter方法。AppConfig类是一个配置类,使用@ComponentScan注解扫描指定包中的组件。

总结

设值注入是Spring框架中依赖注入的一种方式,通过Setter方法将依赖对象注入到目标对象中。设值注入在对象创建后,通过调用Setter方法完成依赖注入。设值注入可以通过XML配置方式或注解方式实现,具体选择哪种方式取决于项目的需求和开发团队的偏好。设值注入提供了更大的灵活性和可选依赖的能力,使得对象的依赖关系更加灵活和可控。

http://www.lryc.cn/news/477012.html

相关文章:

  • Spring整合Mybatis过程
  • 常见HR问题篇
  • 调用数据集mnist(下载+调用全攻略)
  • 【基础语法】Java Scanner hasNext() 和 hasNextLine() 的区别
  • react使用Fullcalendar 实战用法
  • 优秀项目经理必知的10款项目管理软件推荐
  • 植物神经紊乱不用怕,这些维生素来帮你!
  • NRF52832学习笔记(41)——添加串口库libuarte
  • Moore Perf System 1.1版本
  • SpringBoot+Shirp的权限管理
  • OpenCV图像基础
  • 基于MATLAB的图像拼接技术
  • ComfyUI 快速入门(环境搭建)
  • 将HTML项目上传至Gitee仓库(详细教程)
  • 如何应对Oracle SQL语句的数据去重问题,应该考虑哪几个方面?
  • 论负载均衡技术在Web系统中的应用论文
  • NumPy 数据类型
  • JavaScript——(4)
  • 每日一练 | DHCP Relay(DHCP 中继)
  • `psdparse`:解锁Photoshop PSD文件的Python密钥
  • 考研要求掌握的C语言程度(插入排序)
  • mybatis源码解析-sql执行流程
  • Golang | Leetcode Golang题解之第538题把二叉搜索树转换为累加树
  • 【linux】HTTPS 协议原理
  • 安利一款开源企业级的报表系统SpringReport
  • 数据安全-接口数据混合加密笔记
  • JeecgBoot入门
  • 用 Vue.js 打造炫酷的动态数字画廊:展示学生作品的创意之旅
  • 【YOLO学习】YOLOv8改进举例
  • 文心一言 VS 讯飞星火 VS chatgpt (383)-- 算法导论24.5 3题