嘿嘿 testmybatisspring项目,都看的狂神说系列的视频,spring–Javaweb+MVC – springMVC --springBoot–springCloud,加油🚗🚓🚕
Spring 网站https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans 🌍🌎🌏
Spring🌻🌼🌷
1 创建环境
1 导入依赖
spring-webmvc spring-jdbc
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>1.2.4</version>
</dependency>
2 优点
轻量级的控制反转和面向切面的框架
ApplicationContext
ConfigurableApplicationContext
AbstractApplicationContext
AbstractRefreshableApplicationContext
AbstractRefreshableConfigApplicationContext
AbstractXmlApplicationContext
ClassPathXmlApplicationContext
3 application.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">
......
</beans>
2 IOC创建对象的四种方式
1 IOC 无参创建对象
<bean id="user" class="com.test.pojo.User">
<property name="name" value="张三"/>
</bean>
2 IOC 有参创建对象
1 参数下标
<bean id="user" class="com.test.pojo.User">
<!-- <property name="name" value="张三"/>-->
<constructor-arg index="0" value="李四"/>
</bean>
2 参数类型
<bean id="user" class="com.test.pojo.User">
<!-- <property name="name" value="张三"/>-->
<!-- <constructor-arg index="0" value="李四"/>-->
<constructor-arg type="java.lang.String" value="王五"/>
</bean>
3 参数名
<bean id="user" class="com.test.pojo.User">
<!-- <property name="name" value="张三"/>-->
<!-- <constructor-arg index="0" value="李四"/>-->
<!-- <constructor-arg type="java.lang.String" value="王五"/>-->
<constructor-arg name="name" value="柳六"/>
</bean>
3 依赖注入
1 构造器注入
IOC有参构造三种方式
2 setter注入
@Getter
@Setter
@ToString
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Set<String> games;
private Map<String, String> card;
private Properties info;
private String married;
}
@ToString
@Getter
@Setter
public class Address {
private String address;
}
<bean id="student" class="com.test.pojo.Student">
<!-- 基本数据类型-->
<property name="name" value="ketty"/>
<!-- 引用数据类型-->
<property name="address" ref="address"/>
<!-- 数组注入-->
<property name="books">
<array>
<value>head first java</value>
<value>hello spring5!</value>
</array>
</property>
<!-- map注入-->
<property name="card">
<map>
<entry key="歌手" value="许嵩"></entry>
<entry key="演员" value="赵丽颖"/>
</map>
</property>
<!-- set注入-->
<property name="games">
<set>
<value>王者</value>
<value>抖音</value>
</set>
</property>
<!-- List注入-->
<property name="hobbies">
<list>
<value>sing</value>
<value>dance</value>
</list>
</property>
<!-- null空值注入-->
<property name="married">
<null></null>
</property>
<!-- Properties注入-->
<property name="info">