Spring03_DI
2020-05-25 16:10:29来源:博客园 阅读 ()
Spring03_DI
本教程源码请访问:tutorial_demo
一、什么是依赖注入
依赖注入:Dependency Injection ,指容器负责创建和维护对象之间的依赖关系,而不是通过对象本身负责自己的创建和解决自己的依赖。在当前类需要用到其他类的对象,由Spring为我们提供,我们只需要在配置中说明。
二、如何进行注入
2.1、构造函数注入
2.1.1、创建项目
-
在Idea中新建Maven工程;
-
工程创建完成后添加相应的坐标。
<?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>org.codeaction</groupId> <artifactId>di</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.6.RELEASE</version> </dependency> </dependencies> </project>
2.1.2、添加User类
package org.codeaction.bean;
import java.util.Date;
/**
* 用户类
*/
public class User1 {
private String name;
private Integer age;
private String address;
private Date birthday;
//有参构造方法
public User(String name, Integer age, String address, Date birthday) {
this.name = name;
this.age = age;
this.address = address;
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", birthday=" + birthday +
'}';
}
}
User1类中一定要有有参的构造方法。
2.1.3、添加XML配置文件
XML文件在resource目录下。
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="now" class="java.util.Date"></bean>
<bean id="user1" class="org.codeaction.bean.User1">
<!-- 通过构造函数注入属性 -->
<constructor-arg name="name" value="Tom"></constructor-arg>
<constructor-arg name="age" value="10"></constructor-arg>
<constructor-arg name="address" value="上海"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
</beans>
构造函数注入使用constructor-arg标签,这个标签在bean标签的内部。
constructor-arg标签中的属性:
- type:指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型;
- index:指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始;
- name:指定给构造函数中指定名称的参数赋值;
- value:提供基本类型和String类型的数据;
- ref:指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象。
前三个属性用于指定给构造函数中哪个参数赋值,其中name属性最常用。
2.1.4、添加测试方法
package org.codeaction.test;
import org.codeaction.bean.User1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User1 user1 = (User1) context.getBean("user1");
System.out.println(user1);
}
}
运行main方法,控制台输出如下:
User1{name='Tom', age=10, address='上海', birthday=Mon May 25 09:01:58 CST 2020}
2.2、set方法注入
2.2.1、添加User类
package org.codeaction.bean;
import java.util.Date;
public class User2 {
private String name;
private Integer age;
private String address;
private Date birthday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User2{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", birthday=" + birthday +
'}';
}
}
User2类中一定要有set方法。
2.2.2、修改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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="now" class="java.util.Date"></bean>
<bean id="user1" class="org.codeaction.bean.User1">
<!-- 通过构造函数注入属性 -->
<constructor-arg name="name" value="Tom"></constructor-arg>
<constructor-arg name="age" value="10"></constructor-arg>
<constructor-arg name="address" value="上海"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="user2" class="org.codeaction.bean.User2">
<!-- 通过set方法注入 -->
<property name="name" value="Bob"></property>
<property name="age" value="20"></property>
<property name="address" value="北京"></property>
<property name="birthday" ref="now"></property>
</bean>
</beans>
2.2.3、修改测试方法
package org.codeaction.test;
import org.codeaction.bean.User1;
import org.codeaction.bean.User2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//User1 user1 = (User1) context.getBean("user1");
//System.out.println(user1);
User2 user2 = (User2) context.getBean("user2");
System.out.println(user2);
}
}
运行main方法,控制台输出如下:
User2{name='Bob', age=20, address='北京', birthday=Mon May 25 09:37:17 CST 2020}
2.3、注入复杂类型属性
本质上还是给属性注入值,只是被注入的属性都是复杂类型(数组,List,Set,Map,Properties)。
2.3.1、添加User类
package org.codeaction.bean;
import java.util.*;
public class User3 {
private String[] hobbies1;
private List<String> hobbies2;
private Set<String> hobbies3;
private Map<String, String> idInfo1;
private Properties idInfo2;
public void setHobbies1(String[] hobbies1) {
this.hobbies1 = hobbies1;
}
public void setHobbies2(List<String> hobbies2) {
this.hobbies2 = hobbies2;
}
public void setHobbies3(Set<String> hobbies3) {
this.hobbies3 = hobbies3;
}
public void setIdInfo1(Map<String, String> idInfo1) {
this.idInfo1 = idInfo1;
}
public void setIdInfo2(Properties idInfo2) {
this.idInfo2 = idInfo2;
}
public void show() {
System.out.println("array----" + Arrays.toString(hobbies1));
System.out.println("list----" + hobbies2);
System.out.println("set----" + hobbies3);
System.out.println("map----" + idInfo1);
System.out.println("properties----" + idInfo2);
}
}
2.3.2、修改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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="now" class="java.util.Date"></bean>
<bean id="user1" class="org.codeaction.bean.User1">
<!-- 通过构造函数注入属性 -->
<constructor-arg name="name" value="Tom"></constructor-arg>
<constructor-arg name="age" value="10"></constructor-arg>
<constructor-arg name="address" value="上海"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="user2" class="org.codeaction.bean.User2">
<!-- 通过set方法注入 -->
<property name="name" value="Bob"></property>
<property name="age" value="20"></property>
<property name="address" value="北京"></property>
<property name="birthday" ref="now"></property>
</bean>
<!-- 注入复杂类型属性 -->
<bean id="user3" class="org.codeaction.bean.User3">
<!-- 注入复杂类型属性 -->
<property name="hobbies1">
<array>
<value>篮球</value>
<value>读书</value>
<value>帆船</value>
</array>
</property>
<property name="hobbies2">
<list>
<value>太极</value>
<value>自由搏击</value>
<value>咏春</value>
</list>
</property>
<property name="hobbies3">
<set>
<value>旅游</value>
<value>蹦极</value>
<value>马拉松</value>
</set>
</property>
<property name="idInfo1">
<map>
<entry key="name" value="John"></entry>
<entry key="age">
<value>20</value>
</entry>
<entry key="gender" value="男"></entry>
</map>
</property>
<property name="idInfo2">
<props>
<prop key="address">广州</prop>
<prop key="height">180</prop>
</props>
</property>
</bean>
</beans>
用于为List结构注入的标签:list、array、set;
用于为Map结构注入的标签:map、props;
结构相同,标签可以互换。
2.3.3、修改测试方法
package org.codeaction.test;
import org.codeaction.bean.User1;
import org.codeaction.bean.User2;
import org.codeaction.bean.User3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//User1 user1 = (User1) context.getBean("user1");
//System.out.println(user1);
//User2 user2 = (User2) context.getBean("user2");
//System.out.println(user2);
User3 user3 = (User3)context.getBean("user3");
user3.show();
}
}
运行main方法,控制台输出如下:
array----[篮球, 读书, 帆船]
list----[太极, 自由搏击, 咏春]
set----[旅游, 蹦极, 马拉松]
map----{name=John, age=20, gender=男}
properties----{height=180, address=广州}
2.3.4、修改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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="now" class="java.util.Date"></bean>
<bean id="user1" class="org.codeaction.bean.User1">
<!-- 通过构造函数注入属性 -->
<constructor-arg name="name" value="Tom"></constructor-arg>
<constructor-arg name="age" value="10"></constructor-arg>
<constructor-arg name="address" value="上海"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="user2" class="org.codeaction.bean.User2">
<!-- 通过set方法注入 -->
<property name="name" value="Bob"></property>
<property name="age" value="20"></property>
<property name="address" value="北京"></property>
<property name="birthday" ref="now"></property>
</bean>
<!-- 注入复杂类型属性 -->
<bean id="user3" class="org.codeaction.bean.User3">
<!-- 注入复杂类型属性 -->
<property name="hobbies1">
<set>
<value>旅游</value>
<value>蹦极</value>
<value>马拉松</value>
</set>
</property>
<property name="hobbies2">
<array>
<value>篮球</value>
<value>读书</value>
<value>帆船</value>
</array>
</property>
<property name="hobbies3">
<list>
<value>太极</value>
<value>自由搏击</value>
<value>咏春</value>
</list>
</property>
<property name="idInfo1">
<props>
<prop key="address">广州</prop>
<prop key="height">180</prop>
</props>
</property>
<property name="idInfo2">
<map>
<entry key="name" value="John"></entry>
<entry key="age">
<value>20</value>
</entry>
<entry key="gender" value="男"></entry>
</map>
</property>
</bean>
</beans>
运行main方法,控制台输出如下:
array----[旅游, 蹦极, 马拉松]
list----[篮球, 读书, 帆船]
set----[太极, 自由搏击, 咏春]
map----{height=180, address=广州}
properties----{age=20, name=John, gender=男}
通过上面的修改,说明标签结构相同,标签可以互换。
原文链接:https://www.cnblogs.com/codeaction/p/12955456.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Java基础语法菜鸟教程笔记 2020-06-10
- Spring Boot 教程 - Elasticsearch 2020-06-09
- java环境教程:Tomcat下载,安装,设置为Windows服务,启动 2020-06-09
- Java 入门教程 2020-06-09
- 秒懂系列,超详细Java枚举教程!!! 2020-06-08
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash