1. Spring简介
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。目前是JavaEE开发的灵魂框架。他可以简化JavaEE开发,可以非常方便整合其他框架,无入侵的进行功能增强。Spring的核心就是控制反转(IOC)和面向切面(AOP)
2. 控制反转(IOC)
控制反转,之前对象的控制权在类手上,现在反转后到了Spring手上
2.1 导入相关依赖
导入spring-content:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
2.2 编写配置文件
编写:
在resource目录下创建一个applicationContext.xml文件,名字可以随便取,但是建议取applicationContext里面编写
<?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">
</beans>
创建容器从容器中获取对象并测试:
public static void main(String[] args) {
ClassPathXmlApplicationContext app =
new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao studentDao = (StudentDao) app.getBean("studentDao");;
Student std = studentDao.getStudentById(30);
System.out.println(std);
}
xml文件的配置
<bean class="com.zhx.domain.Student" id="student">
<property name="name" value="小明"></property>
<property name="id" value="1"></property>
<property name="age" value="18"></property>
<property name="dog" ref="dog"></property>
</bean>
2.3 Bean的常用属性配置
2.3.1 id
2.3.2 class
2.3.3 scope
scope主要有两个值:singleton(单例模式)和prototype(多例模式)
如果设置为singleton则一个容器中只会有这么一个bean对象。默认容器创建的时候就会创建该对象
如果设置为prototype则一个容器中会有多个该bean对象。每次调用getBean方法获取时都会创建一个新对象。
3.spring一些简单注入
3.1 DI依赖注入
依赖注入可以理解成IOC的一种应用场景,反转的是对象间的依赖关系维护权。
3.2 set方法注入
在要注入属性的bean标签中进行配置。前提是该类有提供属性对应的set方法。
<bean class="com.sangeng.domain.Student" id="student" >
<!--
name属性用来指定要设置哪个属性
value属性用来设置要设置的值
ref属性用来给引用类型的属性设置值,可以写上Spring容器中bean的id
-->
<property name="name" value="东南枝"></property>
<property name="age" value="20"></property>
<property name="id" value="1"></property>
<property name="dog" ref="dog"></property>
</bean>
public static void main(String[] args) {
ClassPathXmlApplicationContext app =
new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao studentDao = (StudentDao) app.getBean("studentDao");
}
3.3 有参构造注入
在要注入属性的bean标签中进行配置。前提是该类有提供对应的有参构造。
<bean class="com.sangeng.domain.Student" id="student2" >
<constructor-arg name="name" value="自挂东南枝"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="id" value="30"></constructor-arg>
<constructor-arg name="dog" ref="dog"></constructor-arg>
</bean>
3.4 复杂类型属性注入
<bean class="com.zhx.domain.User" id="user">
<property name="age" value="18"></property>
<property name="name" value="小张"></property>
<property name="student" ref="student"></property>
<property name="list">
<list>
<value>list</value>
<value>的</value>
<value>集合</value>
</list>
</property>
<property name="students">
<list>
<ref bean="student"></ref>
</list>
</property>
<property name="set">
<set>
<value>set</value>
<value>集合</value>
</set>
</property>
<property name="map">
<map>
<entry key="键" value-ref="student"></entry>
</map>
</property>
<property name="arr">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<property name="properties">
<props>
<prop key="键">值</prop>
</props>
</property>
</bean>
4.Lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
注解:
@Data //根据属性生成set,get方法
@NoArgsConstructor //生成无参构造
@AllArgsConstructor //生成全参构造
5.SPEL
我们可以再配置文件中使用SPEL表达式。写法如下:
<property name="age" value="#{20}"/>
<property name="car" value="#{car}"/>
注意:SPEL需要写到value属性中,不能写到ref属性。
6.配置文件
6.1 读取properties文件
我们可以让Spring读取properties文件中的key/value,然后使用其中的值。
①设置读取properties
在Spring配置文件中加入如下标签:指定要读取的文件的路径。
<context:property-placeholder location="classpath:filename.properties">
其中的classpath表示类加载路径下。
我们也会用到如下写法:classpath:*.properties 其中的 * 表示文件名任意。
②使用配置文件中的值
在我们需要使用的时候可以使用${key}来表示具体的值。注意要再value属性中使用才可以。例如:
<property name="propertyName" value="${key}"/>
6.2 引入Spring配置文件
我们可以在主的配置文件中通过import标签的resource属性,引入其他的xml配置文件
<import resource="classpath:applicationContext-book.xml"/>
7. 低频知识点
7.1 bean的配置
7.1.1 name属性
我们可以用name属性来给bean取名。例如:
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource" name="dataSource2,dataSource3">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
获取的时候就可以使用这个名字来获取了
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
DruidDataSource dataSource = (DruidDataSource) app.getBean("dataSource3");
System.out.println(dataSource);
}
7.1.2 lazy-init
可以控制bean的创建时间,如果设置为true就是在第一次获取该对象的时候才去创建。
<bean class="com.alibaba.druid.pool.DruidDataSource" lazy-init="true" id="dataSource" name="dataSource2,dataSource3">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
7.1.3 init-method
可以用来设置初始化方法,设置完后容器创建完对象就会自动帮我们调用对应的方法。
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private String name;
private int id;
private int age;
//初始化方法
public void init(){
System.out.println("对学生对象进行初始化操作");
}
}
<bean class="com.sangeng.domain.Student" id="student" init-method="init"></bean>