Spring IOC

Spring_IoC

Spring的优势

方便解耦,简化开发:通过Soring提供的IoC容器,可以将对象间的依赖关系交给Spring来控制,避免硬编码造成的过度耦合。用户可以不必再专注于单例模式类、属性文件解析等底层的需求代码,可以更专注于上层应用。

AOP编程:面向切面编程,面向对象编程(OOP)中不容易解决的问题可以用AOP方便实现。

声明式事务的支持:通过声明方式灵活的进行事务的管理,提高开发效率。

​ 程序的耦合:指模块间的关联程度的度量。耦合的强弱取决于模块间接口的复杂性、调用模块的方式以及通过界面传送数据的多少。模块间的耦合度指模块之间的依赖关系,包括控制关系、调用关系、数据传递关系。

Spring的体系结构

在这里插入图片描述

核心容器(Core Container)

​ 核心容器由spring-corespring-beansspring-contextspring-context-supportspring-expression等模块组成,具体如下:

  • spring-core模块提供了框架的基本组成部分,包括 IoC和依赖注入功能。
  • spring-beans 模块提供 BeanFactory,工厂模式的实现,可以把配置和依赖从实际编码逻辑中解耦。
  • context 上下文模块,建立在核心和 Beans 模块的基础之上,它是访问定义和配置任何对象的媒介。ApplicationContext 接口是上下文模块的焦点。
  • spring-expression 模块提供了强大的表达式语言,用于在运行时查询和操作对象图。

数据访问(Data Access/Integration)

​ 数据访问/集成层包括 JDBC(Java Data Base Connectivity)、ORM(Object Relational Mapping)、OXM(Object XML Mapping)、**JMS **和 Transactions(Java Message Service) 模块。

  • JDBC:提供了一个 JDBC 的抽象层,减少了在开发过程中对数据库操作的编码。
  • ORM 模块:对流行的对象关系映射 API,包括 JPA、JDO、Hibernate和 iBatis 提供了的集成层。
  • OXM 模块:提供了一个支持对象/XML 映射的抽象层实现,如 JAXB、Castor、XMLBeans、JiBX 和 XStream。
  • JMS 模块:指 Java消息服务,包含的功能为生产和消费的信息。
  • Transactions 事务模块:支持编程和声明式事务管理实现特殊接口类,并为所有的 POJO。

Web

​ Spring 的 Web 层包括 Web、Servlet、Struts 和 Portlet 组件,具体介绍如下。

  • Web 模块:提供了基本的 Web 开发集成特性,例如多文件上传功能、使用的 Servlet 监听器的 IoC 容器初始化以及 Web 应用上下文。
  • Servlet模块:包括 Spring 模型—视图—控制器(MVC)实现 Web 应用程序。
  • Struts 模块:包含支持类内的 Spring 应用程序,集成了经典的 Struts Web 层。

其他

Spring的其他模块还有 AOP、Aspects、Instrumentation 以及 Test 模块,具体介绍如下。

  • AOP 提供了面向切面编程实现,允许定义方法拦截器和切入点,将代码按照功能进行分离,以降低耦合性。
  • Aspects 提供与 AspectJ 的集成,是一个功能强大且成熟的面向切面编程(AOP)框架。
  • Instrumentation 提供了类工具的支持和类加载器的实现,可以在特定的应用服务器中使用。
  • Test 支持 Spring 组件,使用 JUnit 或 TestNG 框架的测试。

基于XML的IOC

ApplicationContext三个常用实现类

在这里插入图片描述

ClassPathXmlApplicationContext:可以加载类路径下的配置文件,要求配置文件必须在类路径下

FileSystemXmlApplicationContext:可以加载磁盘任意路径下的配置文件(必须要有访问权限)

AnnotationConfigApplicationContext:读取注解,创建容器

BeanFactory和ApplicationContext的区别

​ ApplicationContext:构建核心容器的时,创建对象采取的是立即记载的机制。即只要读取创建文件就创建配置文件中的对象。更适用于单例模式,避免多线程情况下的线程安全问题。

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");
IAccountDao accountDao = applicationContext.getBean("accountDao", AccountDaoImpl.class);

​ BeanFactory:构建核心容器的时,创建对象采用的策略是延迟加载方式。即通过id获取对象的时候才去真正创建对象。更适用于多例模式。

Resource resource = new ClassPathResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
IAccountService accountService = (IAccountService) factory.getBean("accountService");
IAccountDao accountDao = factory.getBean("accountDao", AccountDaoImpl.class);

spring对bean的管理细节

bean对象的创建方式
默认构造函数创建

​ 在Spring的配置文件中适用bean标签,配以id和class属性之后,没有其他属性和标签时,采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。

<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl"></bean>
普通工厂中的方法创建对象

​ 使用某个类中的方法创建对象,并存入spring容器。下例中,通过factory-bean="instanceFacyory"工厂的factory-method="getAccountService"方法创建对象。

InstanceFactory.java

/**
 * 模拟工厂类
 * (该类存在于jar中,无法通过修改源码的方式来修改默认的构造方法)
 */
public class InstanceFactory {
  public IAccountService getAccountService() {
    return new AccountServiceImpl();
  }
}

xml中对应的bean对象配置

<bean id="instanceFacyory" class="com.xijianlv.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFacyory" factory-method="getAccountService"></bean>
使用工厂中的静态方法创建对象

​ 使用某个类中的静态方法创建对象,并存入spring容器。和上一种方式不同的是,由于是静态方法,所以省去了工厂对象的创建过程。直接使用工厂类对应的静态方法返回对应的bean对象。

StaticFactory.java

/**
 * 模拟工厂类
 * (该类存在于jar中,无法通过修改源码的方式来修改默认的构造方法)
 */
public class StaticFactory {
    public static IAccountService getAccountService() {
        return new AccountServiceImpl();
    }
}

xml中对应的bean对象配置

<bean id="accountService" class="com.xijianlv.factory.StaticFactory" factory-method="getAccountService"></bean>
bean对象的作用范围

​ 通过bean标签的scope属性来指定bean的作用范围。

singleton:单例的(默认值)

<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl" scope="singleton"></bean>

prototype:多例的

<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl" scope="prototype"></bean>

request:作用于web应用的请求范围

<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl" scope="request"></bean>

session:作用于web应用的回话范围

<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl" scope="session"></bean>

global-session:作用于集群环境的回话范围(全局会话范围),当前环境如果不是集群,它的作用就等同于session

<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl" scope="global-session"></bean>

​ global-session,集群环境下,一次会话中处理不同请求的可能是不同的机器。需要在多个机器间共享bean对象就需要存在全局session中。比如,登陆过程中的验证码信息,获取login.jsp的请求和验证登陆信息的请求不在同一个机器上处理,验证码信息对象的最用范围就需要是global-session,才能使每个机器都获取到验证码并有验证登陆信息的能力。

bean对象的生命周期
/**
 * 业务层实现类
 */
public class AccountServiceImpl implements IAccountService {
  public AccountServiceImpl(){ System.out.println("对象已创建");}
  public void save() {System.out.println("service save方法。");}
  public void init() {System.out.println("service init方法。");}
  public void desotry() {System.out.println("service desotry方法。");}
}
单例对象
<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl" scope="singleton" init-method="init"
      destroy-method="desotry"></bean>

在这里插入图片描述

出生:当容器创建的时候就创建对象。

存活:只要容器存在,对象就存活。

销毁:容器销毁,容器中的对象随之销毁。

单例对象的生命周期和容器相同

多例对象

在这里插入图片描述

出生:当使用对象时,spring对象才创建对象。

存活:对象只要在使用过程中就一直存活。

销毁:由于spring无从得知对象是否还会继续使用,所以当对象长时间不使用,且没有别当对象引用当时候,交给Java当垃圾回收去执行回收。

依赖注入(Dependency Injection DI)

​ IOC的作用是降低程序间的耦合(降低不是消除),所以程序中还会存在必要的依赖关系。依赖关系的管理就交给spring来维护。在当前类需要用到的其他类对象,由spring为我们提供,开发人员只需要在配置文件中说明即可。依赖关系的维护被称之为依赖注入。

注入的三类数据:基本类型和String、其他的bean类型(在配置文件或注解配置过的bean)、复杂类型/集合类型

注入的方式:构造函数注入、set方法注入、注解注入

构造函数注入

​ 在bean标签内部使用constructor-arg标签。

public class AccountServiceImpl {
  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;
  }}
<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl">
  <constructor-arg name="name" value="test_name"></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>

constructor-arg标签的属性

type:指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的数据类型

index:指定要注入的数据在构造函数中的索引位置。索引从0开始。

name:指定要注入的数据在构造函数中的参数名。

以上三个,确定给构造函数中的哪个参数赋值

value:用于提供基本类型和String类型的数据

ref:用于指定其他的bean类型数据。指的就是在Spring的IOC核型容器中出现过的bean对象

优势:

在获取bean对象的时候,注入数据数据是必须的,否则对象无法成功创建。

弊端:

改变了bean对象的实例化方式,非必须的数据也必须提供。

set方法注入<更常用的方式>

在bean标签内部使用property标签。

public class AccountServiceImpl2 {
    private String name;
    private Integer age;
    private Date birthday;
    public void setBirthday(Date birthday) {this.birthday = birthday;}
    public void setAge(Integer age) {this.age = age;}
    public void setName(String name) {this.name = name;}
}
<bean id="accountService2" class="com.xijianlv.service.impl.AccountServiceImpl2">
  <property name="name" value="test_name_set方法注入"></property>
  <property name="age" value="21"></property>
  <property name="birthday" ref="now"></property>
</bean>

标签的属性:

name:指定注入时所调用的set方法的名称

value:用于提供基本类型和String类型的数据

ref:用于指定其他的bean类型数据。指的就是在Spring的IOC核型容器中出现过的bean对象

优势:

​ 对象实例化没有明确的限制,可以只用默认构造函数。

弊端:

​ 不能确保必须有值的成员被成功赋值,有可能对应的set方法并未执行。

复杂类型/集合类型注入
public class AccountServiceImpl3 implements IAccountService {
  private String[] strs;
  private List<String> list;
  private Set<String> set;
  private Map<String, String> map;
  private Properties prop;
  public void setList(List<String> list) {this.list = list;}
  public void setMap(Map<String, String> map) {this.map = map;}
  public void setProp(Properties prop) {this.prop = prop;}
  public void setStrs(String[] strs) {this.strs = strs;}
  public void setSet(Set<String> set) {this.set = set;}
}
<bean id="accountService3" class="com.xijianlv.service.impl.AccountServiceImpl3">
  <property name="strs">
    <list>
      <value>strs_aaa</value>
      <value>strs_bbb</value>
    </list>
  </property>
  <property name="list">
    <array>
      <value>list_aaa</value>
      <value>list_bbb</value>
    </array>
  </property>
  <property name="set">
    <set>
      <value>set_aaa</value>
      <value>set_bbb</value>
    </set>
  </property>
  <property name="map">
    <map>
      <entry key="key_aaa" value="value_aaa"></entry>
      <entry key="key_bbb">
        <value>value_bbb</value>
      </entry>
    </map>
  </property>
  <property name="prop">
    <props>
      <prop key="key_aaa">aaa</prop>
      <prop key="key_bbb">bbb</prop>
    </props>
  </property>

用于给List结构集合注入的标签:list、array、set

用于给map结构集合注入的标签:map、prop

结构相同,标签可以互换

基于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);
Dao类
/**账户的持久层接口*/
public interface IAccountDao {
    /**查询所有*/
    List<Account> findAllAccount();
    /**查询一个*/
    Account findAccountById(Integer id);
    /**保存*/
    void saveAccount(Account account);
    /**更新*/
    void updateAccount(Account account);
    /**删除*/
    void deleteAccount(Integer id);
}
/**持久层实现类*/
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);
    }
  }
}
Service类
/**账户业务层接口*/
public interface IAccountService {
  /**查询所有*/
  List<Account> findAllAccount();
  /**查询一个*/
  Account findAccountById(Integer id);
  /**保存*/
  void saveAccount(Account account);
  /**更新*/
  void updateAccount(Account account);
  /**删除*/
  void deleteAccount(Integer id);
}
/**账户的业务层实现类*/
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);}
}
XML配置
<!--  业务层Service对象  -->
<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl">
  <!--    注入Dao对象    -->
  <property name="accountDao" ref="accountDao"></property>
</bean>
<!--  配置Dao对象  -->
<bean id="accountDao" class="com.xijianlv.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/spring?useSSL=false"></property>
  <property name="user" value="root"></property>
  <property name="password" value="welcome1"></property>
</bean>
测试
/**
 * 使用Junit单元测试,测试配置
 */
public class AccountServiceTest {


  @Test
  public void testFindAll() {
    //获取容器
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
    //业务层对象
    IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
    //执行方法
    List<Account> accountList = accountService.findAllAccount();
    for (Account account : accountList) {
      System.out.println(account);
    }
  }

  @Test
  public void testFindOne() {
    //获取容器
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
    //业务层对象
    IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
    //执行方法
    Account account = accountService.findAccountById(1);
    System.out.println(account);

  }

  @Test
  public void testSave() {
    Account account = new Account();
    account.setName("test");
    account.setMoney(123456f);
    //获取容器
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
    //业务层对象
    IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
    accountService.saveAccount(account);
  }

  @Test
  public void testUpdate() {
    //获取容器
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
    //业务层对象
    IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
    Account account = accountService.findAccountById(4);
    account.setMoney(654321f);
    accountService.updateAccount(account);
  }

  @Test
  public void testDelete() {
    //获取容器
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
    //业务层对象
    IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
    accountService.deleteAccount(4);
  }
}

基于注解的IOC

使用xml的配置配置的bean。

<bean id="accountService" class="com.xijianlv.service.impl.AccountServiceImpl" scope="" init-method=""
      destroy-method="">
  <property name="" value="" ref=""></property>
</bean>

在使用注解配置,要告知spring扫描的包

<!-- 告知spring在创建容器时要扫描的包 -->
<context:component-scan base-package="com.xijianlv"></context:component-scan>

用于创建对象的注解

​ 他们的作用和在XML配置文件中编写一个标签实现的功能是一样的。

Component:用于把当前类对象存入spring容器中。

@Component
public class AccountServiceImpl implements IAccountService {
  private IAccountDao accountDao = null;
  public void save() {accountDao.saveAccount();}}

@Repository
public class AccountDaoImpl implements IAccountDao {
    public void saveAccount() {System.out.println("保存了账户");}}

​ 属性:

  • value:用于指定beanid。默认值为当前类名,且首字母小写。

    Controller(常用于表现层)、Service(常用于业务层)、Repository(常用于持久层):这三个注解的作用和属性与Component是一模一样的,他们是spring框架提供的明确的三层架构使用的。

在这里插入图片描述

空指针异常是由于没有为AccountServiceImpl中的accountDao对象注入数据。

用于注入数据的注解

​ 他们的作用和在XML配置文件中的标签中写一个标签的作用是一样的。

Autowired:自动依据类型注入。主要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。可以出现在成员上,也可以在方法上等。

@Service
public class AccountServiceImpl implements IAccountService {
  @Autowired
  private IAccountDao accountDao = null;
  public void save() {accountDao.saveAccount();}}

​ 在使用注解注入的时候,set方法就不是必须的了。

​ Spring的IOC容器是一个Map,其中的key是bean的id,上述中的Dao和Servic两个bean的id分别是accountServiceImpl和accountDaoImpl,对应的map的value为对应类型的bean对象。

​ @Autowired直接查询Map的value,如果只有一个对象的类型与之匹配,直接注入;如果没有对象的类型与之匹配,注入失败;如果存在多个对象的类型与之匹配,在类型匹配的这些bean中,继续查找Map的key,如果变量名称和bean的id一致时,就注入一致的这个bean,如果没有没有和变量名称一致的bean,注入失败。

Qualifier:在按照类型注入的基础上,再按照名称注入。在给类成员注入时,不能单独使用,为方法参数注入时可以单独使用。

@Service
public class AccountServiceImpl implements IAccountService {
  @Autowired
  @Qualifier("accountDaoImpl")
  private IAccountDao accountDao = null;
  public void save() {accountDao.saveAccount();}
}

属性:

  • value:用于指定注入bean的id。

Resource:直接按照bean的id注入。可以独立使用。

@Service
public class AccountServiceImpl implements IAccountService {
  @Resource(name="accountDaoImpl")
  private IAccountDao accountDao = null;
  public void save() {accountDao.saveAccount();}}

属性:

  • name:用于指定bean的id。

上述注入都只能注入其他bean类型的数据,基本类型和String无法通过上述注解实现。

对于集合类型的注入,只能通过xml配置实现。

Value:用于注入基本类型和String类型。

@Service
public class AccountServiceImpl implements IAccountService {
    @Value("123")
    private int num;
    @Value("str")
    private String str;}

属性:

  • value:用于指定数据的值。可以使用Spring中的SpEL(Spring中的el表达式)。

用于改变作用范围的直接

​ 他们的作用和在标签中使用scope属性实现的功能是一样的。

Scope:用于指定bean的作用范围。

@Service
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
  @Resource(name = "accountDaoImpl")
  private IAccountDao accountDao = null;
  public void save() {accountDao.saveAccount();}}

属性:

  • value:指定范围的取值。默认情况下为singleton,单例。

生命周期相关的注解

​ 他们的作用和在标签中使用init-method和destroy-method属性实现的功能是一样的

PreDestroy:用于指定销毁方法。

PostConstruct:用于指定初始化方法。

@Service
public class AccountServiceImpl implements IAccountService {
  @Resource(name = "accountDaoImpl")
  private IAccountDao accountDao = null;
  @PostConstruct
  public void init(){System.out.println("初始化。。。");}
  @PreDestroy
  public void destory(){System.out.println("销毁。。。");}
  public void save() {accountDao.saveAccount();}}

基于注解的IOC案例

数据和测试代码使用的和基于XML配置的案例一致

XML配置
<!--  告知spring在创建容器时需要扫描的包  -->
<context:component-scan base-package="com.xijianlv"></context:component-scan>
<!--  配置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/spring?useSSL=false"></property>
  <property name="user" value="root"></property>
  <property name="password" value="welcome1"></property>
</bean>
Service实现类
/**账户的业务层实现类*/
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao 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);
    }
}
Dao实现类
/**持久层实现类*/
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    private 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);
        }
    }
}

Spring的其他注解

//@Configuration
@ComponentScan(basePackages = "com.xijianlv")
@Import(JdbcConfig.class)
public class SpringConfiguration {
}
public class JdbcConfig {
  @Bean(name = "queryRunner")
  @Scope("prototype")
  public QueryRunner createQueryRunner(DataSource dataSource) {
    return new QueryRunner(dataSource);
  }
  @Bean(name = "dataSource")
  public DataSource createDataSource() {
    try {
      ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
      comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
      comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring?useSSL=false");
      comboPooledDataSource.setUser("root");
      comboPooledDataSource.setPassword("welcome1");
      return comboPooledDataSource;
    } catch (PropertyVetoException e) {
      throw new RuntimeException(e);
    }
  }
}
Configuration

​ 指定当前类是一个配置类,当配置类作为AnnotationConfigApplicationContext创建对象的参数时,该注解可以不写。

ComponentScan

​ 用于通过注解指定spring在创建容器时要扫描的包。

属性:

  • value:它和basePackages的作用是一样的,都是用于指定创建容器时需要扫描的包。

等同于xml中的如下配置:

<context:component-scan base-package="com.xijianlv"></context:component-scan>
Bean

​ 用于把当前方法的返回值最为bean对象存入spring的ioc容器中。

属性:

  • name:用于指定bean的id。默认为当前方法的名称。

​ 当使用注解配置方法时,当方法有参数,spring会在容器中查找有没有可用的bean对象。查找的方法和Autowired注解的方式一致。

Import

​ 导入其他的配置类。

属性:

  • value:用于指定其他配置类的字节码。当使用Import注解之后,有Import注解的类就是主配置类,导入的都是子配置类。

​ 到这里,xml文件到所有配置都已经使用注解替换了。但是并没有比xml配置文件简便多少。开发中还是视情况而定。

PropertySource

​ 在JdbcConfig.java中,数据库相关的信息需要摘解出来,写在配置文件中。

jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring?useSSL=false
jdbc.username=root
jdbc.password=welcome1

对应的修改JdbcConfig.java为:

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;
    @Bean(name = "queryRunner")
    @Scope("prototype")
    public QueryRunner createQueryRunner(DataSource dataSource) {
        return new QueryRunner(dataSource);
    }
    @Bean(name = "dataSource")
    public DataSource createDataSource() {
        try {
            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
            comboPooledDataSource.setDriverClass(driver);
            comboPooledDataSource.setJdbcUrl(url);
            comboPooledDataSource.setUser(username);
            comboPooledDataSource.setPassword(password);
            return comboPooledDataSource;
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
    }
}

SpringConfiguration.java中添加PropertySource注解

@ComponentScan(basePackages = "com.xijianlv")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}

PropertySource:用于指定properties文件的位置

属性:

  • value:指定文件的路径和名称。classpath表示类路径下。

Spring整合junit

​ 目前测试类中的代码如下:

@Test
public void testFindAll() {
  //获取容器
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
  //业务层对象
  IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
  //执行方法
  List<Account> accountList = accountService.findAllAccount();
  for (Account account : accountList) {
    System.out.println(account);
  }
}

​ 每个方法都需要获取容器等等操作,虽然可以通过junit提供的@Before注解来减少重复的代码,但是还是需要开发人员写代码来获取容器,获取对象等等。

分析

  1. 应用程序的入口:main方法。
  2. junit单元测试中,没有main方法也可以执行。其实并不是没有,junit集成来一个main方法,该方法会判断测试类中的哪些方法有@Test注解,junit就让有@Test注解的方法执行。
  3. junit无从得知项目中是否有使用spring框架。在执行测试方法的时候,junit不知道是否有spring框架,所有也不会读区配置文件或者配置类来创建spring核心容器。
  4. 由上述3点可知,测试方法执行的时候,并没有IOC容器。

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提供。

    @RunWith(SpringJUnit4ClassRunner.class)
    public class AccountServiceTest {...}
    
  3. 告知Spring的运行器,spring的IOC创建是基于xml配置还是基于注解的,并且说明位置。

    @ContextConfiguration:

    locations:指定xml文件的文职,加上classpath关键字,表示在类路径下

    classes:指定注解类所在的位置

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfiguration.class)
    public class AccountServiceTest {
      @Autowired
      private IAccountService accountService = null;
      @Test
      public void testFindAll() {
        //执行方法
        List<Account> accountList = accountService.findAllAccount();
        for (Account account : accountList) {
          System.out.println(account);
        }
      }
      ...
    }
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:bean.xml")
    public class AccountServiceTest {
      @Autowired
      private IAccountService accountService = null;
      @Test
      public void testFindAll() {
        //执行方法
        List<Account> accountList = accountService.findAllAccount();
        for (Account account : accountList) {
          System.out.println(account);
        }
      }
      ...
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值