Spring
- Spring:意为javaEE开发者的春天,是一个一站式分层结构的轻量级框架目前比较受欢迎。
- Spring特点:开源的 轻量级的 分层架构的 一站式的 对目前流行的框架支持较高。
- 使用Spring好处:可降低复杂性 松耦合 高性能 易测试 声明事务
- 下载地址: http://repo.spring.io/release/org/springframework/spring/
- 使用Spring所需要的 jar: 核心包 beans context core expression logging
- 创建编写配置文件
bean类 提供有参无参构造方法 get、set方法
private String name;
private int age;
private Car car;
public Person() {
System.out.println("空参构造函数");
}
public Person(String name, int age, Car car) {
super();
this.name = name;
this.age = age;
this.car = car;
System.out.println("构造函数1");
}
public Person(Car car ,String name, int age ) {
super();
this.car = car;
this.name = name;
this.age = age;
System.out.println("构造函数2");
}
public Person(Car car ,int name, int age ) {
super();
this.car = car;
this.name = name+"";
this.age = age;
System.out.println("构造函数3");
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
}private String name;
private int age;
private Car car;
public Person() {
System.out.println("空参构造函数");
}
public Person(String name, int age, Car car) {
super();
this.name = name;
this.age = age;
this.car = car;
System.out.println("构造函数1");
}
public Person(Car car ,String name, int age ) {
super();
this.car = car;
this.name = name;
this.age = age;
System.out.println("构造函数2");
}
public Person(Car car ,int name, int age ) {
super();
this.car = car;
this.name = name+"";
this.age = age;
System.out.println("构造函数3");
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
}
编写一个测试类
public static void main(String[] args) {
// 1.创建容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.从容器中获取bean
Person p = (Person) ac.getBean("person");
System.out.println(p);
配置Spring
1. set注入 把需要的外部对象放入内部叫做注入
<bean name="person" class="com.lanou.Person">
<!--1.set注入 必须提供set方法
value 属性用于注入基础数据类型
ref属性注入引用数据类型
-->
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="car" ref="car"></property>
</bean>
7.什么是IOC:
inverse of control 反转控制
就是把对象的翻转创建交给了spring来管理
之前是我们手动new对象 现在是spring拿对象
实现原理:通过反射+配置文件 来达到
class c = Class.form(“com.lanou.Person”);
Object o = c.newIncetance();
context.put(“person”,o);
context.get(“person”);
8.DI技术:
dependency injection
依赖 注入
类A 中需要B类提供的功能
成为A依赖B
例如:Service 依赖DAO
UserService
//UserDao dao = new UserDaoimpl();
使用spring后
UserDao dao = context.getBean(“userDAOimpl”);
//不修改源代码就能实现组件的切换
注入:从外部把需要的对象放到内部就叫做注入
UserService
private UserDao dao ;
set/get
依赖注入:最终的目的就是提高程序的扩展性 尽可能不去修改代码
依赖注入的四种方式
1.set注入(需要依赖set/get方法)
<!--1.set注入 必须提供set方法
value 属性用于注入基础数据类型
ref属性注入引用数据类型
-->
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="car" ref="car"></property>
</bean>
2.构造函数注入(要提供带参数的构造方法)
<!--2.构造函数注入 调用指定的构造函数并传入参数实现注入 -->
<bean name="person4" class="com.lanou.Person">
<!--构造函数的参数 -->
<!-- type 指定参数放到哪一个位置 当多个构造函数参数顺序相同 类型不同时 -->
<constructor-arg name="name" value="10089" type="int"></constructor-arg>
<!-- name 指定参数名 需要与构造函数一致 -->
<constructor-arg name="age" value="58"></constructor-arg>
<!-- index 指定参数放到哪一个位置 当多个构造函数参数类相同 顺序不同时 -->
<constructor-arg name="car" ref="car" index="0" ></constructor-arg>
</bean>
3.p命名空间注入
<!--3.p命名空间
需要先引入命名空间:xmlns:p="http://www.springframework.org/schema/p"
-->
<bean name="person5" class="com.lanou.Person" p:name="老王" p:age="20" p:car-ref="car"></bean>
4.SPEL表达式
<!--4.SPEL注入
SPEL Spring的表达式语言 能够实现一些简单的逻辑
与jsp的EL一个性质
-->
<bean name="person6" class="com.lanou.Person">
<!--找到一个叫做person的对象 调用getName方法获取 -->
<property name="name" value="#{person.name}" ></property>
<!--找到一个叫做person5的对象 调用getAge方法获取 -->
<property name="age" value="#{person5.age}"></property>
<property name="car" ref="car"></property>
</bean>
实现类
1.ClassPathXmlApplicationContext 用于加载class路径下的配置文件
2.fileSystemXMLApplicationContext 加载系统路径下的配置文件
两种容器:
/*
* Spring 提供了两种容器
* 1.BeanFactroy(过时了)
* 他是spring框架最古老的接口
* 仅实现了IOC DI基础功能的接口
* 特点 获取bean时才会创建对应的bean 由于以前硬件设备资源匮乏
*
* 2.ApplicationContext
* 一旦加载配置文件 就全部创建
*/
Resource resource = new FileSystemResource("/Users/lanou/javaweb/java-spring-4-18/src/beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
System.out.println("配置加载完成");
System.out.println(factory.getBean("person"));
scope属性
scope属性 指定bean对象的作用域
singleton:单例 该bean对象在spring中只存在一个(最常用) 在和Struts2整合时Action对象不能设置为单列
prototype:多例 每次getBean时都会创建新的bean(常用)
request和session:对象的生存时间与session和request保持一致
<bean id="person1" scope="singleton" class="com.lanou.Person"></bean>
复杂类型注入
bean类提供有参无参构造方法 get、set方法 toString方法
//复杂类型的注入
private Object[] array;
private List list;
private Map map;
private Properties properties;
配置文件及注入:
<bean id="cbean" class="com.lanou.CollectionBean">
<!--
注入数组
元素只有一个时 直接使用value就可以 -->
<property name="array">
<array>
<value>jack</value>
<value>john</value>
<value>rose</value>
<value>jack</value>
</array>
</property>
<property name="list">
<list>
<value>长城</value>
<value>红旗</value>
<value>五菱宏光</value>
</list>
</property>
<property name="map">
<map>
<!--使用entry 标签指定键值对 -->
<entry key="name" value="大大"></entry>
<entry key="aeg" value="100"></entry>
<!--key或value是引用类型 就加上ref -->
<entry key-ref="abean" value="这是一个bean"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="jdbcDriver">jdbc:mysql://localhost:3306/AAAA</prop>
<prop key="user">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
<bean name="abean" class="com.lanou.CollectionBean"></bean>
测试的方法:
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
ac.getBean("person2");
//获取输出cbean
CollectionBean bean = (CollectionBean)ac.getBean("cbean");
System.out.println(bean);
配置文件的模块化
当一个配置文件内容太多时 可以将其他功能按模块划分
1.在创建容器时传入多个字符串对象 就是配置文件名
2.在配置文件中使用import标签导入
<!--import导入其他配置文件 -->
<import resource="classpath:applicationContext.xml"/>