IoC 全称为 Inversion of Control
,翻译为 “控制反转”,依赖注入DI(Dependency Injection
)是它的具体实现。依赖注入这个词让人望而生畏,现在已经演变成一项复杂的编程技巧或设计模式理念。但事实证明,依赖注入并不像它听上去那么复杂。在项目中应用DI,你会发现你的代码会变得异常简单并且更容易理解和测试。本篇文章将主要介绍Ioc的概念和作用,以及Ioc是如何解决程序的耦合性问题的。
程序的耦合与解耦
耦合指的就是就是对象之间的依赖性。耦合具有两面性:一方面,紧密耦合的代码难以测试、难以复用、难以理解,并且典型地表现出“打地鼠”式的bug特性(修复一个bug,将会出现一个或者更多新的bug)。另一方面,一定程度的耦合又是必须的—完全没有耦合的代码什么也做不了。为了完成有实际意义的功能,不同的类必须以适当的方式进行交互。总而言之,耦合是必须的,但应当被小心谨慎地管理。
例如下面的程序(选自Spring实战第四版):
一个骑士的接口:
public interface Knight {
void embarkOnQuest();
}
一个探险任务的接口:
public interface Quest {
void embark();
}
创建一次拯救少女的探险任务,实现探险任务接口:
public class RescueDamselQuest implements Quest {
public void embark() {
System.out.println("Embarking on a quest to rescue the damsel.");
}
}
创建一个营救少女的骑士,实现骑士接口。
public class DamselRescuingKnight implement Knight{
private RescueDamselQuest quest;
pulicDamselRescuingKnight(){
//RescueDamselQuest类与DamselRescuingKnight类紧密地耦合到了一起
this.quest=new RescueDamselQuest();
}
//测试该方法时也会变得很困难,因为你无法保证quest.embark()方法被调用,因为quest可能并没有被创建
public void embarkOnQuest(){
quest.embark();
}
}
骑士实现了Knight接口,有了embarkOnQuest功能,DamselRescuingKnight在构造函数中自行构建了RescueDamselQuest,这使得DamselRescuingKnight和RescueDamseQuest紧密地连接在一起,这极大限制了骑士执行的探险任务。如果有其他的Quest让骑士去embarkOnQuest,那骑士就无能为力了。
进行改造
通过依赖注入,对象的依赖关系将由系统中负责协调各对象的第三方组件在创建对象的时候进行设定。对象无需自行创建或管理它们的依赖关系。
public class BraveKnight implements Knight {
private Quest quest;
public BraveKnight(Quest quest) {
this.quest = quest;
}
public void embarkOnQuest() {
quest.embark();
}
}
现在这个勇敢的骑士内部并没有自行创建任务,而是将探险任务当作构造参数传进来,属于构造函数注入。任何探险任务只实现了Ouest接口都可以被注入进来让BraveKnight响应。这样就降低了两个类之间的依赖。
例如现在又有一个打巨龙的任务:
public class SlayDragonQuest implements Quest {
private PrintStream stream;
public SlayDragonQuest(PrintStream stream) {
this.stream = stream;
}
public void embark() {
stream.println("Embarking on quest to slay the dragon!");
}
}
这个实现Ouest的接口可以注入到时Knight之中,项目中新建一个spring-context.xml文件,在源码的路径下。创建对象之间的协作行为称之为装配,Spring有许多装配Bean的方式,XML、JavaCode、基于注解的自动装配。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">
<!--声明一个Bean以及它的依赖关系-->
<bean id="slayDragonQuest" class="com.test.bean.SlayDragonQuest">
<constructor-arg value="#{T(java.lang.System).out}"></constructor-arg>
</bean>
<bean id="braveKnight" class="com.test.bean.BraveKnight">
<constructor-arg ref="slayDragonQuest"/>
</bean>
</beans>
Spring通过应用上下文(Application Context)来装配bean的定义把它们组装起来,Spring全权负责bean的创建和组装。Spring有多种上下文的实现,区别就是如何加载bean的配置。
工厂模式解耦
在Web项目中,UI层
,Service层
,Dao层
之间有着前后调用的关系。
public class MyServiceImpl implements IMyService {
private IMyDao myDao = new MyDaoImpl(); // 业务层要调用持久层的接口和实现类
public void myService(){
myDao.serviceProcess();
}
}
业务层
依赖持久层
的接口和实现类,若编译时不存在没有持久层实现类,则编译将不能通过,这构成了编译期依赖。
在实际开发中我们可以把展示层,业务层,持久层三层的对象都使用配置文件配置起来,当启动服务器应用加载的时候,让一个类中的方法通过读取配置文件,把这些对象创建出来并存起来。在接下来的使用的时候,直接拿过来用就好了。 那么,这个读取配置文件,创建和获取三层对象的类就是工厂。
使用springIOC解决程序耦合
简单实例
1.准备工作: 创建MAVEN项目,并准备三层接口类和实现类
创建maven项目,配置其pom.xml
如下:
<?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>cn.maoritian</groupId>
<artifactId>learnspring</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 引入-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>
2. 配置bean
:在类的根路径下的resource
目录下创建bean.xml
文件,把对象的创建交给spring来管理。
每个<bean>
标签对应一个类,其class
属性为该类的全类名,id
属性为该类的id,在spring配置中通过id
获取类的对象。
<?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来管理-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
</beans>
3.在表现层文件Client.java
中通过容器
创建对象。通过核心容器的getBean()
方法获取具体对象。
public class Client {
public static void main(String[] args) {
// 获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 根据id获取Bean对象
IAccountService as = (IAccountService)ac.getBean("accountService");
// 执行as的具体方法
// ...
}
}
我们常用的容器有三种实现类: ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,AnnotationConfigApplicationContext.
- ClassPathXmlApplicationContext: 它是从类的根路径下加载配置文件(常用)
- FileSystemXmlApplicationContext: 它是从磁盘路径上加载配置文件(前提是有访问权限)
- AnnotationConfigApplicationContext: 读取注解创建容器
核心容器中两个接口引发的问题:
ApplicationContext(采用):立即加载,读取完配置文件后就立即创建对象,饿汉式那种
BeanFctory:延迟加载,使用这个对象的时候才创建,懒汉式那种
使用XML配置文件实现IOC
使用配置文件实现IOC,要将托管给spring的类写进bean.xml
配置文件中:
bean标签
- 作用: 配置托管给spring的对象,默认情况下调用类的无参构造函数,若果没有无参构造函数则不能创建成功
- 属性:
- id: 指定对象在容器中的唯一标识,将其作为参数传入getBean()方法可以获取获取对应对象。
- class: 指定类的全类名,默认情况下调用无参构造函数进行创建。
- scope: 指定对象的作用范围,可选值如下
- singleton: 单例对象,默认值
- prototype: 多例对象
- request: 将对象存入到web项目的request域中
- session: 将对象存入到web项目的session域中
- global session: 将对象存入到web项目集群的session域中,若不存在集群,则global session相当于session
- init-method:指定类中的初始化方法名称,在对象创建成功之后执行。
- destroy-method:指定类中销毁方法名称,对prototype多例对象没有作用,因为多例对象的销毁时机不受容器控制。
bean的作用范围和生命周期
- 单例对象: scope="singleton"
- 作用范围: 每个应用只有一个该对象的实例,它的作用范围就是整个应用
- 生命周期: 单例对象的创建与销毁 和 容器的创建与销毁时机一致
- 对象出生: 当应用加载,创建容器时,对象就被创建
- 对象活着: 只要容器存在,对象一直活着
- 对象死亡: 当应用卸载,销毁容器时,对象就被销毁
- 多例对象: scope="prototype"
- 作用范围: 每次访问对象时,都会重新创建对象实例.
- 生命周期: 多例对象的创建与销毁时机不受容器控制
- 对象出生: 当使用对象时,创建新的对象实例
- 对象活着: 只要对象在使用中,就一直活着
- 对象死亡: 当对象长时间不用时,被 java 的垃圾回收器回收了
实例化 Bean 的三种方式
1. 使用默认无参构造函数创建对象
默认情况下会根据默认无参构造函数来创建类对象,若Bean类中没有默认无参构造函数,将会创建失败。
<bean id="accountService" class="cn.maoritian.service.impl.AccountServiceImpl"></bean>
2. 使用静态工厂的方法创建对象
创建一个模拟的静态工厂如(在实际项目中,这个工厂可能在一个jar包中):
// 静态工厂,其静态方法用于创建对象
public class StaticFactory {
public static IAccountService createAccountService(){
return new AccountServiceImpl();
}
}
使用StaticFactory类中的静态方法createAccountService创建对象,涉及到<bean>标签的属性:
- id属性: 指定对象在容器中的标识,用于从容器中获取对象
- class属性: 指定静态工厂的全类名
- factory-method属性: 指定生产对象的静态方法
<bean id="accountService" class="cn.maoritian.factory.StaticFactory"
factory-method="createAccountService"></bean>
其实,类的构造函数也是静态方法,因此默认无参构造函数
也可以看作一种静态工厂方法。
3.使用实例工厂的方法创建对象
创建实例工厂如下:
public class InstanceFactory {
public IAccountService createAccountService(){
return new AccountServiceImpl();
}
}
先创建实例工厂对象instanceFactory
,通过调用其createAccountService()
方法创建对象,涉及到<bean>
标签的属性:
factory-bean
属性: 指定实例工厂的id
factory-method
属性: 指定实例工厂中生产对象的方法
<bean id="instancFactory" class="cn.maoritian.factory.InstanceFactory"></bean>
<bean id="accountService"
factory-bean="instancFactory"
factory-method="createAccountService"></bean>
依赖注入
依赖注入(Dependency Injection)是spring框架核心ioc的具体实现.
通过控制反转,我们把创建对象托管给了spring,但是代码中不可能消除所有依赖,例如:业务层仍然会调用持久层的方法,因此业务层类中应包含持久化层的实现类对象。
我们等待框架通过配置的方式将持久层对象传入业务层,而不是直接在代码中new某个具体的持久化层实现类,这种方式称为依赖注入。
因为我们是通过反射的方式来创建属性对象的,而不是使用new关键字,因此我们要指定创建出对象各字段的取值。一共有三种方式进行依赖注入:
1. 使用构造函数注入
通过类默认的构造函数来给创建类的字段赋值,相当于调用类的构造方法。
涉及的标签: <constructor-arg>用来定义构造函数的参数,其属性可大致分为两类:
- 寻找要赋值给的字段
- index: 指定参数在构造函数参数列表的索引位置
- type: 指定参数在构造函数中的数据类型
- name: 指定参数在构造函数中的变量名,最常用的属性
- 指定赋给字段的值
- value: 给基本数据类型和String类型赋值
- ref: 给其它Bean类型的字段赋值,ref属性的值应为配置文件中配置的Bean的id
public class AccountServiceImpl implements IAccountService {
//如果是经常变化的数据,并不适用于注入的方式
private String name;
private Integer age;
private Date birthday;
public AccountServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public void saveAccount() {
System.out.println(name+","+age+","+birthday);
}
}
<!-- 使用Date类的无参构造函数创建Date对象 -->
<bean id="now" class="java.util.Date" scope="prototype"></bean>
<bean id="accountService" class="cn.maoritian.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="myname"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<!-- birthday字段为已经注册的bean对象,其id为now -->
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
2.使用set方法注入(更常用)
在类中提供需要注入成员属性的set方法,创建对象只调用要赋值属性的set方法.
涉及的标签: <property>,用来定义要调用set方法的成员。 其主要属性可大致分为两类:
- 指定要调用set方法赋值的成员字段
- name:要调用set方法赋值的成员字段
- 指定赋给字段的值
- value: 给基本数据类型和String类型赋值
- ref: 给其它Bean类型的字段赋值,ref属性的值应为配置文件中配置的Bean的id
public class AccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public void saveAccount() {
System.out.println(name+","+age+","+birthday);
}
}
<!-- 使用Date类的无参构造函数创建Date对象 -->
<bean id="now" class="java.util.Date" scope="prototype"></bean>
<bean id="accountService" class="cn.maoritian.service.impl.AccountServiceImpl">
<property name="name" value="myname"></property>
<property name="age" value="21"></property>
<!-- birthday字段为已经注册的bean对象,其id为now -->
<property name="birthday" ref="now"></property>
</bean>
3. 注入集合字段
集合字段及其对应的标签按照集合的结构分为两类: 相同结构的集合标签之间可以互相替换.
- 只有键的结构:
- 数组字段: <array>标签表示集合,<value>标签表示集合内的成员.
- List字段: <list>标签表示集合,<value>标签表示集合内的成员.
- Set字段: <set>标签表示集合,<value>标签表示集合内的成员.
- 其中<array>,<list>,<set>标签之间可以互相替换使用.
- 键值对的结构:
- Map字段: <map>标签表示集合,<entry>标签表示集合内的键值对,其key属性表示键,value属性表示值.
- Properties字段: <props>标签表示集合,<prop>标签表示键值对,其key属性表示键,标签内的内容表示值.
- 其中<map>,<props>标签之间,<entry>,<prop>标签之间可以互相替换使用.
下面使用set方法注入各种集合字段
public class AccountServiceImpl implements IAccountService {
// 集合字段
private String[] myArray;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
// 集合字段的set方法
public void setMyStrs(String[] myArray) {
this.myArray = myArray;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
@Override
public void saveAccount() {
System.out.println(Arrays.toString(myArray));
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(myProps);
}
}
<bean id="accountService" class="cn.maoritian.service.impl.AccountServiceImpl3">
<property name="myStrs">
<array>
<value>value1</value>
<value>value2</value>
<value>value3</value>
</array>
</property>
<property name="myList">
<list>
<value>value1</value>
<value>value2</value>
<value>value3</value>
</list>
</property>
<property name="mySet">
<set>
<value>value1</value>
<value>value2</value>
<value>value3</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="key1" value="value1"></entry>
<entry key="key2">
<value>value2</value>
</entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
</props>
</property>
</bean>
使用注解实现IOC
使用注解实现IOC,要将注解写在类的定义中
常用注解
用于创建对象的注解
这些注解的作用相当于bean.xml
中的<bean>
标签,该注解告知Spring要为这个类创建bean。
- @Component: 把当前类对象存入spring容器中,其属性如下:
- value: 用于指定当前类的id,也就是该类的唯一标识,不写时默认值是当前类名,且首字母改小写。
- 注解表明该类会作为组件类,会告知Spring要创建类的bean。
@Component("accountService") public class AccountServiceImpl implements IAccountService { } @Component("accountService")注解相当于原来的: <bean id="accountService" class="com.lwl.service.impl.AccountServiceImpl"></bean>
- @Controller: 将当前表现层对象存入spring容器中
- @Service: 将当前业务层对象存入spring容器中
- @Repository: 将当前持久层对象存入spring容器中
@Controller
,@Service
,@Repository
注解的作用和属性与@Component
是一模一样的,可以相互替代,它们的作用是使三层对象的分别更加清晰.
使用xml启动组件扫描:使用注解方式时,需要建立一个xml文件,默认组件扫描是不启用的,需要我们通过xml文件显示配置一下Spring,好让程序执行时去扫描这个包中的注解从而命令它去寻找带有@Component注解的类,为其创建bean。
<?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:contex="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--告知Spring在创建容器时要扫描的包,配置所需要的标签不是在bean的约束中,而是在一个名称为
context名称空间和约束中-->
<contex:component-scan base-package="com.lwl"></contex:component-scan>
</beans>
另外一种启动组件扫描的方法:@ComponentScan默认会扫描与配置类相同的包,以及该包下面的子包
用于注入数据的注解
这些注解的作用相当于bean.xml中的<property>标签,表明类中需要要注入的成员变量或方法。
- @Autowired: 自动按照成员变量类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。
- 注入过程
- 当spring容器中有且只有一个对象的类型与要注入的类型相同时,注入该对象。
- 当spring容器中有多个对象类型与要注入的类型相同时,使用要注入的变量名作为bean的id,在spring容器查找键,找到相同的则注入该对象,找不到则报错。
- 出现位置: 既可以在变量上,也可以在方法上
- 细节: 使用注解注入时,set方法可以省略
- 注入过程
- @Qualifier: 在自动按照类型注入的基础之上,再按照bean的id注入。用来指定要注入bean的id
- 出现位置: 既可以在成员变量上,也可以在成员方法上。注入成员变量时不能独立使用,必须和@Autowire一起使用; 注入成员方法时可以独立使用.
- 属性:
- value: 指定bean的id
- @Resource: 直接按照bean的id注入,它可以独立使用.独立使用时相当于同时使用@Autowired和@Qualifier两个注解(常用)。
- 属性:
- name: 指定bean的id
- 属性:
- @Value: 注入基本数据类型和String类型数据。(上面的三个注解无法实现注入基本数据类型和String类型)
- 属性:
- value: 用于指定数据的值,可以使用el表达式(${表达式}),Spring中的el表达式成为SpEL。
- 属性:
用于改变bean作用范围的注解
这些注解的作用相当于bean.xml中的<bean>标签的scope属性.
- @Scope: 指定bean的作用范围
- 属性: value: 用于指定作用范围的取值,"singleton","prototype","request","session","globalsession",不写value默认情况下是单例的
和生命周期相关的注解
他们的作用和在bean标签中使用init-method和destory-method的作用是一样的
@PostConstruct
: 用于指定初始化方法@PreDestroy
: 用于指定销毁方法
实例: 使用纯注解配置实现数据库CRUD
先使用xml文件的方式实现一遍:
1. 项目结构: 其中包com.lwl.service存放业务代码,dao层选用DBUtils
和c3p0
.
2. 包com.lwl.service存放业务代码,其中dao层实现类和service层实现类的代码如下:
service层:
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 insertAccount(Account account) {
accountDao.insertAccount(account);
}
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
public void deleteAccount(Integer id) {
accountDao.deleteAccount(id);
}
}
dao层:
public class AccountDaoImpl implements IAccountDao {
private QueryRunner runner; //核心对象
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAllAccount() {
try {
return runner.query("select * from account1", new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Account findAccountById(Integer id) {
try {
return runner.query("select * from account1 where id = ?",new BeanHandler<Account>(Account.class),id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void insertAccount(Account account) {
try {
runner.update("insert into account1(name,money)values(?,?)",account.getName(),account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void updateAccount(Account account) {
try {
runner.update("update account1 set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void deleteAccount(Integer id) {
try {
runner.update("delete from account1 where id = ?",id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
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.lwl.service.impl.AccountServiceImpl">
<!--注入Dao对象-->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置Dao对象-->
<bean id="accountDao" class="com.lwl.dao.impl.AccountDaoImpl">
<!--注入QueryRunner对象的实例-->
<property name="runner" ref="runner"></property>
</bean>
<!--配置QueryRunner-->
<bean id="runner" 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/test"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>
测试类:
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(3);
System.out.println(account);
}
@Test
public void testInsert(){
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.执行方法
Account account = new Account();
account.setMoney(1200.0);
account.setName("张张");
//as.insertAccount(account);
}
@Test
public void testUpdate(){
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.执行方法
Account account = new Account();
account.setMoney(1200.0);
account.setName("ddd");
account.setId(6);
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(7);
}
}
部分使用注解方式实现:
dao层实现类和service层实现类的代码如下:
service层
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
}
dao层
@Repository("AccountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private QueryRunner runner; //核心对象
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAllAccount() {
try {
return runner.query("select * from account1", new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--告知Spring在创建容器的时候要扫描的包-->
<context:component-scan base-package="com.lwl"></context:component-scan>
<!--配置QueryRunner-->
<bean id="runner" 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/test"></property>
<property name="user" value="root"></property>
<property name="password" value="1022"></property>
</bean>
</beans>
测试类代码不变。
完全使用注解的方式
@Configuration: 用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解,获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class).
细节:当配置类作为AnnotationApplicationContext对象创建的参数时,该注解可以不写。
@ComponentScan: 指定spring在初始化容器时要扫描的包,作用和bean.xml 文件中<context:component-scan base-package="要扫描的包名"/>是一样的。 其属性如下:
- basePackages: 用于指定要扫描的包,是value属性的别名
@Bean: 该注解只能写在方法上,表明使用此方法创建一个对象,并放入spring容器中,其属性如下:
- name: 指定此方法创建出的bean对象的id。
- 细节: 使用注解配置方法时,如果方法有参数,Spring框架会到容器中查找有没有可用的bean对象,查找的方式与@Autowired注解时一样的。
@PropertySource: 用于加载properties配置文件中的配置。例如配置数据源时,可以把连接数据库的信息写到properties配置文件中,就可以使用此注解指定properties配置文件的位置,其属性如下:
- value: 用于指定properties文件位置.如果是在类路径下,需要写上"classpath:"
@Import: 用于导入其他配置类.当我们使用@Import注解之后,有@Import注解的类就是父配置类,而导入的都是子配置类. 其属性如下:
- value: 用于指定其他配置类的字节码。
dao层实现类和service层实现类的代码不变,测试类的方法:
public class AccountServiceTest {
@Test
public void testFindAll(){
//1.获取容器
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.执行方法
List<Account> accounts = as.findAllAccount();
for (Account account : accounts) {
System.out.println(account);
}
}
}
连接数据库的配置类:
@PropertySource("classpath:jdbcConfig.properties")
@Configuration
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
//用于创建一个QueryRunner对象
@Bean(name = "runner")
@Scope("prototype")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
//创建数据源对象
@Bean(name = "dataSource")
public DataSource creatDataSource(){
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
另一个空的配置类:
@Configuration
@ComponentScan(basePackages = "com.lwl")
public class SpringConfiguration {
}
Spring整合junit
1.导入Spring整合junit的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
2.使用junit提供的@Runwith注解把原有的main方法,替换成spring提供的
3.告知Spring的xml,spring和ioc创建是基于xml还是注解,并且说明位置:
@contextConfigration
参数:Location:指定xml文件的位置,加上classpath关键字,表示在类路径下
classes:指定注解类所在位置
注意:当使用Spring5.x版本的时候,要求junit的版本必须是4.12及以上
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testFindAll(){
//3.执行方法
List<Account> accounts = as.findAllAccount();
for (Account account : accounts) {
System.out.println(account);
}
}
}