2 Spring IoC

POM

创建一个工程名为 spring-ioc-demo 的项目,pom.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>spring-ioc-demo</artifactId><version>1.0.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><spring.version>5.1.5.RELEASE</spring.version><lombok.version>1.16.20</lombok.version><junit.version>4.12</junit.version><log4j.version>1.2.17</log4j.version><slf4j.version>1.7.25</slf4j.version></properties><dependencies><!-- Spring Begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version><scope>test</scope></dependency><!-- Spring End --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><!-- lombok Begin --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version><scope>provided</scope></dependency><!-- lombok End --><!-- Log Begin --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jul-to-slf4j</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><!-- Log End --></dependencies><build><plugins><!-- Compiler 插件, 设定 JDK 版本 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><source>${java.version}</source><target>${java.version}</target><encoding>${project.build.sourceEncoding}</encoding><showWarnings>true</showWarnings></configuration></plugin></plugins></build></project>

核心包:主要增加了 org.springframework: spring-context 依赖

案例一,基于 xml 配置

创建 entity

@Data
public class Student {private int id;private String name;private String email;private String address;private Hobboy hobboy;
}
@Data
public class Hobboy {private String basketball;private String football;private String running;
}

创建 Spring 配置文件

在 src/main/resources 目录下创建 spring-context.xml 配置文件,从现在开始类的实例化工作交给 Spring 容器管理(IoC),配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><bean id="stu" class="com.example.spring.demo.entity.Student"><property name="id" value="10"></property><property name="name" value="vincent"></property><property name="email" value="601521821@qq.com"></property><property name="address" value="上海市、宝山区"></property><property name="hobboy" ref="hob"></property></bean><bean id="hob" class="com.example.spring.demo.entity.Hobboy"><property name="basketball" value="Nike"></property><property name="football" value="Adidas"></property><property name="running" value="5km"></property></bean></beans>

<bean/>:用于定义一个实例对象。一个实例对应一个 bean 元素。

id:该属性是 Bean 实例的唯一标识,程序通过 id 属性访问 Bean,Bean 与 Bean 间的依赖关系也是通过 id 属性关联的。

class:指定该 Bean 所属的类,注意这里只能是类,不能是接口

测试 Spring IoC

创建一个 GetBeanTest 测试类,测试对象是否能够通过 Spring 来创建,代码如下:

public class GetBeanTest {@Testpublic void testGetBean() {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");Student stu = (Student) applicationContext.getBean("stu");System.out.println(stu);}
}

测试结果:

Student(id=10, name=vincent, email=601521821@qq.com, address=上海市、宝山区, hobboy=Hobboy(basketball=Nike, football=Adidas, running=5km))

案例一,基于注解配置

改造 entity

@Data
@Component
public class Student {@Value("10")private int id;@Value("Vincrnt")private String name;@Value("601521821@qq.com")private String email;@Value("上海市宝山区")private String address;@Autowiredprivate Hobboy hobboy;
}

@Data
@Component
public class Hobboy {@Value("Nike")private String basketball;@Value("Adidas")private String football;@Value("5km")private String running;
}

改造 Spring 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><!-- 开启自动注解 --><context:annotation-config/><!-- 扫描全部的整个项目的包 --><context:component-scan base-package="com.example.spring.ioc.demo"/></beans>

改造测试类 Spring IoC

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class GetBeanTest {@Autowiredprivate Student student;@Testpublic void annotationStu(){System.out.println(student);}}

测试结果:

Student(id=10, name=vincent, email=601521821@qq.com, address=上海市、宝山区, hobboy=Hobboy(basketball=Nike, football=Adidas, running=5km))

案例二,基于 xml 配置

创建 StudentService 接口

public interface StudentService {public void sayHi();
}

创建 UserServiceImpl 实现类

public class StudentServiceImpl implements StudentService {public void sayHi() {System.out.println("Hello Spring IoC !!!");}
}

创建 Spring 配置文件

在 src/main/resources 目录下创建 spring-context.xml 配置文件,从现在开始类的实例化工作交给 Spring 容器管理(IoC),配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"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"><bean id="studentService" class="com.example.spring.demo.service.impl.StudentServiceImpl" />
</beans>

<bean />:用于定义一个实例对象。一个实例对应一个 bean 元素。

id:该属性是 Bean 实例的唯一标识,程序通过 id 属性访问 Bean,Bean 与 Bean 间的依赖关系也是通过 id 属性关联的。

class:指定该 Bean 所属的类,注意这里只能是类,不能是接口。

测试 Spring IoC

创建一个 GetBeanTest 测试类,测试对象是否能够通过 Spring 来创建,代码如下:

public class GetBeanTest {@Testpublic void sayHi() {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");StudentService studentServiceImpl = (StudentService) applicationContext.getBean("studentService");String sayHi = studentServiceImpl.sayHi();System.out.println(sayHi);}
}

测试结果

Hello Spring IoC !!!

案例二,基于注解配置

改造实现类,加上注解 @Service

@Service
public class StudentServiceImpl implements StudentService {@Overridepublic String sayHi() {return "Hello Spring IoC !!!";}
}

改造 Spring 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><!-- 开启自动注解 --><context:annotation-config/><!-- 扫描全部的整个项目的包 --><context:component-scan base-package="com.example.spring.ioc.demo"/></beans>

改造测试类 Spring IoC

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class GetBeanTest {@Autowiredprivate StudentService studentService;@Testpublic void annotationSayHi() {String sayHi = studentService.sayHi();System.out.println(sayHi);
}

测试结果:

Hello Spring IoC !!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/830642.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

中伟视界:矿山智能管控平台关键功能介绍,AI算法、告警通知、问题解决

矿山智能管控平台的关键功能介绍如下&#xff1a; 1.1. 主界面功能介绍 主界面分为六大区域&#xff0c;分别是设备列表、重点区域、功能区、告警列表、菜单区等&#xff0c;分别对应不同的功能和操作。 1.2. 平台功能 平台包含11条特色功能&#xff0c;分别为&#xff1a…

Open feign远程调用丢失请求头问题

由浏览器发送后台请求会携带cookie&#xff0c;在后台服务中会通过拦截器拦截到请求&#xff0c;并通过session来获取当前用户是否登录&#xff0c;即便httpsession是springSession实现分布式session共享&#xff0c;它通过对request和response和session的包装类实现了自己的se…

《Git---Windows Powershell提交信息中文乱码解决方案》

解释&#xff1a; Windows PowerShell中的Git乱码通常是因为字符编码不正确或Git配置不支持Windows系统的默认编码导致的。Git在处理文件时可能使用UTF-8编码&#xff0c;而Windows系统的命令行工具&#xff08;如PowerShell&#xff09;默认使用的是Windows-1252或GBK编码。 …

mysql 字符串去重

把“aa,bb,cc,dd,aa,bb,cc,dd” 字符串中重复的内容去除。 还是用截取的方式&#xff0c;一个一个截取&#xff0c;然后进行拼接&#xff0c;拼接前用find_in_set函数来判断&#xff0c;是否已经存在&#xff1f; 如果不存在&#xff0c;进行拼接&#xff0c;存在就忽略&#x…

场景文本检测识别学习 day06(Vi-Transformer论文精读)

Vi-Transformer论文精读 在NLP领域&#xff0c;基于注意力的Transformer模型使用的非常广泛&#xff0c;但是在计算机视觉领域&#xff0c;注意力更多是和CNN一起使用&#xff0c;或者是单纯将CNN的卷积替换成注意力&#xff0c;但是整体的CNN 架构没有发生改变VIT说明&#x…

Openstack: live-migration SRIOV的一个问题(1)

​去年分析的一个问题&#xff1a;Openstack: migration 虚拟机热迁移 失败的注意点。里面有很多未知答案的问题。最近再总结一下&#xff0c;可能会有几篇&#xff0c;算是一个系列。 在这两天又遇到&#xff0c;继续看了一下。找到了之前一直没有搞明白的一个问题&#xff1…

Jupyter Notebook 中使用虚拟环境的Python解释器

问题&#xff1a;创建虚拟环境&#xff0c;在pycharm中配置虚拟环境的Python解释器&#xff0c;然后在pycharm中打开ipynb&#xff0c;执行发现缺少包&#xff0c;但是虚拟环境中已经安装了 解决方式&#xff1a; 配置Jupyter Notebook 使用虚拟环境的Python解释器 1&#x…

手写代码题【基础篇】

一.如何实现一个深比较函数 首先&#xff0c;需要明确一点&#xff1a;HTML 是一种用于创建网页的标记语言&#xff0c;它并不适合用来实现复杂的逻辑功能&#xff0c;如深比较函数。深比较函数通常用于比较两个对象的值是否相等&#xff0c;包括它们的属性和嵌套对象。 如果…

JWT是什么?如何使用?

JWT是什么&#xff1f;如何使用&#xff1f; 前言什么是JWT&#xff1f;概念工作方式JWT的组成HeaderPayloadSignatrue 实战引入依赖自定义注解定义实体类定义一个JWT工具类业务校验并生成token定义拦截器配置拦截器定义接口方法并添加注解开始验证 使用场景注意事项 JWT与传统…

用大模型生成带文字的海报

本文代码讲整合在&#xff1a; GitHub - liangwq/Chatglm_lora_multi-gpu: chatglm多gpu用deepspeed和 这篇文章介绍如何利用VLMdiffusion模型来搭建一条文本生成海报的链路。搭建这条链路有两个应用&#xff1a;1.实际的业务中需要批量生产文字图海报可以用&#xff0c;2.可以…

【信息系统项目管理师知识点速记】范围管理:创建WBS

9.6 创建WBS 创建工作分解结构(WBS)是把项目可交付成果和项目工作分解成较小、更易于管理的组件的过程。其主要作用是为所要交付的内容提供架构。这一过程仅在项目的预定义点或仅开展一次。 9.6.1 输入 项目管理计划 范围管理计划:定义了如何根据项目范围说明书创建WBS。项…

C++学习第八课:函数定义使用和它的高级应用

C学习第八课&#xff1a;函数的高级使用 在C中&#xff0c;函数是封装一段代码的单元&#xff0c;使得代码更加模块化、重用性高&#xff0c;并且易于维护。本课我们将介绍如何使用函数组织代码&#xff0c;包括函数原型、定义、调用、参数传递、返回值、默认参数值、递归函数…

生成式人工智能(AIGC)教学解决方案

一、前言 近年来&#xff0c;伴随计算能力跃升和数据量指数级增长&#xff0c;以多模态巨型模型为典型代表的生成式AI技术&#xff0c;在全球范围内引起了广泛关注与热烈追捧。在教育、医疗、法律等众多专业领域&#xff0c;生成式人工智能技术的影响力日益凸显&#xff0c;尤…

基于SSM的美容院管理系统演示。Javaee项目。ssm项目。

演示视频&#xff1a; 基于SSM的美容院管理系统演示。Javaee项目。ssm项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;通过Spring SpringMvcMybatisVueLayuiElem…

C++11 设计模式5. 原型模式

什么是原型模式&#xff1f; 原型模式⼀种创建型设计模式&#xff0c;该模式的核⼼思想是基于现有的对象创建新的对象&#xff0c;⽽不是从头开始创建。在原型模式中&#xff0c;通常有⼀个原型对象&#xff0c;它被⽤作创建新对象的模板。新对象通过复制原型对象的属性和状态来…

24.什么是跨域?解决方案有哪些?

为什么会出现跨域问题 存在浏览器同源策略&#xff0c;所以才会有跨域问题。那么浏览器是出于何种原因会有跨域的限制呢。其实不难想到&#xff0c;跨域限制主要的目的就是为了用户的上网安全。 同源策略导致的跨域是浏览器单方面拒绝响应数据&#xff0c;服务器端是处理完毕…

opencv基础篇 ——(十)非真实感渲染

非真实感渲染&#xff08;Non-Photorealistic Rendering, NPR&#xff09;是指通过一系列图像处理技术&#xff0c;将真实感图像转换为具有特定艺术风格或视觉效果的图像&#xff0c;模拟绘画、素描、卡通等非现实主义表现手法。OpenCV 提供了一些内置函数来实现非真实感渲染&a…

2024最新的,免费的 ChatGPT 网站AI(八个)

ChatGPT是美国人工智能研究实验室OpenAI在2022年11月推出的一款人工智能技术驱动的语言模型应用。它基于GPT-3.5架构&#xff08;后续还有GPT-4架构的升级版&#xff09;构建&#xff0c;拥有强大的自然语言处理能力和上下文理解能力&#xff0c;能够参与多轮对话&#xff0c;为…

Python_GUI工具包 PyQt 与 Pyside6的介绍

Python_GUI工具包 PyQt 与 Pyside6的介绍 一、简介 在Python的GUI&#xff08;图形用户界面&#xff09;开发领域&#xff0c;PyQt和PySide6是两个非常重要的工具包。它们都基于Qt库&#xff0c;为Python开发者提供了丰富的GUI组件和强大的功能。当然Python也有一些其他的GU…

Halcon 检测物体定位点

文章目录 get_domain 返回所有输入图像的定义域作为一个区域add_channels 给区域增加灰度值find_shape_model 发现匹配模板find_shape_models 发现最佳模板示例 get_domain 返回所有输入图像的定义域作为一个区域 Halcon 中的区域 get_domain(Image : Domain : : ) Image : …