springDi依赖注入
setter注入
要想用Spring的形式完成对象的调用非常简单,只需要在类里面定义好需要使用到的实例对象,然后提供setter方法,编辑定义好spring配置文件中的信息property,并且给property定义两个属性,一个name属性指向实例化的具体属性名称,ref属性指向参照哪一个bean即可,但是还有一种情况就是需要用到基本数据类型与字符串类型的情况,其实也很简单,具体处理如下:
- 在类中定义两个属性,提供set方法
public class Diimpl implements Di{
private int number;
private String databasename;
public void setNumber(int number) {
this.number = number;
}
public void setDatabasename(String databasename) {
this.databasename = databasename;
}
public void say() {
System.out.println("hello,dao"+number+databasename);
IOCimpl.say();
}
}
2.编写String配置文件,通过value对int属性和String属性赋值
<bean id ="DIimpl" class="com.xiaohe.service.Diimpl">
<property name="IOCimpl" ref="IOCimpl"/>
<property name="databasename" value="数据库名称"/>
<property name="number" value="100"/>
</bean>
3.测试
public class Application {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplactionContext.xml");
// IOCimpl test = (IOCimpl) applicationContext.getBean("IOCimpl");
// test.say();
Diimpl bean = (Diimpl) applicationContext.getBean("DIimpl");
bean.say();
}
}
使用构造器注入
使用构造器注入的方法与setter方法注入略有区别,具体实现流程如下:
第一步,定义实例对象并且生成构造方法
public class Diimpl implements Di{
private IOCimpl IOCimpl;
private int number;
private String databasename;
public Diimpl(com.xiaohe.dao.IOCimpl IOCimpl, int number, String databasename) {
this.IOCimpl = IOCimpl;
this.number = number;
this.databasename = databasename;
}
public void say() {
System.out.println("hello,dao"+number+databasename);
IOCimpl.say();
}
}
第二步,编写spring配置文件,注意:这里不能使用property来传递参数,而需要使用constructor-arg来传递
<bean id="diimpl" class="com.xiaohe.service.Diimpl">
<constructor-arg name="IOCimpl" ref="IOCimpl"/>
<constructor-arg name="databasename" value="database"/>
<constructor-arg name="number" value="10"/>
</bean>
第三步,测试
public class Application {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplactionContext.xml");
Diimpl bean = (Diimpl) applicationContext.getBean("diimpl");
bean.say();
}
}
两种注入方式一般更推荐使用setter注入
依赖自动装配
虽然setter注入和构造器注入相对来说比较简单,但如果用的多了还会觉得麻烦,spring就提供了一种自动装配的方式,自动注入,ioc容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程称为自动装配。
自动装配就是指 Spring 容器在不使用
实现:配置
第一种、byname 根据 bean 的 id,name属性 自动装配,如果使用自动装配的bean没有找到需要用到的实例的bean,则会报错,由于这种方式会导致代码耦合度变高,所以一般不推荐使用
第二种、bytype 根据bean的类型自动装配(class参数指向的实体类)
第三种、constructor:利用构造方法进行装配,并且构造函数的参数通过byType进行装配。(不推荐)
第四种、no 不使用自动装配
注意:
- 使用自动装配的时候必须提供set方法,否则会报异常,
- 自动装配不能用于简单类型的操作
- 自动装配优先级低于setter注入及构造器注入,同时出现时自动装配失效
public class Diimpl implements Di{
private IOCimpl IOCimpl;
public void setIOCimpl(com.xiaohe.dao.IOCimpl IOCimpl) {
this.IOCimpl = IOCimpl;
}
public void say() {
System.out.println("hello,dao");
IOCimpl.say();
}
}
<bean id ="IOCimpl" class="com.xiaohe.dao.IOCimpl"></bean>
<bean id="diimpl" class="com.xiaohe.service.Diimpl" autowire="byType"/>
集合与数组注入
集合与数组注入的方式有一定的相似性,都需要配置
<property name = "array">
<!--这里的name属性指的是实体类里面定义的属性名称-->
<array>
<value>100</value>
<value>200</value>
<value>300</value>
</array>
</property>
其余的集合注入方式与数组类似,无非更改
<property name = "List">
<List>
<value>100</value>
<value>200</value>
<value>300</value>
</List>
</property>
Map集合略有不同,因为Map集合是以键值对的形式存储数据
<property name = "Map">
<Map>
<entry key="键名" value="值"/>
</Map>
</property>
管理第三方的bean
spring也可以做到管理第三方的bean,Spring基本可以对市面上所有的开发工具进行管理,这里以druid数据库连接池举例
spring管理druid实现步骤:
1.在xml配置文件中写德鲁伊对应的坐标信息
2.编写spring配置文件(使用property编辑属性)
<bean id="datasource" class = "com.alibaba.druid.pool.druiddatasource">
<property name="driverClassName" value="com.mysql.jdbc.driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db1"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
3.创建ApplactionContext对象获取ioc容器,再通过.getbean方法获取对象
管理properties文件
spring对properties文件的管理比较特殊,分以下几步实现:
一、开启context命名空间
- 在spring配置文件中复制如下信息到第三行
xmlns="http://www.springframework.org/schema/beans"
-
将复制到的信息修改为如下格式(在xmlns后面添加参数:context,并把文件尾部信息beans改为context)
xmlns:context="http://www.springframework.org/schema/context"
-
配置xsi:schemaLocation里面的信息,把它里面的两条原有数据复制粘贴到下两行,并且把里面所有的beans改为context
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-beans.xsd
注:以上均为修改完成的信息,修改完成后spring配置文件头详细信息如下:
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
二、使用context空间加载properties文件
<context:property-placeholder location ="jdbc.properties"/>
其中location属性指向的是要配置的properties文件
第三步、使用名称占位符${}读取替换properties文件中的属性
<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>
Spring补充
获取容器的第二种方式:从文件系统下加载配置文件
ApplactionContext ctx = new FileSystemXmlApplicationContext("//文件绝对路径");
该方式和第一种获取方式的区别仅在于查找配置文件的方式不一样,ClassPathXmlApplication是从文件目录下查找,这种方式是从文件系统里面查找,仅此而已
获取bean的其他两种方式:
//最初用法:使用名称查找Bean
Bookdao bookdao = ctx.getbean("Bookdao");
//这种方式获取bean需要做强制转型
//第二种:使用名称获取bean并指定类型
Bookdao bookdao = ctx.getbean("Boodao",Bookdao.class);
//这种方式直接告诉程序数据类型来查找不需要做强制转型
//第三种:指定bean类型获取
Bookdao bookdao = ctx.getbean(Bookdao.class)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!