Spring入门配置中如何通过DL实现依赖注入的图解教程?

2026-05-16 06:201阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1186个文字,预计阅读时间需要5分钟。

Spring入门配置中如何通过DL实现依赖注入的图解教程?

1. Spring入门配置 1.1 起别名 给项目起别名 1.2 导入其他xml 1.3 Bean的配置最关键的,还有很多配置,我们先学一点

2.依赖注入(DL)

2.1 set注入 三种方式:+xml version=2.1

1、Spring入门配置

1.1、起别名

给项目起别名

!

1.2、import

导入其他xml

1.3、Bean的配置最重要的,又很多配置,我们先学一点

2、依赖注入(DL)

很重要

2.1、set注入

三种方式:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="bing.pojo.Student"> <!-- Set注入--> <property name="name" value="小冰"/> <!-- 引用注入,address--> <property name="address" ref="address"/> <!-- 数组注入--> <property name="advantages"> <array> <value>帅</value> <value>情商高</value> <value>智慧</value> <value>沉稳</value> <value>有钱</value> </array> </property> <!-- set--> <property name="course"> <set> <value>赚钱</value> <value>情商</value> <value>心理学</value> <value>经济学</value> <value>哲学</value> <value>英语</value> <value>数学</value> <value>计算机</value> </set> </property> <!-- map注入--> <property name="grades"> <map> <entry key="java" value="10000"/> <entry key="math" value="200"/> <entry key="English" value="300"/> <entry key="psychology" value="400"/> </map> </property> </bean> <bean id="address" class="bing.pojo.Address"> <property name="address" value="大亳州"/> </bean> </beans>

记住这些类型的注入就行了

2.2、构造器注入

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="bing.User"> <!-- 第一种方式:通过index获取--> <constructor-arg index="0" value="bingbing"/> </bean> <bean id="student" class="bing.Student"> <!-- 方式二:通过key value--> <constructor-arg name="gender" value="小仙女"></constructor-arg> <constructor-arg name="age" value="19"/> </bean> <bean id="student2" class="bing.Student"> <!-- 方式三:通过 类型获取,不推荐可能会出现歧义比如两个String--> <!-- 这里以及上面为什么使用全类名? 肯定用到了反射--> <constructor-arg type="java.lang.String" value="女"/> <constructor-arg type="int" value="18"/> </bean> <bean id="teacher" class="bing.Teacher"> <!-- 我们来试一下两个String类型会发生什么情况--> <constructor-arg type="java.lang.String" value="girl"/> <constructor-arg type="java.lang.String" value="刘老师"/> </bean> <!-- 相当于new对象,对象名为person,只有这一个对象--> <bean id="person" class="bing.Person"> </bean> </beans>

注意:我们一般选用 key value注入,见名知意

2.3、拓展注入

为了简化我们可以引入p/c命名空间

使用方式:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:p="www.springframework.org/schema/p" xmlns:c="www.springframework.org/schema/c" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <!-- p,命名空间,p本质上是什么? 就是properties,可以给简单的属性命名,目的是为了简化--> <bean id="user" class="bing.pojo.User" p:name="zhangsan" p:age="18"> </bean> <!-- c命名空间,怎么用? 是给constructor 构造器初始化的,这里就要求必须要有有参构造--> <bean id="user2" class="bing.pojo.User" c:name="bingbing" c:age="19" p:age="20"/> </beans>

注意点:

Spring入门配置中如何通过DL实现依赖注入的图解教程?

使用前要导入:

xmlns:p="www.springframework.org/schema/p"
xmlns:c="www.springframework.org/schema/c"

类比:

p相当于标签:properties 其实就是set注入,不过可以简化简单的操作

c详单与 :constructor:就是用来给有参构造器初始化的

2.4、bean标签作用域

bean是什么?就是一个对象,也就是类的实例

我们可以给他设置单例模式等等

单例模式

<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

原型模式

<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计1186个文字,预计阅读时间需要5分钟。

Spring入门配置中如何通过DL实现依赖注入的图解教程?

1. Spring入门配置 1.1 起别名 给项目起别名 1.2 导入其他xml 1.3 Bean的配置最关键的,还有很多配置,我们先学一点

2.依赖注入(DL)

2.1 set注入 三种方式:+xml version=2.1

1、Spring入门配置

1.1、起别名

给项目起别名

!

1.2、import

导入其他xml

1.3、Bean的配置最重要的,又很多配置,我们先学一点

2、依赖注入(DL)

很重要

2.1、set注入

三种方式:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="bing.pojo.Student"> <!-- Set注入--> <property name="name" value="小冰"/> <!-- 引用注入,address--> <property name="address" ref="address"/> <!-- 数组注入--> <property name="advantages"> <array> <value>帅</value> <value>情商高</value> <value>智慧</value> <value>沉稳</value> <value>有钱</value> </array> </property> <!-- set--> <property name="course"> <set> <value>赚钱</value> <value>情商</value> <value>心理学</value> <value>经济学</value> <value>哲学</value> <value>英语</value> <value>数学</value> <value>计算机</value> </set> </property> <!-- map注入--> <property name="grades"> <map> <entry key="java" value="10000"/> <entry key="math" value="200"/> <entry key="English" value="300"/> <entry key="psychology" value="400"/> </map> </property> </bean> <bean id="address" class="bing.pojo.Address"> <property name="address" value="大亳州"/> </bean> </beans>

记住这些类型的注入就行了

2.2、构造器注入

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="bing.User"> <!-- 第一种方式:通过index获取--> <constructor-arg index="0" value="bingbing"/> </bean> <bean id="student" class="bing.Student"> <!-- 方式二:通过key value--> <constructor-arg name="gender" value="小仙女"></constructor-arg> <constructor-arg name="age" value="19"/> </bean> <bean id="student2" class="bing.Student"> <!-- 方式三:通过 类型获取,不推荐可能会出现歧义比如两个String--> <!-- 这里以及上面为什么使用全类名? 肯定用到了反射--> <constructor-arg type="java.lang.String" value="女"/> <constructor-arg type="int" value="18"/> </bean> <bean id="teacher" class="bing.Teacher"> <!-- 我们来试一下两个String类型会发生什么情况--> <constructor-arg type="java.lang.String" value="girl"/> <constructor-arg type="java.lang.String" value="刘老师"/> </bean> <!-- 相当于new对象,对象名为person,只有这一个对象--> <bean id="person" class="bing.Person"> </bean> </beans>

注意:我们一般选用 key value注入,见名知意

2.3、拓展注入

为了简化我们可以引入p/c命名空间

使用方式:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:p="www.springframework.org/schema/p" xmlns:c="www.springframework.org/schema/c" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <!-- p,命名空间,p本质上是什么? 就是properties,可以给简单的属性命名,目的是为了简化--> <bean id="user" class="bing.pojo.User" p:name="zhangsan" p:age="18"> </bean> <!-- c命名空间,怎么用? 是给constructor 构造器初始化的,这里就要求必须要有有参构造--> <bean id="user2" class="bing.pojo.User" c:name="bingbing" c:age="19" p:age="20"/> </beans>

注意点:

Spring入门配置中如何通过DL实现依赖注入的图解教程?

使用前要导入:

xmlns:p="www.springframework.org/schema/p"
xmlns:c="www.springframework.org/schema/c"

类比:

p相当于标签:properties 其实就是set注入,不过可以简化简单的操作

c详单与 :constructor:就是用来给有参构造器初始化的

2.4、bean标签作用域

bean是什么?就是一个对象,也就是类的实例

我们可以给他设置单例模式等等

单例模式

<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

原型模式

<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。