2002年,首次推出了Spring框架的雏形----interface21框架。
2004年3月24日,以interface21框架为基础正式发布了Spring1.0正式版,它是一个轻量级的Java 开发框架,由Rod Johnson创建。
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。
简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。
官网:https://spring.io/projects/spring-framework#overview
官方下载地址: http://repo.spring.io/release/org/springframework/spring
导入的依赖
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.0.RELEASEversion>
dependency>
最终定义:
Spring就是一个轻量级的控制反转(IOC) 和面向切面编程(AOP)的框架!
控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法
在没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制
控制反转后将对象的创建转移给第三方,对象由Spring 来创建 , 管理 , 装配
个人认为所谓控制反转就是:获得依赖对象的方式反转了。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。
注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.10.RELEASEversion>
dependency>
package com.yl.pojo;
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public void show(){
System.out.println("Hello "+str+"!");
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
<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="hello" class="com.yl.pojo.Hello">
<property name="str" value="String" />
bean>
beans>
@Test
public void test(){
//解析beans.xml文件 , 生成管理相应的Bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//getBean : 参数即为spring配置文件中bean的id
Hello hello = (Hello)context.getBean("hello");
hello.show();
System.out.println(hello.toString());
}
默认使用无参构造
下标法创建法:
<bean id="user" class="com.yl.pojo.User">
<constructor-arg index="0" value="张三"/>
bean>
类型创建法(不建议使用)
<bean id="user" class="com.yl.pojo.User">
<constructor-arg type="java.lang.String" value="张三"/>
bean>
参数名创建法
<bean id="user" class="com.yl.pojo.User">
<constructor-arg name="name" value="张三"/>
bean>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!
<alias name="user" alias="userNew"/>
<bean id="user" class="com.yl.pojo.User" name="user2 user3;user4,user5">
<property name="name" value="张三" />
bean>
import一般用于团队开发使用,他可以将多个配置文件,导入合并为一个
合并beans.xml,beans2.xml,beans3.xml配置文件代码如下
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
beans.xml配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.yl.pojo.Address" />
<bean id="student" class="com.yl.pojo.Student">
<property name="name" value="张三" />
<property name="address" ref="address" />
<property name="books">
<array>
<value>西游value>
<value>三国value>
array>
property>
<property name="hobbys">
<list>
<value>唱歌value>
<value>跳舞value>
list>
property>
<property name="card">
<map>
<entry key="身份证" value="666666666666666666" />
<entry key="银行卡" value="111111111111111111" />
map>
property>
<property name="games">
<set>
<value>LoLvalue>
<value>CSvalue>
<value>DNFvalue>
set>
property>
<property name="wife">
<null />
property>
<property name="info">
<props>
<prop key="username">zhangsanprop>
<prop key="password">123456prop>
props>
property>
bean>
<bean id="user" class="com.yl.pojo.User" p:name="zhang" p:age="12" scope="singleton"/>
beans>
测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
System.out.println(student.getAddress());
}
}
可以使用 p命令空间和c命令空间进行注入
package com.yl.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
xml约束
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.yl.pojo.User" p:name="zhang" p:age="12" scope="singleton"/>
<bean id="user2" class="com.yl.pojo.User" c:name="li" c:age="18" scope="singleton" />
beans>
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
User user2 = context.getBean("user", User.class);
System.out.println(user==user2);
}
<bean id="user" class="com.yl.pojo.User" p:name="zhang" p:age="12" scope="singleton"/>
每次从容器中get的时候,都会产生一个新对象!
<bean id="user" class="com.yl.pojo.User" p:name="zhang" p:age="12" scope="prototype"/>