1、什么是IOC
IOC-Inversion of Control,即控制反转。它不是什么技术,而是一种设计思想。
传统的创建对象的方法是直接通过 new 关键字,而 spring 则是通过 IOC 容器来创建对象,也就是说我们将创建对象的控制权交给了 IOC 容器。
解释:
User user = new User(); 自己控制对象的创建
现在需要对象,自己不创建,交给外部的容器创建,叫控制反转。
IOC容器= bean.xml配置 + ApplicationContext容器类
IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建、初始化、销毁等工作交给spring容器来做。
削减计算机程序的耦合(解除我们代码中的依赖关系)
2、Spring环境配置
Spring官网:https://spring.io/
下载地址:https://repo.spring.io/libs-release-local/org/springframework/spring/
这里使用的是spring5.0.2。
注意:spring5版本是用jdk8编写的,所以要求我们的jdk版本是8及以上。 同时tomcat的版本要求8.5及以上。


解压:(Spring目录结构:)
docs :API和开发规范.
libs :jar包和源码.
schema :约束.
3、Spring基于XML的IOC环境搭建和入门


添加工程需要的源码文件

创建测试数据库:
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);
pom.xml文件导入spring相关配置
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
自动导入后

通过maven可以查看依赖关系视图


建立bean.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">
</beans>
在beans中继续填写bean标签
<!--
bean标签:用于配置让spring创建对象,并且存入ioc容器之中
id属性:对象的唯一标识。
class属性:指定要创建对象的全限定类名
-->
<!--把对象的创建交给spring来管理-->
<!-- 配置service -->
<bean id="accountService" class="com.neuedu.service.impl.AccountServiceImpl">
</bean>
<!-- 配置dao -->
<bean id="accountDao" class="com.neuedu.dao.impl.AccountDaoImpl">
</bean>
创建业务层接口和实现类
接口:AccountService.java
package com.neuedu.service;
/**
* 账户业务层的接口
*/
public interface AccountService {
/**
* 模拟保存账户
*/
void saveAccount();
}
实现类:AccountService.java
package com.neuedu.service.impl;
import com.neuedu.dao.AccountDao;
import com.neuedu.dao.impl.AccountDaoImpl;
import com.neuedu.service.AccountService;
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao = new AccountDaoImpl();
public void saveAccount(){
accountDao.saveAccount();
}
}
创建持久层接口和实现类
接口:AccountDao.java
package com.neuedu.dao;
/**
* 账户的持久层接口
*/
public interface AccountDao {
/**
* 模拟保存账户
*/
void saveAccount();
}
实现类:AccountDao.java
package com.neuedu.dao.impl;
import com.neuedu.dao.AccountDao;
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl implements AccountDao {
public void saveAccount(){
System.out.println("保存了账户");
}
}
Client.java
package com.neuedu.ui;
import com.neuedu.dao.AccountDao;
import com.neuedu.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 模拟一个表现层,用于调用业务
*/
public class Client {
/**
* 获取spring的IOC容器,并根据id获取对象
*/
public static void main(String[] args) {
//1、获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取Bean对象
AccountService as = (AccountService)ac.getBean("accountService");
AccountDao adao = ac.getBean("accountDao", AccountDao.class);
System.out.println(as);
System.out.println(adao);
}
}
运行效果:

spring中工厂的类结构图

ApplicationContext的三个常用实现类:
- ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(与第二个相比,第一个更常用)
- FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
- AnnotationConfigApplicationContext:它是用于读取注解创建容器的(后续解释)。
ApplicationContext ac = new FileSystemXmlApplicationContext("C:\\Users\\zhy\\Desktop\\bean.xml");
BeanFactory和ApplicationContext的区别
核心容器的两个接口引发出的问题:
- ApplicationContext:
单例对象适用 采用此接口
它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。 - BeanFactory:
多例对象使用
它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
ApplicationContext测试:



BeanFactory测试
//--------BeanFactory----------
Resource resource = new ClassPathResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
AccountService as = (AccountService)factory.getBean("accountService");
System.out.println(as);


bean标签
作用: 用于配置对象让spring来创建的。
默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。
属性:
id:给对象在容器中提供一个唯一标识。用于获取对象。
class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。
scope:指定对象的作用范围。
- singleton :默认值,单例的.
- prototype :多例的.
- request :WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中.
- session :WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中.
- global session :WEB项目中,应用在Portlet环境.如果没有Portlet环境那么
globalSession相当于session.
init-method:指定类中的初始化方法名称。
destroy-method:指定类中销毁方法名称。
spring对bean对象管理细节
1.创建bean的三种方式
2.bean对象的作用范围
3.bean对象的生命周期
1、创建bean的三种方式
创建工程


使用之前例子的工程作为模板,修改业务层实现类AccountServiceImpl
package com.neuedu.service.impl;
import com.neuedu.service.IAccountService;
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements AccountService {
public AccountServiceImpl(){
System.out.println("对象创建了");
}
public void saveAccount(){
System.out.println("service中的saveAccount方法执行了。。。");
}
}
bean.xml
<!--创建Bean的三种方式 -->
<!-- 第一种方式:使用默认构造函数创建。
在spring的配置文件中使用bean标签,配以id和class属性后,且没有其他属性和标签时,
采用的就是默认构造方法创建bean对象,此时如果类中没有默认构造方法,则对象无法创建
-->
<bean id="accountService" class="com.neuedu.service.impl.AccountServiceImpl"></bean>
注意:
该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造方法
package com.neuedu.factory;
import com.neuedu.service.AccountService;
import com.neuedu.service.impl.AccountServiceImpl;
/**
* 模拟一个工厂类
*/
public class InstanceFactory {
public AccountService getAccountService(){
return new AccountServiceImpl();
}
}
<!-- 第二种方式: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
-->
<bean id="instanceFactory" class="com.neuedu.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>

package com.neuedu.factory;
import com.neuedu.service.AccountService;
import com.neuedu.service.impl.AccountServiceImpl;
再次模拟一个工厂类,其中含有静态方法
/**
* 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造方法)
*/
public class StaticFactory {
public static AccountService getAccountService(){
return new AccountServiceImpl();
}
}
<!-- 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)
-->
<bean id="accountService" class="com.neuedu.factory.StaticFactory" factory-method="getAccountService"></bean>

2.bean对象的作用范围
修改测试类
package com.neuedu.ui;
import com.neuedu.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 模拟一个表现层,用于调用业务
*/
public class Client {
/**
* 获取spring的IOC容器,并根据id获取对象
*/
public static void main(String[] args) {
//1、获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取Bean对象
AccountService as1 = (AccountService)ac.getBean("accountService");
AccountService as2 = (AccountService)ac.getBean("accountService");
System.out.println(as1 == as2);
}
}
bean标签的scope属性:
作用:用于指定bean的作用范围
取值: 常用的就是单例的和多例的
singleton:单例的(默认值)
prototype:多例的
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session。
单例:
<bean id="accountService" class="com.neuedu.service.impl.AccountServiceImpl" scope="singleton"></bean>

多例:
<bean id="accountService" class="com.neuedu.service.impl.AccountServiceImpl" scope="prototype"></bean>

3.bean对象的生命周期
bean对象的生命周期
1)单例对象
创建:当容器创建时对象出生
存在:只要容器还在,对象一直存在
销毁:容器销毁,对象消亡
总结:单例对象的生命周期和容器相同
2)多例对象
创建:当我们使用对象时spring框架为我们创建
存在:对象只要是在使用过程中就一直存在。
销毁:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收
修改service实现类
package com.neuedu.service.impl;
import com.neuedu.service.AccountService;
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements AccountService {
public AccountServiceImpl(){
System.out.println("对象创建了");
}
public void saveAccount(){
System.out.println("service中的saveAccount方法执行了。。。");
}
public void init(){
System.out.println("对象初始化了。。。");
}
public void destroy(){
System.out.println("对象销毁了。。。");
}
}
测试类:
package com.neuedu.ui;
import com.neuedu.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 模拟一个表现层,用于调用业务
*/
public class Client {
/**
* 获取spring的IOC容器,并根据id获取对象
*/
public static void main(String[] args) {
//1、获取核心容器对象
//ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取Bean对象
AccountService as = (AccountService)ac.getBean("accountService");
as.saveAccount();
ac.close();
}
}
bean.xml
<bean id="accountService" class="com.neuedu.service.impl.AccountServiceImpl"
scope="singleton" init-method="init" destroy-method="destroy"></bean>

<bean id="accountService" class="com.neuedu.service.impl.AccountServiceImpl"
scope="prototype" init-method="init" destroy-method="destroy"></bean>

网友评论