Spring
一、Spring框架概述
1.spring是什么
spring是一个轻量级开源的JavaEE框架
spring可以解决企业应用开发的复杂性
spring有两个核心部分:IOC和AOP
IOC:控制反转,把创建对象过程交给spring进行管理。
AOP:面向切面,不修改源代码进行功能增强。
2.spring的特点
方便解耦,简化开发
AOP编程的支持
声明式事务的支持
方便程序的测试
方便集成各种优秀框架
降低Java EE API的使用难度
Java 源码是经典学习范例
3.spring入门案例
- 创建maven工程
- 导入spring的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
- 创建实体类对象并且编写方法
public class User {
public void sout(){
System.out.println ("你好,spring5");
}
}
- 创建spring配置文件 bean1.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">
<!--配置User对象的创建-->
<bean id="user" class="com.spring.User"></bean>
</beans>
- 进行测试代码编写
public class UserTest {
@Test
public void testSout(){
//1,加载spring配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext ("bean1.xml");
//2.获得配置创建的对象
User user = (User) context.getBean ("user", User.class);
System.out.println (user);
user.sout ();
}
}
二、IOC容器
1. IOC底层原理
1.1 什么是IOC
- IOC就是控制反转,把对象创建和对象之间的调用过程交给spring进行管理
- 使用IOC的目的:为了耦合度降低
- 做入门案例就是IOC的实现
1.2 IOC底层原理
IOC底层原理主要用到了:xml解析、工厂模式、反射
工厂模式
IOC过程
2. IOC接口
IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
BeanFactory
Spring提供IOC容器实现两个方式:(两个接口)
- BeanFactory :IOC容器基本实现,是spring内部使用的接口,不提供开发人员进行使用。
- 加载配置文件的时候不会创建对象,在获取(使用)才去创建对象
- ApplicationContext :BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人员进行使用。
- 加载配置文件的时候就会把配置文件对象进行创建
ApplicationContext
ApplicationContext 接口的主要实现类:
3. IOC操作Bean管理
3.1 什么是bean管理?
bean管理指的是两个操作:
- spring 创建对象
- spring 注入属性
3.2 Bean管理的两种方式
- 基于XML
- 基于注解
3.3 IOC操作Bean管理(基于XML方式)
3.3.1 基于XML方式创建对象bean标签
在spring配置文件中,使用bean标签,标签里面添加对应的属性,就可以实现对象创建。
注意:创建对象时,默认也是执行无参构造方法完成对象的创建
bean标签的属性介绍:
- id 属性: 唯一标识
- class 属性:类的全路径(包类路径)
<!--配置User对象的创建-->
<bean id="user" class="com.spring.User"></bean>
3.3.2 基于XML方式注入属性
DI:依赖注入,就是注入属性
①方法一:使用set注入
- 创建实体类的属性,设置set方法
package com.spring;
public class Book {
private String bookname;
private int id;
//方式一:set注入
public void setBookname(String bookname) {
this.bookname = bookname;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Book{" + "bookName='" + bookname + '\'' + ", id=" + id + '}';
}
}
- 在spring配置下的 bean 标签下使用 property 完成属性注解
<?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">
<!--配置User对象的创建-->
<bean id="book" class="com.spring.Book">
<!-- 使用 property 完成属性注解
name: 类里面的属性名称
value: 值
-->
<property name="bookname" value="活着"></property>
<property name="id" value="1"></property>
</bean>
</beans>
- 测试
@Test
public void BookTest(){
//1,加载spring配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext ("bean1.xml");
//2.获得配置创建的对象
Book book = context.getBean ("book", Book.class);
System.out.println (book.toString ());
}
②方法二:使用有参数的构造器进行注入
- 创建实体类对象,并设置有参构造器
package com.spring;
public class Person {
private String pname;
private int age;
//有参构造器
public Person(String pname, int age) {
this.pname = pname;
this.age = age;
}
@Override
public String toString() {
return "Person{" + "pname='" + pname + '\'' + ", age=" + age + '}';
}
}
- 在spring的配置中的bean标签下,使用constructor-arg 属性进行注入
<?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">
<!--配置User对象的创建-->
<bean id="person" class="com.spring.Person">
<!-- 还可以使用name属性:
<constructor-arg name="pname" value="霸王花"></constructor-arg>-->
<constructor-arg index="0" value="霸王花"></constructor-arg>
<constructor-arg index="1" value="18"></constructor-arg>
</bean>
</beans>
- 测试
@Test
public void personTest(){
//1,加载spring配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext ("bean1.xml");
//2.获得配置创建的对象
Person person = context.getBean ("person", Person.class);
System.out.println (person.toString ());
}
③p名称空间注入(了解)
使用p名称空间注入,可以简化基于XML配置方式
- 添加p名称空间在配置文件中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置User对象的创建-->
<bean id="book" class="com.spring.Book" p:bookname="海绵宝宝" p:id="9">
</bean>
</beans>
-
进行属性注入,在bean标签里面进行操作
-
测试同①
3.3.3XML注入其他类型属性
- 字面值
- 注入属性——外部bean
- 注入属性——内部bean和级联赋值
①字面量
字面量:
- 空值null
<property name="bookname">
<null/>
</property>
-
属性值包含特殊符号
特殊符号:
1. 把<>进行转义
2. 把特殊符号内容写进CDATA
<property name="bookname" >
<value>
<![CDATA[<<南京>>]]>
</value>
</property>
②注入属性——外部bean
注入属性——外部bean
- 创建两个类service类和dao类
package dao;
public class User implements UserDao {
@Override
public void show() {
System.out.println ("我是user");
}
}
package dao;
public interface UserDao {
public void show();
}
- 在service调用里面的方法
package service;
import dao.UserDao;
public class UserService {
private UserDao userdao;
public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}
public void test(){
System.out.println ("我是userservice");
userdao.show ();
}
}
- 在spring的配置文件中进行配置
<?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="userService" class="service.UserService">
<!-- 注入userDao对象-->
<property name="userdao" ref="userDao"></property>
</bean>
<bean name="userDao" class="dao.User"></bean>
</beans>
- 测试
@Test
public void testSout(){
//1,加载spring配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext ("bean1.xml");
//2.获得配置创建的对象
UserService userService = context.getBean ("userService",UserService.class);
userService.test ();
}
③注入属性——内部bean
- 创建两个类Emp类和Dept类
package bean;
public class Emp {
private String name;
private int age;
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Emp{" + "name='" + name + '\'' + ", age=" + age + ", dept=" + dept + '}';
}
}
package bean;
public class Dept {
private int did;
public void setDid(int did) {
this.did = did;
}
@Override
public String toString() {
return "Dept{" + "did=" + did + '}';
}
}
- 在spring的配置文件中进行配置
<?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="emp" class="bean.Emp">
<property name="name" value="霸王花"></property>
<property name="age" value="18"></property>
<!-- 内部bean -->
<property name="dept">
<bean id="dept" class="bean.Dept">
<property name="did" value="9"></property>
</bean>
</property>
</bean>
- 测试
@Test
public void test(){
//1,加载spring配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext ("bean2.xml");
//2.获得配置创建的对象
Emp emp = context.getBean ("emp", Emp.class);
System.out.println (emp.toString ());
}
④注入属性——级联赋值
方法一:
<?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="emp" class="bean.Emp">
<property name="name" value="霸王花"></property>
<property name="age" value="18"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="bean.Dept">
<property name="did" value="0"></property>
</bean>
</beans>
方法二:
需要提前在emp类中生成dept的get方法
<?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:schemaLoc