信誉好的企业网站开发,网站建设与管理试卷 判断题,长沙包装设计公司排名,花生壳做wordpress目录Spring IOCIOC是什么IOC可以做什么依赖注入IOC和DIIOC容器Bean配置IOC容器spring ioc 依赖XML配置实例化容器使用容器xml配置详解spring对bean的管理1、创建bean的三种方式2、bean对象的作用范围3、bean对象的生命周期构造函数依赖注入Setter方法依赖注入注解配置使用xml和…
目录Spring IOCIOC是什么IOC可以做什么依赖注入IOC和DIIOC容器Bean配置IOC容器spring ioc 依赖XML配置实例化容器使用容器xml配置详解spring对bean的管理1、创建bean的三种方式2、bean对象的作用范围3、bean对象的生命周期构造函数依赖注入Setter方法依赖注入注解配置使用xml和注解配置xml配置Component、Controller、Service、Repository用于注入数据的注解AutowiredQualifierResource关于Autowired、Qualifier、ResourceValue用于指定作用范围的注解 Scope与生命周期相关的注解 PreDestroy、PostConstruct使用注解配置使用ConfigurationComponentScanBeanImportPropertySource注解配置案例Maven 依赖domain配置类主配置类子配置类jdbc.properties持久层IAccountDaoIAccountDaoimpl业务层IAccountServiceIAccountServiceimpl测试类Spring IOC
IOC是什么
IOC是 Inversion of Control 的缩写即控制反转。
上层模块不应该依赖于下层模块它们共同依赖于一个抽象抽象不能依赖于具体实现具体实现依赖于抽象 IoC 不是什么技术而是一种设计思想。在 Java 开发中IoC 意味着将你设计好的对象交给容器控制而不是传统的在你的对象内部直接控制。如何理解 Ioc 呢理解 Ioc 的关键是要明确“谁控制谁控制什么为何是反转有反转就应该有正转了哪些方面反转了”那我们来深入分析一下 谁控制谁控制什么传统 JavaSE 程序设计我们直接在对象内部通过 new 进行创建对象是程序主动去创建依赖对象而 IoC 是有专门一个容器来创建这些对象即由 IoC 容器来控制对象的创建谁控制谁当然是 IoC 容器控制了对象控制什么那就是主要控制了外部资源获取不只是对象包括比如文件等。 为何是反转哪些方面反转了有反转就有正转传统应用程序是由我们自己在对象中主动控制去直接获取依赖对象也就是正转而反转则是由容器来帮忙创建及注入依赖对象为何是反转因为由容器帮我们查找及注入依赖对象对象只是被动的接受依赖对象所以是反转哪些方面反转了依赖对象的获取被反转了。 ----转载自github的一篇博客。 未使用IOC容器前
使用IOC容器后
IOC可以做什么 IoC 不是一种技术只是一种思想一个重要的面向对象编程的法则它能指导我们如何设计出松耦合、更优良的程序。传统应用程序都是由我们在类内部主动创建依赖对象从而导致类与类之间高耦合难于测试有了 IoC 容器后把创建和查找依赖对象的控制权交给了容器由容器进行注入组合对象所以对象与对象之间是松散耦合这样也方便测试利于功能复用更重要的是使得程序的整个体系结构变得非常灵活。 其实 IoC 对编程带来的最大改变不是从代码上而是从思想上发生了“主从换位”的变化。应用程序原本是老大要获取什么资源都是主动出击但是在 IoC/DI 思想中应用程序就变成被动的了被动的等待 IoC 容器来创建并注入它所需要的资源了。 IoC 很好的体现了面向对象设计法则之一—— 好莱坞法则“别找我们我们找你”即由 IoC 容器帮对象找相应的依赖对象并注入而不是由对象主动去找。 ----转载自github的一篇博客。 依赖注入 DI是 Dependency Injection 的缩写即依赖注入。 依赖注入是 IoC 的最常见形式。 容器全权负责的组件的装配它会把符合依赖关系的对象通过 JavaBean 属性或者构造函数传递给需要的对象。 DI 是组件之间依赖关系由容器在运行期决定形象的说即由容器动态的将某个依赖关系注入到组件之中。依赖注入的目的并非为软件系统带来更多功能而是为了提升组件重用的频率并为系统搭建一个灵活、可扩展的平台。通过依赖注入机制我们只需要通过简单的配置而无需任何代码就可指定目标需要的资源完成自身的业务逻辑而不需要关心具体的资源来自何处由谁实现。 理解 DI 的关键是“谁依赖谁为什么需要依赖谁注入谁注入了什么”那我们来深入分析一下 谁依赖于谁当然是应用程序依赖于 IoC 容器 为什么需要依赖应用程序需要 IoC 容器来提供对象需要的外部资源 谁注入谁很明显是 IoC 容器注入应用程序某个对象应用程序依赖的对象 注入了什么就是注入某个对象所需要的外部资源包括对象、资源、常量数据。 IOC和DI
其实它们是同一个概念的不同角度描述由于控制反转概念比较含糊可能只是理解为容器控制对象这一个层面很难让人想到谁来维护对象关系所以 2004 年大师级人物 Martin Fowler 又给出了一个新的名字“依赖注入”相对 IoC 而言“依赖注入”明确描述了“被注入对象依赖 IoC 容器配置依赖对象”。
IOC容器
其实它们是同一个概念的不同角度描述由于控制反转概念比较含糊可能只是理解为容器控制对象这一个层面很难让人想到谁来维护对象关系所以 2004 年大师级人物 Martin Fowler 又给出了一个新的名字“依赖注入”相对 IoC 而言“依赖注入”明确描述了“被注入对象依赖 IoC 容器配置依赖对象”。
Bean JavaBean 是一种 JAVA 语言写成的可重用组件。为写成 JavaBean类必须是具体的和公共的并且具有无参数的构造器。JavaBean 对外部通过提供 getter / setter 方法来访问其成员。 由 IoC 容器管理的那些组成你应用程序的对象我们就叫它 Bean。Bean 就是由 Spring 容器初始化、装配及管理的对象除此之外bean 就与应用程序中的其他对象没有什么区别了。那 IoC 怎样确定如何实例化 Bean、管理 Bean 之间的依赖关系以及管理 Bean 呢这就需要配置元数据在 Spring 中由 BeanDefinition 代表后边会详细介绍配置元数据指定如何实例化 Bean、如何组装 Bean 等。
配置IOC容器
IoC 容器的配置有三种方式
基于 xml 配置基于注解配置基于 Java 配置
spring ioc 依赖
首先我们先导入maven的spring ioc依赖
dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.2.0.RELEASE/version/dependency
/dependenciesXML配置
首先创建一个spring ioc 的xml配置文件
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
bean idUserService classcom.service.Impl.IUserServiceimpl /beanimport resourcebean2.xml /
/beans标签说明
beans 是 Spring 配置文件的根节点。bean用来定义一个 JavaBean。id 属性是它的标识在文件中必须唯一class 属性是它关联的类。import / 用来导入其他配置文件的 Bean 定义。这是为了加载多个配置文件。
实例化容器
核心容器的两个接口
ApplicationContext 单例对象适用 它在构建核心容器时创建对象采取的策略是采用立即加载的方式。也就是说只要一读取完配置文件马上就创建配置文件中配置的对象BeanFactory 多例对象适用 它在构建核心容器时创建对象采取的策略是采用延时加载的方式。也就是说只有通过id获取对象时才真正的创建对象
实例化容器的过程 定位资源XML 配置文件 读取配置信息(Resource) 转化为 Spring 可识别的数据形式BeanDefinition。
ApplicationContext context new ClassPathXmlApplicationContext(new String[] {bean1.xml, bean2.xml});组合 xml 配置文件 配置的 Bean 功能各不相同都放在一个 xml 文件中不便管理。 Java 设计模式讲究职责单一原则。配置其实也是如此功能不同的 JavaBean 应该被组织在不同的 xml 文件中。然后使用 import 标签把它们统一导入。
ApplicationContext是一个接口 有常用的三个实现类
1、ClassPathXmlApplicationContext 它可以加载类路径下的配置文件要求配置文件必须要在类路径下。ApplicationContext context new ClassPathXmlApplicationContext(new String[] {bean1.xml, bean2.xml}); 2、FileSystemXmlApplicationContext 它可以加载磁盘任意路径下的配置文件(前提是必须有访问权限) 3、AnnotationConfigApplicationContext 它用于读取注解创建容器的。new AnnotationConfigApplicationContext(Class? ... config);ApplicationContext applicationContext new AnnotationConfigApplicationContext(SpringConfiguration.class);
使用容器
使用容器的方式就是通过getBean获取 IoC 容器中的 JavaBean。 第一种 getBean(String id) 第二种 getBean(String id, Class? object) ApplicationContext applicationContext new ClassPathXmlApplicationContext(bean.xml);IUserService iUserService1 (IUserServiceimpl) applicationContext.getBean(IUserService);IUserService iUserService2 applicationContext.getBean(IUserService, IUserService.class);xml配置详解
spring对bean的管理
1、创建bean的三种方式 使用默认构造函数(无参)创建对象 在使用spring配置文件中使用bean标签时配以id和class属性之后并且没有其他属性和标签时采用的就是默认构造函数创建bean对象此时如果类中没有默认构造函数则对象无法创建。 使用普通工厂中的方法创建对象使用某个类中的方法创建对象并存入spring容器 id为需要创建的对象的id需要为目标对象配beanfactory-bean属性为工厂类的id先给工厂类配beanfactory-method属性为工厂类创建目标对象的方法该方法不能为static 使用普通工厂中的静态(static)方法创建对象使用某个类中的静态(static)方法创建对象并存入spring容器 class属性为工厂的全限定类名这也代表着bean创建的id对应的是工厂类, 但是设置factory-method属性为工厂类创建目标对象的静态(static)方法,这样就会默认调用该方法返回的就是目标对象。
使用默认构造函数(无参)创建对象
bean idUserService classcom.service.Impl.IUserServiceimpl /bean使用普通工厂中的方法创建对象 bean idinstanceFactory classcom.factory.InstanceFactory/beanbean idIUserService factory-beaninstanceFactory factory-methodgetIUserSevice /bean使用普通工厂中的静态(static)方法创建对象
bean idIUserService classcom.factory.staticFactory factory-methodgetIUserSevice/bean2、bean对象的作用范围
默认情况下 bean对象只创建一次————————单例对象。我们可以通过bean标签的scope属性设置bean对象的作用范围。 scope属性
作用用于指定bean的作用范围取值 singleton单例的默认值prototype多例的request作用于web应用的请求范围session作用于web应用的会话范围global-session作用于集群环境的会话范围全局会话范围。当不是集群环境时与session等同
3、bean对象的生命周期
1、单例对象
出生当容器创建时对象出生活着只要容器没有销毁对象一直活着死亡当容器销毁对象跟着死亡总结单例对象的生命周期与容器相同
2、多例对象
出生当我们使用对象时spring框架为我们创建活着对象只要在使用过程中就一直活着死亡当对象长时间未使用且没有其他对象引用时java垃圾回收机制会自动回收对象 我们可以借助bean标签的init-method属性调用初始化方法和destroy-method调用销毁方法 //init 和 destroy 是类IUserServiceimpl 定义的方法
bean idIUserService classcom.service.Impl.IUserServiceimpl scopesingleton init-methodinit destroy-methoddestroy/bean构造函数依赖注入
使用的标签constructor-arg/constructor-arg标签出现的位置bean标签内部标签的属性 name用于指定给构造函数中指定名称的参数赋值index用于指定需要注入的数据给构造函数中指定索引位置的参数赋值type用于指定需要注入的数据类型该数据类型也是构造函数中某个或者某些参数的类型value:用于指定基本类型和String类型的数据ref用于指定其他bean类型数据。它指的就是在Spring的ioc核心容器中出现过的bean对象
bean idIUserService classcom.service.Impl.IUserServiceimpl constructor-arg namename valuexiuyuandashen/constructor-argconstructor-arg nameage value18/constructor-argconstructor-arg namebirthday refbirthday/constructor-arg
/bean
bean idbirthday classjava.util.Date/bean对应的构造函数 public IUserServiceimpl(String name, Date birthday, Integer age) {this.name name;this.birthday birthday;this.age age;}Setter方法依赖注入
首先该对象得Setter方法
使用的标签property标签出现的位置bean标签内部标签的属性 name用于指定给set方法名称value:用于指定基本类型和String类型的数据ref用于指定其他bean类型数据。它指的就是在Spring的ioc核心容器中出现过的bean对象
复杂类型的注入或者集合结构类型的注入
使用的标签property标签出现的位置bean标签内部用于给list结构集合注入的标签 array list set注意结构相同标签可以互换 用于给map结构集合注入的标签 map props注意结构相同标签可以互换
bean idfuzajiegou classcom.domain.fuzajiegouproperty namemyArrarrayvaluehello/valuevalueworld/valuevaluejava/valuevaluejavaScript/value/array/propertyproperty namemyListlistvaluelist——a/valuevaluelist——b/valuevaluelist——c/valuevaluelist——e/valuevaluelist——f/value/list/propertyproperty namemySetsetvalueset——a/valuevalueset——b/valuevalueset——c/valuevalueset——e/valuevalueset——f/value/set/propertyproperty namemyMapmapentry keymap1 value1/entryentry keymap2 value2/entryentry keymap3 value3/entryentry keymap4value4/value/entry/map/propertyproperty namemyPropropsprop keyProperties11/propprop keyProperties22/propprop keyProperties33/propprop keyProperties44/prop/props/property/bean对应的bean类
public class fuzajiegou {private String[] myArr;private List myList;private Set mySet;private Properties myPro;private MapString,String myMap;public void setMyArr(String[] myArr) {this.myArr myArr;}public void setMyList(List myList) {this.myList myList;}public void setMySet(Set mySet) {this.mySet mySet;}public void setMyPro(Properties myPro) {this.myPro myPro;}public void setMyMap(MapString, String myMap) {this.myMap myMap;}
}public static void main(String[] args) {ApplicationContext applicationContext new ClassPathXmlApplicationContext(bean.xml);final fuzajiegou fuzajiegou applicationContext.getBean(fuzajiegou, fuzajiegou.class);System.out.println(fuzajiegou);}注解配置
使用xml和注解配置
xml配置
Spring 默认是不启用注解的。如果想使用注解需要先在 xml 中启动注解。 启动方式在 xml 中加入一个标签很简单吧。
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdcontext:component-scan base-packagecom//beans标签说明
context:component-scan base-packagecom/ 标签 属性base-package 表示扫描的位置
Component、Controller、Service、Repository
Component 注解
作用用于将当前类对象存入spring容器属性 value用于指定bean的id。当我们不写时默认为当前类名且首字母小写
Controller、Service、Repository 注解
以上这三种的作用和属性与Component一样,用这三种注解使三层结构对象更清晰。Controller 表示表现层Service 表示业务层Repository 表示持久层
用于注入数据的注解
他们的作用就和xml配置文件中的bean标签中写一个property/property标签的作用是一样的
Autowired
Autowired
作用自动按照类型注入。只需要容器中有唯一的一个bean对象类型和要注入的变量匹配就可以注入成功。 如果ioc容器中没有任何bean的类型与要注入的变量类型匹配则报错。细节 如果ioc容器中有多个类型匹配时先按照变量类型找出所有然后将变量名称作为id进行查找对应的对象/值。
Qualifier
作用在按照类型注入的基础(Autowired)上再按照名称注入。细节 它在给类成员注入时不能单独使用。但是在给方法参数注入时可以单独使用。 属性 value用于指定注入bean的id。
Resource
作用直接按照bean的id注入。它可以单独使用。属性 name用于指定bean的id
关于Autowired、Qualifier、Resource
以上三种注解(Autowired、Qualifier、Resource)都只能注入其他bean类型的数据而基本类型和String类型无法使用上述三种注解实现
Value
作用用于注入基本类型和String类型的数据属性 value用于指定数据的值。它可以使用spring中的el表达式spEL。spEl的写法${表达式}
用于指定作用范围的注解 Scope
他们的作用就和bean标签中使用scope属性实现的功能一致。 Scope
作用用于指定bean的作用范围属性 value指定范围的取值。常用取值singleton单例prototype多例…
与生命周期相关的注解 PreDestroy、PostConstruct
他们的作用就和bean标签中使用scope属性实现的功能一致。 PreDestroy
作用用于指定销毁方法 PostConstruct作用用于指定初始化方法
使用注解配置
首先使用注解配置需要解决xml配置文件中注解对context:component-scan base-packagecom/的依赖 我们可以使用ComponentScan注解来代替context:component-scan base-packagecom/
使用Configuration
Configuration
作用指定当前类是一个配置类细节当该配置类作为AnnotationConfigApplicationContext对象创建的参数时该注解可以不写。
ComponentScan
ComponentScan
作用用于通过注解指定spring在创建容器时要扫描的包属性 basePackages它和value的作用是一致的都是用于指定创建容器时需要扫描的包,我们使用此注解就等同于在xml配置了context:component-scan base-package/.value与basePackages一样以上两个属性的值都是一个String[]数组。数组的每个值表示要扫描的包名。 细节 被扫描的包中的类需要被扫描到的话需要加上 Configuration 注解。
Bean
作用用于将当前方法的返回值作为bean对象存入spring的IOC容器中。属性 name用于指定bean的id。当不写name时默认值为当前方法的名称。 细节 当我们使用注解配置方法时如果方法有参数spring框架会去容器中查找有没有可用的bean对象。 查找的方式和 Autowired注解的作用是一致的。使用Bean注解默认是单例的需要加个Scope(prototype)指明为多例对象
Import
作用用于导入其他的配置类。属性 Class []value该数组的值就是需要引入的其他配置类的字节码文件。 细节当类被其他类用Import 注解引入时该类就不需要用 Configuration来指明是配置类.相当于使用Import的配置类为父配置类引入的类就是子配置类.在使用AnnotationConfigApplicationContext对象创建IOC容器时只需要引入父配置类即可。
PropertySource
作用用于指定Properties文件的位置属性 String []value指定文件的路径和名称。关键词classpath 表示类路径下。例如 PropertySource(classpath:jdbc.properties)
注解配置案例
这是一个单表CRUD的案例。 项目结构
Maven 依赖
dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.2.5.RELEASE/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.14/version/dependencydependencygroupIdcommons-dbutils/groupIdartifactIdcommons-dbutils/artifactIdversion1.6/version/dependencydependencygroupIdc3p0/groupIdartifactIdc3p0/artifactIdversion0.9.1/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13/versionscopetest/scope/dependency/dependencies
domain
package com.domain;public class Account {private Integer id;private String name;private double money;public Account() {}public Account(String name, double money) {this.name name;this.money money;}Overridepublic String toString() {return Account{ id id , name name \ , money 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 double getMoney() {return money;}public void setMoney(double money) {this.money money;}
}
配置类
主配置类
package com.config;import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.*;import javax.sql.DataSource;
Configuration
ComponentScan(basePackages {com.Dao,com.service})
Import({jdbcConfig.class})
PropertySource({classpath:jdbc.properties})
public class SpringConfiguration {}子配置类
package com.config;import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;import javax.sql.DataSource;public class jdbcConfig {Value(${jdbc.driver})private String driver;Value(${jdbc.jdbcUrl})private String jdbcUrl;Value(${jdbc.username})private String username;Value(${jdbc.password})private String password;/*** dbUtils* param dataSource* return*/Bean(name runner)Scope(prototype)public QueryRunner createQueryRunner(DataSource dataSource) {return new QueryRunner(dataSource);}/*** 配置数据源* return*/Bean(name dataSource)public DataSource createDataSource() {try {ComboPooledDataSource ds new ComboPooledDataSource();ds.setDriverClass(driver);ds.setJdbcUrl(jdbcUrl);ds.setUser(username);ds.setPassword(password);return ds;} catch (Exception e) {throw new RuntimeException(e);}}}
jdbc.properties
jdbc.drivercom.mysql.cj.jdbc.Driver
jdbc.jdbcUrljdbc:mysql://localhost:3306/zlf?serverTimezoneUTC
jdbc.usernameroot
jdbc.passwordfeng10.10持久层
IAccountDao
package com.Dao;import com.domain.Account;import java.util.List;/***账户的持久层接口**/
public interface IAccountDao {/*** 查询所有* return*/ListAccount selAll();/*** 查询一个* param id* return*/Account findAccountbyid(int id);/*** 保存* param account*/void saveAccount(Account account);/***更新* param account*/void updateAccount(Account account);/*** 删除* param id*/void deleteAccount(int id) throws Exception;
}
IAccountDaoimpl
package com.Dao.Impl;import com.Dao.IAccountDao;
import com.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;import java.util.List;/*** 账户的持久层实现类*/
Repository(IAccountDao)
public class IAccountDaoimpl implements IAccountDao {Autowiredprivate QueryRunner runner;public void setRunner(QueryRunner runner) {this.runner runner;}public ListAccount selAll() {try{return runner.query(select *from account,new BeanListHandlerAccount(Account.class));}catch (Exception e){throw new RuntimeException(e);}}public Account findAccountbyid(int id) {try{return runner.query(select *from account where id?,new BeanHandlerAccount(Account.class),id);}catch (Exception e){throw new RuntimeException(e);}}public void saveAccount(Account account) {try{runner.update(insert into account(name,money) values(?,?) ,account.getName(),account.getMoney());}catch (Exception e){throw new RuntimeException(e);}}public void updateAccount(Account account) {try{runner.update(update account set name? ,money? where id? ,account.getName(),account.getMoney(),account.getId());}catch (Exception e){throw new RuntimeException(e);}}public void deleteAccount(int id) {try {runner.update(delete from account where id?,id);}catch (Exception e){throw new RuntimeException(e);}}
}
业务层
IAccountService
package com.service;import com.domain.Account;import java.util.List;/*** 账户的业务层接口*/
public interface IAccountService {/*** 查询所有* return*/ListAccount selAll();/*** 查询一个* param id* return*/Account findAccountbyid(int id);/*** 保存* param account*/void saveAccount(Account account);/***更新* param account*/void updateAccount(Account account);/*** 删除* param id*/void deleteAccount(int id) throws Exception;}
IAccountServiceimpl
package com.service.Impl;import com.Dao.IAccountDao;
import com.Dao.Impl.IAccountDaoimpl;
import com.domain.Account;
import com.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
Service(IAccountService)
public class IAccountServiceimpl implements IAccountService {Autowiredprivate IAccountDao iAccountDao;public ListAccount selAll() {return iAccountDao.selAll();}public Account findAccountbyid(int id) {return iAccountDao.findAccountbyid(id);}public void saveAccount(Account account) {iAccountDao.saveAccount(account);}public void updateAccount(Account account) {iAccountDao.updateAccount(account);}public void deleteAccount(int id) throws Exception {iAccountDao.deleteAccount(id);}public void setiAccountDao(IAccountDaoimpl iAccountDao) {this.iAccountDaoiAccountDao;}
}
测试类
package com.test;import com.config.SpringConfiguration;
import com.domain.Account;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.service.IAccountService;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.sql.DataSource;
import java.util.List;public class accountServiceTest {IAccountService iAccountService null;Beforepublic void init() throws Exception {final ApplicationContext applicationContext new AnnotationConfigApplicationContext(SpringConfiguration.class);iAccountService applicationContext.getBean(IAccountService, IAccountService.class);}/*** 查询所有*/Testpublic void findAll() {System.out.println(iAccountService.selAll());}/*** 查询一位*/Testpublic void findOne() {System.out.println(iAccountService.findAccountbyid(2));}/*** 保存*/Testpublic void TestSave() {iAccountService.saveAccount(new Account(小明, 1002.5));}/*** 更新*/Testpublic void TestUpadata() {final Account account iAccountService.findAccountbyid(4);account.setMoney(100002.5);iAccountService.updateAccount(account);}/*** 删除** throws Exception*/Testpublic void Testdalete() throws Exception {iAccountService.deleteAccount(4);}}