Spring
- 2002年,首次推出了Spring框架的雏形:interface21!
- Spring框架以interface21为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日,发布了1.0正式版。
官方文档:https://spring.io/projects/spring-framework#learn
下载地址:https://repo.spring.io/release/org/springframework/spring/
GitHub:https://github.com/spring-projects/spring-framework
Maven依赖:(webmvc可以引入其他相关依赖)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
总结一句话:Spring是轻量级的控制反转(IOC),面向切面编程(AOP)的框架。
1. 第一个Spring程序
环境搭建
- 导入依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
- 创建pojo实体类
public class HelloStr {
private String str;
public void show(){
System.out.println(str);
}
//注意一定要有set方法,因为Spring需要set注入属性的值
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "HelloStr{" +
"str='" + str + '\'' +
'}';
}
}
- 配置xml(暂时起名beans.xml) 正式应为ApplicationContext.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="myhello" class="com.xchao.pojo.HelloStr">
<!--对象中的属性注入,需要有set方法支持
value="具体的值,基本数据类型或String。。。"
ref="Spring中注册的引用类型对象"
-->
<property name="str" value="hello的属性str值"></property>
</bean>
</beans>
- 测试
@Test
public void Test01(){
//ClassPathXmlApplicationContext("beans.xml")通过类路径下的xml文件获取
//获得Spring上下文对象获取Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloStr myhello1 = (HelloStr) context.getBean("myhello");
myhello1.show();
}
- **获取Spring上下文拿到Spring容器方式:**看源码 ctrl+鼠标右键
所谓IOC:一句话搞定,对象由Spring创建,管理,装配。
2. 属性注入方式 (构造器注入)
- 无参构造,属性通过set注入,必须有set方法
<bean id="myhello" class="com.xchao.pojo.HelloStr">
<!--对象中的属性注入,需要有set方法支持
value="具体的值,基本数据类型或String。。。"
ref="Spring中注册的引用类型对象"
-->
<property name="str" value="hello的属性str值"></property>
</bean>
- 构造器注入属性
public class HelloStr {
private String str;
public HelloStr(String str) {
//有参构造
this.str = str;
System.out.println("创建对象");//在使用有参构造生成对象时输出
}
...
}
bean.xml
<!--构造器创建,通过参数下标注入-->
<bean id="myhello1" class="com.xchao.pojo.HelloStr">
<constructor-arg index="0" value="下标驻入属性"></constructor-arg>
</bean>
<!--构造器创建,通过参数类型赋值-->
<bean id="myhello2" class="com.xchao.pojo.HelloStr">
<!--注意:类型为基本类型可直接如int,引用类型需写全路径名-->
<constructor-arg type="java.lang.String" value="属性类型注入"></constructor-arg>
</bean>
<!--构造器创建,通过参数名注入-->
<bean id="myhello3" class="com.xchao.pojo.HelloStr">
<constructor-arg name="str" value="属性名注入"></constructor-arg>
</bean>
测试:
@Test
public void Test01(){
//获得Spring上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloStr myhello1 = (HelloStr) context.getBean("myhello1");//只get得到myhello1
myhello1.show();
}
总结:在配置文件加载的时候,对象就已经初始化了,默认为单例模式。
通过Spring上下文来getBean可获得对象的单例。
3. ApplicationContext.xml中的标签配置
- 别名
<alias name="" alias=""></alias>
- bean
<bean id="myhello1" class="com.xchao.pojo.HelloStr" name="alias1" scope="prototype">
id:唯一标识名
class:包名+类名
name:别名1,别名2...
scope="singleton"默认单例模式
scope="prototype"原型模式
</bean>
- import,合并配置文件时使用
- beans1.xml
- beans2.xml
- beans3.xml
- ApplicationContext.xml
<import resource="beans1.xml"</import>
<import resource=&#