Spring学习之IOC
spring是什么?
Spring 是一种轻量级开发框架,旨在提高开发人员的开发效率以及系统的可维护性。
我们一般说 Spring 框架指的都是 Spring Framework,它是很多模块的集合,使用这些模块可以很方便地协助我们进行开发。这些模块是:核心容器、数据访问/集成,、Web、AOP(面向切面编程)、工具、消息和测试模块。比如:Core Container 中的 Core 组件是Spring 所有组件的核心,Beans 组件和 Context 组件是实现IOC和依赖注入的基础,AOP组件用来实现面向切面编程。
spring的优势
a.方面解耦简化开发
通过Spring提供的ioc容器,可以将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合。用户不必要再为单例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用
b.aop编程的支持
通过Spring的AOP功能,方便进行面向切面的编程,许多不容易用传统OOP实现的功能可以通过AOP轻松应付
c.声明式事务的支持
可以将我们从单调烦闷的食物管理代码中解脱出来,通过声明式灵活的进行事务的管理,提高开发效率和质量
d.方便的程序测试
可以用非容器依赖的编程方式进行几乎所有的测试工作,测试不再是昂贵的操作,而是随手可做的事情
e.降低Jave EE API的使用难度
Spring对Java EE开发中非常难用的一些API(如JDBC、JavaMail等),都提供了封装,使这些API应用难度大大降低。
f.方便集成各种优秀框架
Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如Struts、Hibernate、MyBatis、Quartz等)的直接支持
spring体系结构
程序的耦合
- 耦合: 程序间的依赖关系.在开发中,应该做到解决编译期依赖,即编译期不依赖,运行时才依赖.
- 解耦的思路: 使用反射来创建对象,而避免使用new关键字,并通过读取配置文件来获取要创建的对象全限定类名.
在注册的驱动的时候就使用了反射来进行解耦
通过工厂来解耦
写一个bean.properties
accountService=com.wh.service.impl.AccountServiceImpl
accountDao=com.wh.dao.impl.AccountDaoImpl
创建一个bean工厂类
public class BeanFactory {
//定义一个Properties对象
private static Properties props;
//定义一个Map,用于存放我们需要创建的对象,我们把他称之为容器
private static Map<String, Object> beans;
//使用静态代码块为Properties对象赋值
static {
try {
//实例化对象
props = new Properties();
//获取properties文件的流对象
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
props.load(in);
//实例化容器
beans = new HashMap<String, Object>();
//取出配置文件中所有的key
Enumeration keys = props.keys();
//遍历枚举
while (keys.hasMoreElements()) {
//取出每个key
String key = keys.nextElement().toString();
//根据key获取value
String beanPath = props.getProperty(key);
//反射创建对象
Object value = Class.forName(beanPath).newInstance();
beans.put(key, value);
}
} catch (Exception e) {
throw new ExceptionInInitializerError("初始化properties失败");
}
}
public static Object getBean(String beanName) {
return beans.get(beanName);
}
}
体会:之前是通过多例模式:每次调用都要通过反射的手段调用到相对应的bean。后面改为单例,先在加载静态类方法的时候先将所有的类创建存入map之中,然后调用跟据beanName 去map中获取与之对应的类
它的引用
public static void main(String[] args) {
// IAccountService as = new AccountServiceImpl();
for (int i = 0; i < 5; i++) {
IAccountService as = (IAccountService) BeanFactory.getBean("accountService");
System.out.println(as);
as.saveAccount();
}
}
/**
* 总结:
* private IAccountDao accountDao =new AccountDaoImpl();
* private IAccountDao accountDao = (IAccountDao)BeanFactory.getBean(“accountDao”);
* 这两者的区别
* 通过new出来的对象 是我们主动去寻求资源的,它的耦合度就高了
* 通过工厂实现的话,是我们把资源交给工厂去代理,我们只要传入对应的字段,然后工厂就返回我们对应的值,
* 从来实现解耦。
*
* ioc的作用:是削减计算机程序之间的耦合
*/
使用spring进行解耦
使用spring的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">
<!-- 把对象的创建交给spring来管理-->
<!--spring对bean的管理细节
1.创建bean的三种方式
2.bean对象的作用范围
3.bean对象的生命周期
-->
<!--创建bean的三种方式-->
<!-- 第一种方式:使用默认构造函数创建
在spring的配置文件中使用bean标签,配置id和class属性之后,且没有其他属性和标签时。
采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
<bean id="accountService" class="com.wh.service.impl.AccountServiceImpl"></bean>
-->
<!--第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
<bean id="instanceFactory" class="com.wh.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
-->
<!--第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器中)
<bean id="accountService" class="com.wh.factory.StaticFactory" factory-method="getAccountService"></bean>
-->
<!-- bean的作用范围调整
bean标签的scope属性:
自拍用:用于指定bean的作用范围
取值:常用的就是单例的和多例的
singleton:单例的(默认值)
prototype:多例的
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
<bean id="accountService" class="com.wh.service.impl.AccountServiceImpl" scope="prototype"></bean>
-->
<!--bean的生命周期
单例对象:
出生:当容器创建时对象出生
活着:只要容器还在,对象一直活着
死亡:容器销毁,对象消亡
总结:当立对象的生命周期和容器相同
多例对象:
出生:当未免使用对象时spring框架为未免创建
活着:对象只要是在使用过程之中久一直活着
死亡:当对象长时间不用,且没有别的对象引用时,由java的垃圾回收器回收
-->
<bean id="accountService" class="com.wh.service.impl.AccountServiceImpl"
scope="singleton" init-method="init" destroy-method="destroy"></bean>
</beans>
模拟一个表现层,用于调用业务层
/**
* 获取spring的ioc的核心容器,并根据id获取对象
* ApplicationContext的三个常用实现类:
* ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下,不在的话,加载不了
* FileSystemXmlApplicationContext:它可以加载磁盘人一路径下的配置文件(必须又访问权限)
* AnnotationConfigApplicationContext:它是用于读取注解创建容器的
*
* 核心容器的两个接口引发出的问题
* ApplicationContext: 单例对象适用
* 它在构建核心容器时,创建对象采用的策略是采用立即加载的方式。也就是说,只要一度去配置文件立马就去创建文件中的配置的对象。
* BeanFactory: 多例对象适用
* 它在构建核心容器时,创建对象采用的策略是采用延迟加载的方式。什么时候根据id获取对象了,什么时候才真正的创建对象。
* @param args
*/
public static void main(String[] args) {
//1.获取核心容器对象
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// ApplicationContext ac = new FileSystemXmlApplicationContext("E:\\spring\\spring01_spring\\src\\main\\resources\\bean.xml");
// //2.根据id获取bean对象
// IAccountService as = (IAccountService) ac.getBean("accountService");
// IAccountDao ad = ac.getBean("accountDao", IAccountDao.class);
// System.out.println(as);
// System.out.println(ad);
//------beanFactory------
Resource resource = new ClassPathResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
IAccountService as = (IAccountService) factory.getBean("accountService");
System.out.println(as);
}
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">
<!--spring中的依赖注入
依赖注入:
dependency Injection
IOC的作用:
降低程序间的耦合
依赖关系的管理:
以后都交给Spring来维护
在当前类需要用到其他类的对象,由Spring为我们提供,我们只需要在配置文件中说明
依赖关系的维护:
久称之为依赖注入
依赖注入:
能注入的数据:有三类
基本类型和String
其他bean类型(在配置文件中或者注解配置过的bean)
复杂类型/集合类型
注入的方式:有三种
第一种:使用构造函数提供
第二种:使用set方法提供
第三种:使用注解提供
-->
<!-- 构造函数注入:
使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性
type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或者某些参数类型
index:用于指定要注入的数据结构构造函数中指定索引位置的参数赋值。索引的位置从0开始
name:用于指定给构造函数中指定名称的参数赋值
=========以上三个用于给指定构造函数中那个参数赋值================
value:用于提供基础类型和String类型的数据
ref:用于指定其他的bean类型数据,它指的就是spring的ioc核心容器中出现过的bean对象
优势:
在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功
弊端:
改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,必须提供。
-->
<bean id="accountService" class="com.wh.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="test"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<!--配置一个日期对象-->
<bean id="now" class="java.util.Date"></bean>
<!--set方法注入 更常用的方式
涉及的标签:property
出现的位置:bean标签的内部
标签的属性
name:用于指定注入时所调用的set方法名称
value:用于提供基础类型和String类型的数据
ref:用于指定其他的bean类型数据,它指的就是spring的ioc核心容器中出现过的bean对象
优势:
创建对象没有明确的限制,可以直接适用默认构造函数
弊端:
如果有某个成员必须有值,则获取对象时有可能set方法没有执行
-->
<bean id="accountService2" class="com.wh.service.impl.AccountServiceImpl2">
<property name="name" value="test"></property>
<property name="age" value="21"></property>
<property name="birthday" ref="now"></property>
</bean>
<!--复杂类型的注入/集合类型的注入
用于给List结构注入的标签有:
list array set
用于给Map结构几个注入的标签:
map props
结构相同,标签可以互换
-->
<bean id="accountService3" class="com.wh.service.impl.AccountServiceImpl3">
<property name="myStrs">
<array>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</array>
</property>
<property name="myList">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<property name="mySet">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="testA" value="AAA"></entry>
<entry key="testB">
<value>BBB</value>
</entry>
</map>
</property>
<property name="myPorps">
<props>
<prop key="testC">ccc</prop>
<prop key="testD">ddd</prop>
</props>
</property>
</bean>
</beans>
spring基于xml实现ioc
项目结构
导入pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wh</groupId>
<artifactId>spring07_xmlioc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
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">
<!--配置service-->
<bean id="accountService" class="com.wh.service.impl.AccountServiceImpl">
<!--注入dao对象-->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置dao对象-->
<bean id="accountDao" class="com.wh.dao.impl.AccountDaoImpl">
<!--注入QueryRunner-->
<property name="queryRunner" ref="queryRunner"></property>
</bean>
<!--配置QueryRunner-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入数据源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/user"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>
账户持久层实现代码
package com.wh.dao;
import com.wh.model.Account;
import java.util.List;
/**
* 账户的持久层
*/
public interface IAccountDao {
/**
* 查询所有
* @return
*/
List<Account> findAllAccount();
/**
* 查询一个
* @return
*/
Account findAccountById(Integer id);
/**
* 保存
* @param account
*/
void saveAccount(Account account);
/**
* 更新
* @param account
*/
void updateAccount(Account account);
/**
* 删除
* @param id
*/
void deleteAccount(Integer id);
}
package com.wh.dao.impl;
import com.wh.dao.IAccountDao;
import com.wh.model.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* 账户持久层实现代码
*/
public class AccountDaoImpl implements IAccountDao {
private QueryRunner queryRunner;
public void setQueryRunner(QueryRunner queryRunner) {
this.queryRunner = queryRunner;
}
public List<Account> findAllAccount() {
try {
return queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Account findAccountById(Integer id) {
try {
return queryRunner.query("select * from account where id = ?", new BeanHandler<Account>(Account.class), id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void saveAccount(Account account) {
try {
queryRunner.update("insert into account(name,money)values(?,?)", account.getName(), account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void updateAccount(Account account) {
try {
queryRunner.update("update account set name=?,money=? where id=? ", account.getName(), account.getMoney(), account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void deleteAccount(Integer id) {
try {
queryRunner.update("delete from account where id=?", id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
model类
package com.wh.model;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
账户的业务层接口
package com.wh.service;
import com.wh.model.Account;
import java.util.List;
/**
* 账户的业务层接口
*/
public interface IAccountService {
/**
* 查询所有
* @return
*/
List<Account> findAllAccount();
/**
* 查询一个
* @return
*/
Account findAccountById(Integer id);
/**
* 保存
* @param account
*/
void saveAccount(Account account);
/**
* 更新
* @param account
*/
void updateAccount(Account account);
/**
* 删除
* @param id
*/
void deleteAccount(Integer id);
}
package com.wh.service.impl;
import com.wh.dao.IAccountDao;
import com.wh.model.Account;
import com.wh.service.IAccountService;
import java.util.List;
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
public Account findAccountById(Integer id) {
return accountDao.findAccountById(id);
}
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
public void deleteAccount(Integer id) {
accountDao.deleteAccount(id);
}
}
使用junit单元测试:测试我们的配置
package com.wh.test;
import com.wh.model.Account;
import com.wh.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* 使用junit单元测试:测试我们的配置
*/
public class AccountServiceTest {
@Test
public void testFindAll() {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.执行方法
List<Account> accounts = as.findAllAccount();
for (Account account : accounts) {
System.out.println(account);
}
}
@Test
public void testFindOne() {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.执行方法
Account account = as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave() {
Account account = new Account();
account.setName("eee");
account.setMoney(123f);
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.执行方法
as.saveAccount(account);
}
@Test
public void testUpdate() {
Account account = new Account();
account.setName("eee");
account.setMoney(123456f);
account.setId(4);
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.执行方法
as.updateAccount(account);
}
@Test
public void testDelete() {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.执行方法
as.deleteAccount(4);
}
}
这个就是学习基于xml的ioc的整个过程
spring基于注解实现ioc
spring中的注解
/**
* 账户的业务层实现类
* <bean id="accountService" class="com.wh.service.impl.AccountServiceImpl"
* scope = "" init-method = "" destroy_method = "">
* <property name = "" value = ""></property>
* </bean>
*
* 对于创建对象的
* 它们的作用就和在xml配置文件中编写一个<bean>标签实现的功能一样
* @Component:
* 作用:用于把当前类对象存入spring容器
* 属性:
* value:用于指定bean的id,当我们不写时,它默认是当前类名,首字母小写
* @Controller: 一般用于表现层
* @Service: 一般用于业务层
* @Repository :一般用于持久层
* 以上三个注解他们的作用和属性与Component一模一样
* 他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰
*
*
*
* 用于注入数据的
* 它们的作用就和在xml配置文件中的<bean>标签中写一个<property>标签的作用一样
* @Autowired:
* 作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
* 如果ioc容器中没有任何bean的类型和注入的变量类型匹配,则报错
* 如果ioc容器中有多个类型匹配时:
* 出现位置:
* 可以是变量上面,也可以是方法上面
* 细节:
* 使用注解注入使,set方法就不是必须的了。
* @Qualifier:
* 作用:在按照类中注入的基础之上再按照名称注入。它在给类成员注入时不能单独使用。但是在给方法参数注入时可以
* 属性:
* value:用于指定注入bean的id
* @Resource:
* 作用:直接按照bean的id注入。可以独立使用
* 属性:
* name:用于指定bean的id
* 以上三个注解都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。
* 另外,集合类型的注入只能通过xml来实现
*
* @Value:
* 作用:用来注入基本类型和String类型的数据
* 属性:
* value:用于指定数据的值。它可以使用Spring的SpEL(也就是spring的el表达式)
* SpEL的写法:$ {表达式}
*
* 用于改变作用范围的
* 它们的作用就和在bean标签中使用scope属性实现的功能一样的
* Scope
* 作用:用于指定bean的作用范围
* 属性:
* value:指定范围的取值。常用取值:singleton prototype
*
* 和生命周期的(了解)
* 它们的作用就和在bean标签中使用init-method 和destroy_method的作用一样
* PreDestroy
* 作用:用于指定销毁方法
* PostConstruct
* 作用:用于指定初始化方法
*/
@Service("accountService")
//@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
// @Autowired
// @Qualifier("accountDao1")
@Resource(name = "accountDao1")
private IAccountDao accountDao;
public void saveAccount() {
accountDao.saveAccount();
}
@PostConstruct
public void init(){
System.out.println("初始化");
}
@PreDestroy
public void destroy(){
System.out.println("销毁");
}
}
相对于xml的ioc来说,注解的更加容易一点,只需要在pom.xml 告知要扫描的包
<context:component-scan base-package="com.wh"></context:component-scan>
在账户持久层实现代码加入注解
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao
然后在业务实现类中引用
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
}