Spring框架学习(一)
框架宗旨:不重新发明技术,让原有技术使用起来更加方便.
发明人:Rod Johnson
核心功能:
IoC/DI: 控制反转/依赖注入
AOP: 面向切面编程
声明式事务
Spring 框架 runtime:
- test: spring 提供测试功能
- Core Container:核心容器.Spring 启动最基本的条件
- Beans : Spring 负责创建类对象并管理对象
- Core: 核心类
- Context: 上下文参数.获取外部资源或这管理注解等
- SpEl: expression.jar
- AOP: 实现 aop 功能需要依赖
- Aspects: 切面 AOP 依赖的包
- Data Access/Integration : spring 封装数据访问层相关内容
- JDBC : Spring 对 JDBC 封装后的代码.
- ORM: 封装了持久层框架的代码.例如 Hibernate
- transactions:对应 spring-tx.jar,声明式事务使用.
- WEB:需要 spring 完成 web 相关功能时需要.
- 由 tomcat 加载 spring 配置文件时需要有 spring-web 包
IOC(控制反转):
- 概念:
IoC 完成的事情原先由程序员主动通过 new 实例化对象事情,转交给 Spring 负责。 - 控制反转中控制指的是:控制类的对象。
- 控制反转中反转指的是转交给 Spring 负责。
- IOC最大作用:解耦
Spring环境搭建:
-
首先导入需要的Spring的jar包:
-
在src下创建applicationContext.xml配置文件:
注:- Spring 容器 ApplicationContext,applicationContext.xml 配 置的信息最终存储到了AppliationContext 容器中。
- spring 配置文件是基于 schema,schema 文件扩展名.xsd,每次引入一个 xsd 文件是一个 namespace(xmlns)
- 配置文件中只需要引入基本 schema,通过 创建对象.
<?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="Student1" class="pojo.Student">
<!-- id表示获取到对象标识
class 创建哪个类的对象
-->
<constructor-arg index="0" name="snum" type="int" value="123"></constructor-arg>
<constructor-arg index="1" name="sname" type="java.lang.String" value="大大大"></constructor-arg>
</bean>
<bean id="Student2" class="pojo.StudentFactory" factory-bean="Student2" factory-method="newInstance"></bean>
<bean id="Student3" class="pojo.StudentFactory2" factory-method="newInstance"></bean>
</beans>
- 在src下创建pojo包,并创建实例类:
package com.bjsxt.pojo;
public class People {
private int id;
private String name;
public People() {
super();
System.out.println("执行构造方法");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + "]";
}
}
- 编写测试文件test:
package test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Student;
import pojo.StudentFactory;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Student student=ac.getBean("Student1",Student.class);
System.out.print(student);
//StudentFactory sf=new StudentFactory();
//System.out.print(sf);
}
}
Spring创建对象的三种方式:
- 通过构造方法创建:
- 无参构造创建:默认情况
public Student() {
super();
System.out.print("无参构造方法");
}
- 有参构造创建:需要明确配置
- 需要在类中提供有参构造方法
- 在 applicationContext.xml 中设置调用哪个构造方法创建 对象
- 如果设定的条件匹配多个构造方法执行最后的构造方法
- index : 参数的索引,从 0 开始
- name: 参数名
- type:类型(区分开关键字和封装类 int 和 Integer)
public Student(String sname,int snum,int sclass,int sex){
super();
this.sname=sname;
this.snum=snum;
this.sclass=sclass;
this.sex=sex;
System.out.print("含参构造方法2");
}
<bean id="Student1" class="pojo.Student">
<constructor-arg index="0" name="snum" type="int" value="123"></constructor-arg>
<constructor-arg index="1" name="sname" type="java.lang.String" value="大大大"></constructor-arg>
</bean>
-
实例工厂:
- 工厂设计模式:帮助创建类对象.一个工厂可以生产多个对象。实例工厂:需要先创建工厂,才能生产对象。
- 创建步骤:
- 必须要有一个实例工厂:
package pojo;
public class StudentFactory {
public Student newInstance(){
return new Student(123,"大大大",1,111);
}
}
- 在 applicationContext.xml 中配置工厂对象和需要创建的 对象:
<bean id="Student2" class="pojo.StudentFactory" factory-bean="Student2" factory-method="newInstance"></bean>
静态工厂:
- 不需要创建工厂,快速创建对象
- 实现步骤:
- 编写一个静态工厂:
package pojo;
public class StudentFactory2 {
public static Student newInstance(){
return new Student(2,"aaa",2,121);
}
}
- 并在applicationContext.xml 中:
<bean id="Student3" class="pojo.StudentFactory2" factory-method="newInstance"></bean>
给Bean的属性赋值:
- 通过构造方法设置值
- 如果属性是基本数据类型或 String 等简单类型:
<bean id="peo" class="com.bjsxt.pojo.People">
<property name="id" value="222"></property>
<property name="name" value="张三"></property>
</bean>
相当于:
<bean id="peo" class="com.bjsxt.pojo.People">
<property name="id">
<value>456</value>
</property>
<property name="name">
<value>zhangsan</value>
</property>
</bean>
- 如果属性是 Set<?>
<property name="sets">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</set>
</property>
- 如果属性是 List<?>:
<property name="list" value="1">
</property>
- 如果属性是数组:
如果数组中就只有一个值,可以直接通过 value 属性赋值:
<property name="strs" >
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
- 如果属性是 map:
<property name="map">
<map>
<entry key="a" value="b" >
</entry>
<entry key="c" value="d" >
</entry>
</map>
</property>
- 如果属性 Properties 类型:
<property name="demo">
<props>
<prop key="key">value</prop>
<prop key="key1">value1</prop>
</props>
</property>
DI(依赖注入):
- 概念: 当一个类(A)中需要依赖另一个类()对象时,把 B 赋值给 A 的过 程就叫做依赖注入.
<property name="desk" ref="desk"></property>
</bean>
<bean id="desk" class="com.bjsxt.pojo.Desk">
<property name="id" value="1"></property>
<property name="price" value="12"></property>
</bean>