SSM学习2:依赖注入、依赖自动装配、集合注入、加载properties文件

devtools/2024/9/24 14:21:30/

依赖注入

依赖注入方式
在这里插入图片描述

setter注入——引用类型

在这里插入图片描述

setter注入——简单类型

public class BookDaoImpl implements BookDao {public void setDatabaseName(String databaseName) {this.databaseName = databaseName;}public void setNum(int num) {this.num = num;}private String databaseName;private int num;public void save() {System.out.println("book dao save ..." + databaseName + "," + num);}
}

databaseNamenum两个属性提供可访问的set方法,然后在applicationContext.xml里进行配置

<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl"><property name="databaseName" value="mysql"/><property name="num" value="100"/></bean>
</beans>

运行时会自动把设置好的值注入进去
在这里插入图片描述

构造器注入——引用类型

BookServiceImpl.java

public class BookServiceImpl implements BookService {public BookServiceImpl(BookDao bookDao) {this.bookDao = bookDao;}private BookDao bookDao;public void save() {System.out.println("book service save ...");bookDao.save();}}

applicationContext.xml

<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl" /><bean id="bookService" class="com.itheima.service.impl.BookServiceImpl"><constructor-arg name="bookDao" ref="bookDao" /></bean>
</beans>

在这里插入图片描述

构造器注入——简单类型

BookDaoImpl.java

public class BookDaoImpl implements BookDao {public BookDaoImpl(String databaseName, int num) {this.databaseName = databaseName;this.num = num;}private String databaseName;private int num;public void save() {System.out.println("book dao save ..." + databaseName + "," + num);}
}

applicationContext.xml

<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl"><constructor-arg name="databaseName" value="mysql"/><constructor-arg name="num" value="100"/></bean><bean id="bookService" class="com.itheima.service.impl.BookServiceImpl"><constructor-arg name="bookDao" ref="bookDao"/></bean>
</beans>

自动装配

在这里插入图片描述
按类型

BookServiceImpl

public class BookServiceImpl implements BookService{private BookDao bookDao;public void setBookDao(BookDao bookDao) {this.bookDao = bookDao;}public void save() {System.out.println("book service save ...");bookDao.save();}
}

applicationContext.xml

<?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 class="com.itheima.dao.impl.BookDaoImpl"/><!--autowire属性:开启自动装配,通常使用按类型装配--><bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/></beans>

使用这种方式:
1、必须提供对应的set方法
2、必须有要装配类的对应的bean配置

 <bean class="com.itheima.dao.impl.BookDaoImpl"/>

3、按类型装配时,对应的bean需要是唯一的
在这里插入图片描述

按名称

<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/><bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byName"/>
</beans>

在这里插入图片描述
注意点
在这里插入图片描述

集合注入

BookDaoImpl

public class BookDaoImpl implements BookDao {private int[] array;private List<String> list;private Set<String> set;private Map<String,String> map;private Properties properties;public void setArray(int[] array) {this.array = array;}public void setList(List<String> list) {this.list = list;}public void setSet(Set<String> set) {this.set = set;}public void setMap(Map<String, String> map) {this.map = map;}public void setProperties(Properties properties) {this.properties = properties;}public void save() {System.out.println("book dao save ...");System.out.println("遍历数组:" + Arrays.toString(array));System.out.println("遍历List" + list);System.out.println("遍历Set" + set);System.out.println("遍历Map" + map);System.out.println("遍历Properties" + properties);}
}

applicationContext.xml

<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl"><!--数组注入--><property name="array"><array><value>100</value><value>200</value><value>300</value></array></property><!--list集合注入--><property name="list"><list><value>itcast</value><value>itheima</value><value>boxuegu</value><value>chuanzhihui</value></list></property><!--set集合注入--><property name="set"><set><value>itcast</value><value>itheima</value><value>boxuegu</value><value>boxuegu</value></set></property><!--map集合注入--><property name="map"><map><entry key="country" value="china"/><entry key="province" value="henan"/><entry key="city" value="kaifeng"/></map></property><!--Properties注入--><property name="properties"><props><prop key="country">china</prop><prop key="province">henan</prop><prop key="city">kaifeng</prop></props></property></bean>
</beans>

名称要一一对应
在这里插入图片描述

加载properties文件

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=root

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--    1.开启context命名空间--><!--    2.使用context空间加载properties文件--><!--classpath*:*.properties  :  设置加载当前工程类路径和当前工程所依赖的所有jar包中的所有properties文件--><context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/><!--    3.使用属性占位符${}读取properties文件中的属性--><!--    说明:idea自动识别${}加载的属性值,需要手工点击才可以查阅原始书写格式--><bean class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean>
</beans>

加载properties文件
在这里插入图片描述


http://www.ppmy.cn/devtools/56520.html

相关文章

[图解]SysML和EA建模住宅安全系统-01-包图

1 00:00:01,400 --> 00:00:04,870 得到这个之后&#xff0c;我们就来画我们的包图了 2 00:00:05,350 --> 00:00:07,940 我们来看包图的内容 3 00:00:09,750 --> 00:00:12,430 名字是这个&#xff0c;模型组织 4 00:00:13,820 --> 00:00:20,570 然后上面&#xf…

如何在 Linux 中后台运行进程?

一、后台进程 在后台运行进程是 Linux 系统中的常见要求。在后台运行进程允许您在进程独立运行时继续使用终端或执行其他命令。这对于长时间运行的任务或当您想要同时执行多个命令时特别有用。 在深入研究各种方法之前&#xff0c;让我们先了解一下什么是后台进程。在 Linux 中…

Spring Boot中的国际化配置

Spring Boot中的国际化配置 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01;今天我们将探讨如何在Spring Boot应用中实现国际化配置&#xff0c;使得应用能够轻松…

等级保护 | 如何完成等保的建设整改

等级保护整改是等保基本建设的一个阶段。为了能成功通过等级测评&#xff0c;企业要根据等级保护建设要求&#xff0c;对信息和信息系统进行网络安全升级&#xff0c;对定级对象当前不满足要求的进行建设整改&#xff0c;包括技术层面的整改&#xff0c;也包括管理方面的整改。…

Java面试题:解释常见的HTTP状态码及其含义

HTTP状态码是由服务器返回给客户端的三位数字&#xff0c;用于表示HTTP请求的结果状态。以下是一些常见的HTTP状态码及其含义&#xff1a; 1xx: 信息响应 102 Processing (WebDAV)&#xff1a;表示服务器已收到并正在处理请求&#xff0c;但尚未有响应可用。 2xx: 成功 203…

JAVA学习笔记-JAVA基础语法-DAY19-File类、递归

第一章 File类 1.1 概述 java.io.File 类是文件和目录路径名的抽象表示&#xff0c;主要用于文件和目录的创建、查找和删除等操作。 1.2 构造方法 public File(String pathname) &#xff1a;通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。public File(St…

k8s的daemonset里判断CRD资源里定义的NodeSelector是否包含本节点

要实现一个 Kubernetes 控制器&#xff0c;它在 DaemonSet 容器中运行&#xff0c;并侦听自定义资源创建事件&#xff0c;然后基于自定义资源的 spec.nodeSelector 判断当前节点是否匹配&#xff0c;你可以根据以下步骤实现&#xff1a; 第 1 步&#xff1a;获取当前节点的标签…

vue封装接口

找到src下的api 在里面创建一个名为Api.js的文件 1&#xff0c;在里面写入接口 import axios from axios//先引入axiosexport async function menusIndex(data) {//这个data代表传过来的值return axios({url: 要请求的地址,method: POST,data,}) } 2&#xff0c;在页面中 …