1.下载Spring的压缩文件
解压dist:
/docs中是Spring的文档,lib中有Spring的各种功能的jar包包含文档jar包和源文件jar包。
由图可知:Spring的核心包括beans、core、context、spEL。环境配置:在/libs中找到对应的jar包放到项目的/lib中
还需要导入日志的包,在依赖库中找:
Spring叫做EE开发的一站式框架。一站式框架有EE开发的每一层解决方案。
WEB层:SpringMVC
Service层:Spring的Bean管理,Spring声明式事务
Dao层:Spring的JDBC模板,Spring的ORM模板
Spring IOC入门
IOC(Inversion of Control):将对象的创建权交给Spring管理。
传统的设计在需要对业务扩展的时候不得不修改低层源代码,因为面向接口编程接口和实现类有耦合。使用工厂模式可以解除接口和实现类的耦合,但是接口和工厂有耦合。
使用工厂+反射+配置文件 可以实现文件的解耦和。(IOC底层实现)
创建配置文件:Spring默认文件名采用applicationContext.xml命名;
applicationContext.xml配置文件书写:引入约束:spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html 找个the beans schema将<beans>标签复制。 在beans标签中配置。
基础IOC测试:
1.创建需要被控制的对象:
public class Student {
private String name ;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void annoce() {
System.out.println("Student被调用了");
}
}
2.在applicationContext.xml中配置bean
<?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 name="stu" class="com.aloha.spring.test1.Student">
</bean>
</beans>
3.创建测试类
@Test
public void Test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("stu");
student.annoce();
}
测试结果:
IOC和DI
IOC将对象的控制权反转给Spring。
DI依赖注入,前提是得有IOC环境。Spring管理这个类的时候将类依赖的属性注入(设置)进来。
面向对象: 1.依赖:如果B类里创建了A类的对象,那么说B类对A类有依赖关系 2.继承: 3.聚合:松散聚合和紧密聚合 |
<!-- 对于Student中的属性name采用依赖注入的方式 -->
<bean name="stu" class="com.aloha.spring.test1.Student">
<property name="name" value="aloha"></property>
</bean>
这时就把name的值设置为“aloha”了。
Spring的工厂类
BeanFactory:老版本工厂类 | 调用getBean()的时候,才会生成类的实例 |
ApplicationContext:新版本工厂类(现在使用) | 加载配置文件的时候就会将Spring管理的类都实例化 |
可以看到ApplicationContext间接继承了BeanFactory。
ApplicationContext有两个继承者FileSystemXmlApplicationContext和ClassPathXmlApplicationContext。
Spring配置
Spring框架本身四大原则
1)使用POJO进行轻量级和最小侵入开发;
2)通过依赖注入和基于接口编程实现松耦合;
3)通过AOP和默认习惯进行声明编程;
4)使用AOP和模板减少模式化代码。
Spring的bean管理xml和注解:
基于XML配置 | 基于注解配置 | |
bean定义 | <bean id="..." class="..." /> | @Component衍生类: @Controller、@Service、@Repository |
bean名称 | bean 的 id/name属性 | @Component(“名称”) |
bean注入 | <property>或者p名称空间 | @Autowired按类型注入 @Qualifier按名称注入 |
生命过程 | init-method destory-method scope | @PostConstruct @PerDestory @Scope |
<bean>标签的属性:
属性名 | 特点 |
id | 唯一约束,不能出现特殊字符 |
name | 理论不唯一,实际开发要唯一,可以出现特殊字符(老版本中常用) |
init-method | 随便写值,值即是类中的方法名 |
destroy-method | 单例工厂关闭了才会执行小鬼 |
scope | bean作用范围的配置 |
Bean作用范围配置(scope属性):
singleton(默认):单例创建对象。
prototype:多例创建对象。常用于Struts2整合Spring,因为Action是多例。
request:web项目中,Spring创建类后将对象存入request域中。
session:web项目中,Spring创建类后将对象存入session域中。
globalsession:web项目中,porlet环境下使用。作用于所有子网。
Spring属性注入方式(DI依赖注入)
给bean中属性设置值有三种方式:
①有参构造方法方式
在java类中提供需要的有参构造,在Spring配置文件ApplicationContext中加入如下内容:
<bean name="给类命名" class="类的全路径">
<constructor-arg name="类的属性1" value="属性的值1"/>
<constructor-arg name="类的属性2" value="属性的值2"/>
......
</bean>
②get方法方式
在java类中提供属性的get方法,在Spring配置文件ApplicationContext中加入如下内容:
<bean name="给类命名" class="类的全路径">
<property name="类的属性1" value="属性的值1"/>
<property name="类的属性2" value="属性的值2"/>
<property name="对象属性" ref="写对象类的id或name"/>
......
</bean>
③接口注入方式
空。
注解方式:
普通属性:@Value
对象属性:@Resource
自动装配:@Autowired
装配指定对象:@Qualifier
@Resource = @Autowired +@Qualifier
p名称空间属性注入:
首先在<beans ...>中加入属性xml名称空间 xmlns:p="http://www.springframework.org/schema/p"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
......>
p名称空间引入成功。
p名称空间直接写在<bean>标签的属性位置 ,
普通属性 | 对象属性 |
p:属性名 = "值" | p:属性名-ref = "值" |
<!-- p名称空间注入方式 -->
<bean name="pstu" class="com.aloha.spring.test1.Student" p:name="ns_aloha" p:car-ref="car"></bean>
SpEl属性 ——Spring Exception Language
语法:#{SpEL}
需要注意:在SpEL中的<property>标签中没有ref属性,这里的对象值依然使用value属性。
SpEL处可以填基本类型、引用类型、对象.属性、对象.方法......
数组、集合属性注入
是数组还是集合取决于java类中定义的属性是哪种类型,容器类型可以是数组,list,set,map。
每一个标签里装的是数组/集合中的一个(对)值。
<list>
<value></value>普通数组/集合填入value标签中
<ref><ref>对象数组填入ref标签
</list>
map集合这样配置: (还有一种复杂方式没有列出)
<map>
<entry key="键名" value="值名" />
<entry key="键名" value="值名" />
.......
</map>
分模块开发
①分模块就是每部分使用自己的配置文件,ClassPathXmlApplicationContext(String ...)这是一个可变参数的方法; 这个方法可以一次加载多个配置文件,在传附件IOC的时候引入所有有关配置文件:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml","applicationContext2.xml");
②在一个配置文件中导入多个配置文件:
<import resource = "applicationContext.xml" />
resource属性的值是需要导入xml文件的相对路径。