spring框架就是一个 开源的轻量级的java开发框架,是一个分层的javaEE/SE full-stack一站式开发框架
一站式开发框架:就是对EE开发的每一层都有一个解决方案
◆JAVA EE应该更加容易使用。
◆面向对象的设计比任何实现技术(比如JAVA EE)都重要。
◆面向接口编程,而不是针对类编程。Spring将使用接口的复杂度降低到零。(面向接口编程有哪些复杂度?)
◆代码应该易于测试。Spring框架会帮助你,使代码的测试更加简单。
◆JavaBean提供了应用程序配置的最好方法。
◆在Java中,已检查异常(Checked exception)被过度使用。框架不应该迫使你捕获不能恢复的异常。
调用getBean()方法的时候,才会生成类的实例
该工厂类有两个实现类,
加载配置文件的时候,就会将Spring管理的类都实例化
- ClassPathXmlApplicationContext :加载类路径下的配置文件
- FileSystemXmlApplicationContext:加载文件系统下的配置文件
就是在xml文件中写的时候,出现标签的提示信息。
- bean标签的id和name的配置
- id :使用了约束中的唯一约束。里面不能出现特殊字符的。
- name :没有使用约束中的唯一约束(理论上可以出现重复的,但是实际开发不能出现的)。里面可以出现特殊字符。
- Spring和Struts1框架整合的时候
init-method :Bean被初始化的时候执行的方法
destroy-method :Bean被销毁的时候执行的方法(Bean是单例创建,工厂关闭)
- scope :Bean的作用范围
singleton :默认的,Spring会采用单例模式创建这个对象。
prototype :多例模式。(Struts2和Spring整合一定会用到)
- request :应用在web项目中,Spring创建这个类以后,将这个类存入到request范围中。
- session :应用在web项目中,Spring创建这个类以后,将这个类存入到session范围中。
- globalsession :应用在web项目中,必须在porlet环境下使用。但是如果没有这种环境,相对于session。
IOC:控制反转,就是将对象交给spring管理,原来是自己new对象,现在是交给程序(spring框架)new对象,所以就是控制反转
面向对象的时候
依赖
Class A{}
Class B{
public void xxx(A a){}
}
继承:is a
Class A{}
Class B extends A{}
聚合:has a
官网链接地址
docs:spring的开发规范和API
libs:spring的开发所需要的jar包和源码
schema:spring框架的配置文件所需要的约束
package com.dao;
public interface UserDao {
/**
* 用户管理Dao层接口
*/
public void save();
}
======================================================
package com.dao.impl;
import com.dao.UserDao;
public class UserDaoImpl implements UserDao {
/**
* 用户管理Dao层实现类
*/
public void save() {
System.out.println("save方法运行啦");
}
}
**出现的问题**
如果底层的实现切换了,需要修改源代码,能不能不修改程序源代码对程序进行扩展?
在spring的解压路径下spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html 的html中有scamer约束,复制下来就可以。
<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="userDao" class="com.dao.impl.UserDaoImpl" >bean>
beans>
public class SpringDemo {
@Test
public void demo1(){
//创建spring的工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.save();
}
}
Bean已经都交给Spring管理,Spring创建这些类的时候,有几种方式:
public class Bean1 {
public Bean1(){
System.out.println("无参的构造方法执行了--");
}
}
<bean id="bean1" class="com.domain.Bean1">bean>
public class Bean2 {
public Bean1(){
System.out.println("无参的构造方法执行了--");
}
}
=====================================
public class BeanFactory {
public static Bean2 createBean2(){
System.out.println("BeanFactory工厂方法createBean2执行了");
return new Bean2();
}
}
<bean id="bean2" class="com.domain.BeanFactory"
factory-method="createBean2">bean>
public class Bean3 {
public Bean1(){
System.out.println("无参的构造方法执行了--");
}
}
=====================================
public class BeanFactory {
public Bean3 createBean3(){
System.out.println("BeanFactory工厂方法crateBean3执行了");
return new Bean3();
}
}
<bean id="beanFactory" class="com.domain.BeanFactory">bean>
<bean id="bean3" factory-bean="beanFactory" factory-method="createBean3">bean>
DI:依赖注入,前提必须有IOC的环境,当spring管理这个类的时候,也就是new这个对象的时候,要把这个对象需要的属性的值注入进来,属性有基本类型,引用类型、集合,数组
必须要有有参的构造方法,才能实现属性的注入。在注入引用类型的变量,使用ref属性,属性的值是交给spring管理的对象的name属性的值
Car.java类
public class Car {
private String name;
private double price;
private Bean1 bean1;
public Car(String name, double price, Bean1 bean1) {
this.name = name;
this.price = price;
this.bean1 = bean1;
}
}
applicationContext.xml文件
<bean id="bean1" class="com.domain.Bean1">bean>
<bean id="car" class="com.domain.Car">
<constructor-arg name="name" value="奔驰">constructor-arg>
<constructor-arg name="price" value="12">constructor-arg>
<constructor-arg name="bean1" ref="bean1">constructor-arg>
bean>
public class Customer {
private String name;
private Car car;
public void setName(String name) {
this.name = name;
}
public void setCar(Car car) {
this.car = car;
}
public Customer() {
System.out.println("无参构造执行了");
}
public Customer(String name, Car car) {
this.name = name;
this.car = car;
}
}
applicationContext.xml
<bean id="customer" class="com.domain.Customer">
<property name="name" value="王五">property>
<property name="car" ref="car">property>
bean>
需要在aplicationContext.xml文件中加上scamer约束
- 通过引入p名称空间完成属性的注入:
- 写法:
- 普通属性 p:属性名=”值”
- 对象属性 p:属性名-ref=”值”
Customer.java类与上面一样
applicationContext.xml
<bean id="customer1" class="com.domain.Customer" p:name="王二" p:car-ref="car">bean>
<bean id="customer" class="com.domain.Customer">
<property name="name" value="#{car.carName()}">property>
<property name="car" value="#{car}">property>
bean>
集合类型的注入也是根据set方法注入的,具体配置如下
<bean id="customer" class="com.domain.Customer">
<property name="list" >
<list>
<value>嘿嘿value>
<ref bean="car">ref>
list>
property>
<property name="list">
<list>
<value>李兵value>
<ref bean="car">ref>
list>
property>
<property name="set">
<set>
<value>aaavalue>
<ref bean="car">ref>
set>
property>
<property name="map" >
<map>
<entry key="aaa" value="111">entry>
<entry key="bbb" value-ref="car">entry>
map>
property>
bean>
ApplicationContext applicationContext
= new ClassPathXmlApplicationContext(
"applicationContext.xml","applicationContext2.xml");
<import resource="applicationContext2.xml">import>
引入jar包
引入配置文件
在服务器启动的时候,创建一个Spring的工厂。
创建完工厂,将这个工厂类保存到ServletContext中。
每次使用的时候都从ServletContext中获取。
监听ServletContext对象的创建和销毁。
spring-web.jar
默认加载的配置文件applicationContext.xml文件在/WEB-INF/applicationContext.xml目录下,可以通过配置文件进行修改,使其加载src目录下的xml文件
修改路径的方法
Spring配置文件在WEB-INF下面
这种情况你可以不去管他,不进行配置,因为spring会默认去加载,如果一定要配置呢,可以这样
WEB-INF/applicationContext.xmlSpring配置文件在WEB-INF下的某个文件夹下,比如config下,可以这样配置
WEB-INF/config/applicationContext.xmlSpring配置文件在src下面,可以这样配置
WEB-INF/classes/applicationContext.xml 或者
classpath:applicationContext.xmlSpring配置文件在src下的某个包里,比如com.config,可以这样配置
WEB-INF/classes/com/config/applicationContext.xml 或者
classpath:com/config/applicationContext.xml