1.Spring
1.1简介:
Spring:春天------>给软件行业带来了春天!
. 2002,首次推出了Spring框架的雏形: interface21框架!
Spring框架即以interface21框架为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日,发布了1.0正式版。
. Rod Johnson,Spring Framework创始人,著名作者。很难想象Rod Johnson的学历,真的让好多人大吃一惊,他是悉尼大学的博士,然而他的专业不是计算机,而是音乐学。
. spring理念:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架!
. SSH : Struct2 + Spring + Hibernate!
. SSM : SpringMvc + Spring + Mybatis!
官方下载地址:
https://repo.spring.io/ui/native/release/org/springframework/spring
中文:https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/core.html#beans
https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-collection-elements
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0</version>
</dependency>
1.2优点:
Spring 是一个开源的免费的框架(容器)
Spring是一个轻量级的、非入侵式的框架!
控制反转(IOC),面向切面编程(AOP) !
支持事务的处理,对框架整合的支持!
-总结一句话: Spring就是一个轻量级的控制反转(IOC)和面向切面编程的框架!
. Spring Boot
.一个快速开发的脚手架。
.基于SpringBoot可以快速的开发单个微服务。。约定大于配置!
. Spring Cloud
. SpringCloud是基于SpringBoot实现的。
因为现在大多数公司都在使用SpringBoot进行快速开发,学习SpringBoot的前提,需要完全掌握Spring及springMVC!承上启下的作用!
IOC本质
控制反转loC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现loC的一种方法,也有人认为DI只是lOC的另一种说法。没有lOC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。
采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是loC容器,其实现方法是依赖注入(Dependency Injection,Dl)。
HelloSpring
beans.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="..."> (1) (2)
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
完善注入:
beans.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Bean注入,ref-->
<bean id="address" class="com.sophy.pojo.Address">
<property name="address" value="唐朝"/>
</bean>
<bean id="student" class="com.sophy.pojo.Student">
<!-- collaborators and configuration for this bean go here -->
<!--普通值注入-->
<property name="name" value="李白"/>
<property name="address" ref="address"/>
<!--数组注入 ref-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>左传</value>
<value>三国演义</value>
</array>
</property>
<!--List 注入-->
<property name="hobbys">
<list>
<value>写诗</value>
<value>喝酒</value>
<value>唱歌</value>
</list>
</property>
<!--Map 注入-->
<property name="card">
<map>
<entry key="身份证" value="1111111111111111111"/>
<entry key="银行卡" value="1213513546410313"/>
</map>
</property>
<!--Set 注入-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--NULL 注入-->
<property name="wife">
<null/>
</property>
<!--properties-->
<property name="info">
<props>
<prop key="学号">21021022</prop>
<prop key="性别">男</prop>
<prop key="username">root</prop>
<prop key="password">12121</prop>
</props>
</property>
</bean>
<!-- <bean id="..." class="...">
<!– collaborators and configuration for this bean go here –>
</bean>
more bean definitions go here -->
</beans>
学生类:
package com.sophy.pojo;
import java.util.*;
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", booK=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
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;
}
}
地址类:
package com.sophy.pojo;
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 + '\'' +
'}';
}
}
测试类:
package com.sophy;
import com.sophy.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
/**
* Student{name='李白',
* address=Address{address='null'},
* booK=[红楼梦, 西游记, 水浒传, 左传, 三国演义],
* hobbys=[写诗, 喝酒, 唱歌],
* card={身份证=1111111111111111111, 银行卡=1213513546410313},
* games=[LOL, COC, BOB], wife='null',
* info={学号=21021022,
* 性别=男, password=12121, username=root}}
*/
}
}
拓展注入方式:
p命名空间
加上
xmlns:p="http://www.springframework.org/schema/p"
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p 命名空间注入,可以直接注入属性的值:properties-->
<bean id="user" class="com.sophy.pojo.User" p:name="杜甫" p:age="68"/>
</beans>
测试
public void test2(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = (User) context.getBean("user1");
System.out.println(user);
}
c 命名空间
加上:
xmlns:c="http://www.springframework.org/schema/c"
<?xml version="1.0" encoding="UTF-8"?>
<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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p 命名空间注入,可以直接注入属性的值:properties-->
<bean id="user" class="com.sophy.pojo.User" p:name="杜甫" p:age="68"/>
<!-- c 命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.sophy.pojo.User" c:name="张飞" c:age="56"/>
</beans>
测试
public void test2(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = (User) context.getBean("user2");
System.out.println(user);
}
beans 的作用域:
1.单例模式
<!--单例模式 Sring默认机制-->
<bean id="user2" class="com.sophy.pojo.User" c:name="张飞" c:age="56" scope="singleton"/>
2.原型模式
每次从容器中get的时候,都会产生一个新的对象
<bean id="user2" class="com.sophy.pojo.User" c:name="张飞" c:age="56" scope="prototype"/>
测试:
@Test
public void test2(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = (User) context.getBean("user2");
User user1 = (User) context.getBean("user2");
System.out.println(user==user1);
System.out.println(user);
}
}
结果:
3.其余的request、session、application在web中使用