1、如何使用spring.
1.1、添加相应的依赖
<!--引入依赖:-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
</dependencies>
1.2创建spring配置文件。----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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean:表示该类交于spring容器来管理。
id:唯一表示。方便别人通过该id的值找到对应的对象。
class:表示全类名
-->
<bean id="hello01" class="com.am.HelloWorld"></bean>
</beans>
1.3测试
public static void main(String[] args) {
//1.读取spring的配置文件。
ApplicationContext app=new ClassPathXmlApplicationContext("spring01.xml");
//2.从容器中获取指定的对象。
HelloWorld hello01 = (HelloWorld) app.getBean("hello01");
//3.通过对象调用类中相应的成员
hello01.show();
hello01.print();
}
思考
- Hello 对象是谁创建的 ? 【hello 对象是由Spring创建的
- Hello 对象的属性是怎么设置的 ? hello 对象的属性是由Spring容器设置的
这个过程就叫控制反转 :
- 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
- 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .
1.4 DI依赖注入
Dependency Injection
概念
- 依赖注入(Dependency Injection,DI)。
- 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 . 1.5、
1.5注入的方式:
1.5.1 、通过set方法注入。用的非常多。
<bean id="userService" class="com.am.service.UserService">
<!--property:为类中属性赋值通过set方法赋值。name:表示属性名.
ref:值的引用
-->
<property name="userDao" ref="mysql"/>
</bean>
1.5 2、通过构造方法。class类不是你定义,而是使用别人的类,而别人写的类没有无参构造函数。
<bean id="userService" class="com.am.service.UserService">
<!--通过构造方法为类中属性注入:参数的下标-->
<!-- <constructor-arg index="0" ref="oracle"/>-->
<!--通过构造方法为类中属性注入:参数名称-->
<!-- <constructor-arg name="userDao" ref="mysql"/>-->
<constructor-arg type="com.am.dao.UserDao" ref="mysql"/>
</bean>
1.6、注入的数据类型。
(1)基本数据类型或者字符串。
(2)引用类型--对象类型。
(3)集合List,Set。
(4)map集合。
(5)数组
<?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="clazz01" class="com.am.di.Clazz">
<!--如果是基本类型和字符串类型注入值时 使用value-->
<property name="cid" value="123"/>
<property name="cname" value="am123"/>
</bean>
<bean id="stu" class="com.am.di.Student">
<property name="name" value="阿木"/>
<property name="age" value="25"/>
<!--如果属性类型为对象类型。ref-->
<property name="clazz" ref="clazz01"/>
<!--属性类型为list类型 <list></list>-->
<property name="list">
<list>
<value>阿木</value>
<value>张三</value>
<value>李四</value>
<value>阿木</value>
<value>王二</value>
</list>
</property>
<property name="set">
<set>
<value>阿木</value>
<value>张三</value>
<value>李四</value>
<value>阿木</value>
<value>王二</value>
</set>
</property>
<!--map属性类型-->
<property name="map">
<map>
<entry key="name" value="阿木"/>
<entry key="age" value="18"/>
<entry key="sex" value="男"/>
</map>
</property>
<property name="arr">
<array>
<value>110</value>
<value>120</value>
<value>130</value>
<value>140</value>
</array>
</property>
</bean>
</beans>