对于IOC的注入两种方式(注解和XML)

embedded/2024/9/24 20:22:30/

xml进行注入

在Spring IoC 容器中基于xml完成对Bean的装配,创建测试类,获取ProductController的实例,调用其save()方法,在控制台上打印Product的信息

spring-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="productController" class="com.yt.Controller.ProductController"></bean></beans>

ProductTest

@Testpublic void test2(){Product product = new Product(12, "111", "111", 123);
//这里是要根据target的相对路径来获取的相应的xml文件的的位置的ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-config.xml");ProductController productController = (ProductController) classPathXmlApplicationContext.getBean("productController");productController.save(product);}

注解注入

在Spring IoC 容器中基于注解(@Autowired)完成对Bean的装配,创建测试类,获取ProductController的实例,调用其save()方法,在控制台上打印Product的信息

AppConfig 

package com.yt.config;import org.springframework.context.annotation.ComponentScan;/*** @program: mybatis-operate* @description* @author: YangTao* @create: 2024-04-22 08:43**/@ComponentScan("com.yt")public class AppConfig {}

Product
package com.yt;/*** @program: mybatis-operate* @description* @author: YangTao* @create: 2024-04-22 08:25**/public class Product {private Integer id;private String name;private String subTitle;private float price;public Product(Integer id, String name, String subTitle, float price) {this.id = id;this.name = name;this.subTitle = subTitle;this.price = price;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSubTitle() {return subTitle;}public void setSubTitle(String subTitle) {this.subTitle = subTitle;}public float getPrice() {return price;}public void setPrice(float price) {this.price = price;}@Overridepublic String toString() {return "Product{" +"id=" + id +", name='" + name + '\'' +", subTitle='" + subTitle + '\'' +", price=" + price +'}';}}
Controller

package com.yt.Controller;import com.yt.Product;import com.yt.Service.ProductService;import org.springframework.stereotype.Controller;/*** @program: mybatis-operate* @description* @author: YangTao* @create: 2024-04-22 08:27**/@Controllerpublic class ProductController {private ProductService productService;public void save(Product product){System.out.println("hello");//            productService.save(product);// 调用业务层接口的保存方法}}

Service
package com.yt.Service;import com.yt.Product;import org.springframework.stereotype.Service;/*** @program: mybatis-operate* @description* @author: YangTao* @create: 2024-04-22 08:28**/public interface ProductService {public void save(Product product); // 保存商品操作}package com.yt.Service.ServiceImpl;import com.yt.Product;import com.yt.Service.ProductService;import org.springframework.stereotype.Service;/*** @program: mybatis-operate* @description* @author: YangTao* @create: 2024-04-22 08:28**/@Servicepublic class ProductServiceImpl implements ProductService {@Overridepublic void save(Product product) {System.out.println(product); // 实现接口中定义的方法}}

ProductTest 
public class ProductTest {@Testpublic void test1(){Product product = new Product(12, "111", "111", 123);AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);ProductController productController = (ProductController) annotationConfigApplicationContext.getBean(ProductController.class);productController.save(product);ProductService productService = (ProductService) annotationConfigApplicationContext.getBean(ProductService.class);productService.save(product);}}


http://www.ppmy.cn/embedded/19242.html

相关文章

Kotlin语法快速入门-函数(4)

Kotlin语法快速入门-函数&#xff08;4&#xff09; 文章目录 Kotlin语法快速入门-函数&#xff08;4&#xff09;四、函数1、函数定义2、infix关键字3、参数省略4、函数类型参数5、多参数--vararg 四、函数 1、函数定义 fun 函数名(参数: 类型) &#xff1a;返回值类型{//函…

Java-AQS的原理

文章目录 基本概述1. 设计思想2. 基本实现 一些关键词语以及常用术语&#xff0c;主要如下&#xff1a; 信号量(Semaphore): 是在多线程环境下使用的一种设施&#xff0c;是可以用来保证两个或多个关键代码段不被并发调用&#xff0c;也是作系统用来解决并发中的互斥和同步问题…

Android NDK开发 CMAKE 相关总结

预设变量含义介绍 工程结构组织&#xff1a; 代码目录 ├── CMakeLists.txt ├── a │ ├── CMakeLists.txt │ └── a.cpp └── b├── CMakeLists.txt├── b.cpp└── b.h路径相关&#xff1a; CMAKE_SOURCE_DIR&#xff1a;最顶层 CMakceLists.txt 所在…

如何在SpringBoot中集成MyBatis?

Spring Boot 中集成 MyBatis 的详细步骤及示例代码&#xff1a; 添加 MyBatis 依赖&#xff1a; 在 Maven 的 pom.xml 文件中添加 MyBatis 的依赖&#xff1a; <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boo…

14 Linux实操篇-进程管理

第十四章 Linux实操篇-进程管理&#xff08;重点&#xff09; 14.1 基本介绍 在 LINUX 中&#xff0c;每个执行的程序都称为一个进程。每一个进程都分配一个 ID 号(pid,进程号)。>windows > linux 每个进程都可能以两种方式存在的**。前台与后台**&#xff0c;所谓前台…

220V降3.3V恒压100mA-300mA非隔离WT5107

220V降3.3V恒压100mA-300mA非隔离WT5107 WT5107是一款高性能、高精度的非隔离降压开关电源恒压控制驱动芯片。它适用于220VAC或全电压范围输入电压的非隔离Buck、Buck-Boost拓扑结构&#xff0c;固定输出3.3V尤其适合小家电和白色家电等设备的电源。 此款芯片内部集成了高压M…

基于streamlit快速部署机器学习项目(Public URL)

基于streamlit的AIGC项目前端展示 1.Streamlit 简介与入门1.1 安装 Streamlit1.2 开发Streamlit应用程序1.3 启动并运行1.3.1 本地运行1.3.2 部署 现在LLM技术发展迅速&#xff0c;很多人在学习的时候&#xff0c;都想展示效果&#xff0c;并且想部署在服务器上&#xff0c;但是…

ubuntu如何运行python程序

打开LINUX UBUNTU操作系统。 找到左边的TERMINAL&#xff0c;打开窗口。 输入python&#xff0c;如果没有安装&#xff0c;就会提示需要安装。 如果已经安装好python3&#xff0c;直接输入&#xff0c;那么就会进入。 exit()就可以退出python3的模式。 创建一个文件&#xff0c…