初识 Spring
1.什么是Spring?
1.轻量级的开源框架
目的:
1.解决企业应用开发的复杂性
作用:
1.使用基本的JavaBean来完成以前只可能由EJB完成的事
范围
1.Spring的用途不仅局限于服务器端的开发,从简单性,可测试性,松耦合的角度而言,任何Java应用都可以。
重点
1.Spring的核心是IOC(控制反转)以及AOP(面向切面)
2.Spring的特点
1.方便解耦,简化开发(将所有的对象创建和依赖关系维护交给Spring管理)
2.AOP编程的支持(Spring提供面向切面编程,可以方便的实现对程序进行权限拦截,运行监控等功能)
3.声明式事务的支持(只需要配置就可以完成对事务的管理)
4.方便集成各种优秀框架
3.什么是IOC?
IOC(Inversion of Control),也就是我们所说的控制反转,那什么又是控制反转呢?没有IOC之前我们都是new 对象,有了IOC我们调用对象中的方法不用去创建对象,所谓的IOC就是将对象的创建交给外部容器(Spring),在一定程度上可以很大的解耦合。
4.Spring快速入门
1.下载Spring开发包
https://repo.springsource.org/libs-release-local/org/springframework/spring
2.创建项目,导入jar包
普通项目下导入
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar、
Maven项目在pom.xml下添加Spring的坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
3.创建对应的包,编写实现类和接口
package org.best.service;
import org.best.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
package org.best.service;
import org.best.User;
import java.util.List;
public class UserServiceImpl implements UserService {
public List<User> findAll() {
System.out.println("实现类执行了");
return null;
}
}
4.创建配置文件(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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.best.service.UserServiceImpl" id="userService"></bean>
</beans>
5.编写测试类
package org.best.test;
import org.best.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void test1() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.findAll();
}
}
实现类执行了
5.Spring读取配置文件的三种方式
new ClassPathXmlApplicationContext("配置文件路径");
new FileSystemXmlApplicationContext("配置文件路径");
new AnnotationConfigApplicationContext("配置类");
6.Spring创建Bean对象的三种方式
1.通过空参构造
Spring中默认采用的就是空参构造,当类中没有空参构造时,bean对象无法创建,会报No matching constructor found in class '类名'错误
<bean class="org.best.service.UserServiceImp" id="serviceImp"></bean>
2.使用普通工厂中的方法创建对象
<bean class="org.best.service.StudentServiceImpl" id="studentService"></bean>
<bean factory-bean="studentService" factory-method="say" id="studentsay"></bean>
3.使用工厂中的静态方法创建对象
<bean class="org.best.service.UserServiceImp" factory-method="getStudent" id="service"></bean>
7.Spring中Bean对象的配置
1. id属性和name属性的区别
id Bean起个名字,在约束中采用ID的约束,唯一
取值要求:必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号id:不能出现特殊字符
name -- Bean起个名字,没有采用ID的约束(了解)
取值要求:name:出现特殊字符.如果<bean>没有id的话 , name可以当做id使用
2. class属性 -- Bean对象的全路径
3. scope属性 -- scope属性代表Bean的作用范围
singleton -- 单例(默认值)
prototype -- 多例,在Spring框架整合Struts2框架的时候,Action类也需要交给Spring做管理,配置把Action类配置成多例!!
request -- 应用在Web项目中,每次HTTP请求都会创建一个新的Bean 放到request域中,request销毁了,对象也就没了
session -- 应用在Web项目中,同一个HTTP Session 共享一个Bean Session销毁了,里面的对象也就没了
globalsession -- 应用在Web项目中,多服务器间的session
4. Bean对象的创建和销毁的两个属性配置
* 说明:Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法
* init-method -- 当bean被载入到容器的时候调用init-method属性指定的方法
* destroy-method -- 当bean从容器中删除的时候调用destroy-method属性指定的方法
想查看destroy-method的效果,有如下条件
* scope= singleton有效
* web容器中会自动调用,但是main函数或测试用例需要手动调用(需要使用ClassPathXmlApplicationContext的close()方法)
5. bean对象的生命周期
单例对象
出生:当容器创建时对象出生
活着:只要容器还在,对象一直活着
死亡:容器销毁,对象消亡
总结:单例对象的生命周期和容器相同
多例对象
出生:当我们使用对象时spring框架为我们创建 getBean()
活着:对象只要是在使用过程中就一直活着。
死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收
8.什么是DI?
1.DI,Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中
通过set方法来注入值
<!--通过set方法注入值-->
<bean class="org.best.service.StudentServiceImpl" id="studentService">
<property name="name" value="bestLee"></property>
<property name="age" value="18"></property>
<property name="height" value="185.0"></property>
</bean>
public class StudentServiceImpl implements StudentService {
private String name;
private int age;
private double height;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(double height) {
this.height = height;
}
}
通过有参构造注入值
<bean class="org.best.service.UserServiceImp" id="serviceImp" >
<constructor-arg name="name" value="ls"></constructor-arg>
<constructor-arg name="age" value="17"></constructor-arg>
<constructor-arg name="height" value="180"></constructor-arg>
</bean>
public class UserServiceImp implements UserService {
private String name;
private int age;
private double height;
public UserServiceImp(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
@Override
public String toString() {
return "UserServiceImp{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
在Service层注入Dao层
<bean class="org.best.service.UserServiceImp" id="serviceImp">
<property name="userDao" ref="userDao"></property>
</bean>
<bean class="org.best.dao.UserDaoImpl" id="userDao">
</bean>
package org.best.service;
import org.best.dao.UserDaoImpl;
public class UserServiceImp implements UserService {
private UserDaoImpl userDao;
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
public static StudentService getStudent(){
return new StudentServiceImpl();
}
public void Test(){
userDao.init();
}
}
9.数组,集合(List,Set,Map),Properties注入
package org.best.service;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class DiImpl implements Di {
private String[] array;
private List list;
private Set set;
private Map map;
private Properties properties;
public void setArray(String[] array) {
this.array = array;
}
public void setList(List list) {
this.list = list;
}
public void setSet(Set set) {
this.set = set;
}
public void setMap(Map map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
<!--数组注入-->
<bean class="org.best.service.DiImpl" id="di">
<property name="array">
<array>
<value>bestlee</value>
<value>yyp</value>
<value>最好的我们</value>
</array>
</property>
</bean>
<!--list注入-->
<bean class="org.best.service.DiImpl" id="di2">
<property name="list">
<list>
<value>11</value>
<value>22</value>
</list>
</property>
</bean>
<!--set注入-->
<bean class="org.best.service.DiImpl" id="di3">
<property name="set">
<set>
<value>3333</value>
<value>4444</value>
</set>
</property>
</bean>
<!--properties注入-->
<bean class="org.best.service.DiImpl" id="di4">
<property name="map">
<props>
<prop key="1">1号</prop>
<prop key="2">2号</prop>
</props>
</property>
</bean>
<!--map注入-->
<bean class="org.best.service.DiImpl" id="di5">
<property name="map">
<map>
<entry key="1" value="1号"></entry>
<entry key="2" value="2号"></entry>
</map>
</property>
</bean>
10.多个配置文件管理
1.在主配置文件中引入其他配置文件
<!--引入配置文件-->
<import resource="配置文件路径"></import>
2.在加载配置文件时加载多个配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("配置文件1","配置文件2" );