spring基于xml配置

主要依赖的jar包有

文件: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>com.wyu</groupId>
    <artifactId>spring_ioc_xml</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <!--IOC框架-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>

        <!--操作数据工具-->
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.7</version>
        </dependency>

        <!--mysql连接器-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</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.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

项目中的接口和实现类

实体类

文件:Account.java

package com.wyu.domain;

import org.apache.commons.dbutils.ResultSetHandler;

/*
*
* 账户实体类
*
* */
public class Account {
    private Integer customer_id;
    private String first_name;
    private String email;

    public Integer getCustomer_id() {
        return customer_id;
    }

    public void setCustomer_id(Integer customer_id) {
        this.customer_id = customer_id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Account{" +
                "customer_id=" + customer_id +
                ", first_name='" + first_name + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

service接口

文件:IAccountService.java

package com.wyu.service;

import com.wyu.domain.Account;

import java.util.List;

/*
* 账户业务接口
* */
public interface IAccountService {
    /*
    * 查询所有账户
    * */
    List<Account> findAllAccount();

    /*
     * 查询单个账户
     *
     * */
    Account findAccount(Integer id);

    /*
     *
     * 保存账户
     * */
    void saveAccount(Account account);

    /*
     * 更新账户
     * */
    void updateAccount(Account account);

    /*
     * 删除账户
     *
     * */
    void deleteAccount(Integer id);

}

serivce实现类

文件:AccountService.java

package com.wyu.service.impl;

import com.wyu.dao.IAccountDao;
import com.wyu.domain.Account;
import com.wyu.service.IAccountService;

import java.sql.SQLException;
import java.util.List;

/*
* 账户业务层实现类
*
* */
public class AccountService implements IAccountService {

    private IAccountDao iAccountDao;

    /*private AccountDao accountDao;


    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }*/

   public void setiAccountDao(IAccountDao iAccountDao) {
        this.iAccountDao = iAccountDao;
    }


    public List<Account> findAllAccount() {
        try {
            return (List<Account>) iAccountDao.findAllAccount();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Account findAccount(Integer id) {
        return iAccountDao.findAccount(id);
    }

    public void saveAccount(Account account) {
        iAccountDao.saveAccount(account);
    }

    public void updateAccount(Account account) {
        iAccountDao.updateAccount(account);
    }

    public void deleteAccount(Integer id) {
        iAccountDao.deleteAccount(id);
    }

}

dao层接口

文件:IAccountDao.java

package com.wyu.service.impl;

import com.wyu.dao.IAccountDao;
import com.wyu.domain.Account;
import com.wyu.service.IAccountService;

import java.sql.SQLException;
import java.util.List;

/*
* 账户业务层实现类
*
* */
public class AccountService implements IAccountService {

    private IAccountDao iAccountDao;
    
   public void setiAccountDao(IAccountDao iAccountDao) {
        this.iAccountDao = iAccountDao;
    }


    public List<Account> findAllAccount() {
        try {
            return (List<Account>) iAccountDao.findAllAccount();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Account findAccount(Integer id) {
        return iAccountDao.findAccount(id);
    }

    public void saveAccount(Account account) {
        iAccountDao.saveAccount(account);
    }

    public void updateAccount(Account account) {
        iAccountDao.updateAccount(account);
    }

    public void deleteAccount(Integer id) {
        iAccountDao.deleteAccount(id);
    }

}

测试类

文件:AccountServiceTest.java

package com.wyu.service;

import com.wyu.dao.IAccountDao;
import com.wyu.domain.Account;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/*
 * service层测试
 *
 * */
public class AccountServiceTest {

    @Test
    public void testfindAllAccount(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
        List<Account> accounts = accountService.findAllAccount();
        for (Account account : accounts) {
            System.out.println(account);
        }
    }

    @Test
    public void testFindAccount() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService iAccountService = applicationContext.getBean("accountService", IAccountService.class);
        System.out.println(iAccountService);
        Account account = iAccountService.findAccount(1);
        System.out.println(account);
    }
}

基于xml的bean配置

文件: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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--配置service层对象-->
    <bean id="accountService" class="com.wyu.service.impl.AccountService">
        <property name="iAccountDao" ref="accountDao"></property>
    </bean>

    <!--配置dao层对象-->
    <bean id="accountDao" class="com.wyu.dao.impl.AccountDao">
        <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://xxxxxx:3306/sakila?serverTimezone=UTC"></property>
        <property name="user" value="xxx"></property>
        <property name="password" value="xxxx"></property>
    </bean>
</beans>

后记

该程序主要使用的xml文件来配置bean对象,将需要创建的对象的AccountService类、AccountDao类、QueryRunner类、DataSource类配置到bean.xml文件中,从而实现在其他类中轻易地创建这些类的对象来使用,采用的依赖注入方式是Setter方法注入,该方法相对与通过构造方法注入的方式,避免了构造方法的臃肿。主要代码为:
文件:AccountService.java

    private IAccountDao iAccountDao;

   public void setiAccountDao(IAccountDao iAccountDao) {
        this.iAccountDao = iAccountDao;
    }

文件:AccountDao.java

    private QueryRunner queryRunner;

    /*
    * 使用Setter方法注入QueryRunner
    *
    * */
    public void setQueryRunner(QueryRunner queryRunner) {
        this.queryRunner = queryRunner;
    }

使用spring创建对象的好处是:spring采用工厂模式,需要哪个对象只需要将其加入到容器中,然后使用,不用自己一层一层实例化对象,从而降低程序的耦合性。