天天看點

基于xml的spring ioc的demo

首先建立一個maven架構

我們來給pom.xml來增加一些依賴

//首先增加上一個packaging
<packaging>jar</packaging>
//然後添加依賴,包括spring架構的、用于資料庫的、連接配接池坐标的及junit測試的。
<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.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>
           

然後我們來建立一個service包

先建立一個業務層的接口

//在接口内定義一個方法
List<Account> FindAll();
           

然後建立一個impl包,其中建立service的實作類

//針對于聲明的accountDao對象,使用set方法
private IAccountDao accountDao;

public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

public List<Account> FindAll(){
    return accountDao.FindAll();
}
           

我們再來建立此時還在報紅的Account類

​ 類名那個地方要加implements Serializable,才可以使用。

//我們建立一個Account類,可以建立一個包,将其放于包下
private Integer id;
private String name;
private float money;
//建立這些變量的get和set方法,并建立toString()方法
 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 +
                '}';
    }
           

然後再來建立service實作類中使用的dao對象

先建立dao包,在dao包下建立IAccountDao這個接口

在建立一個impl包,裡面建立dao的實作類AccountDaoImpl

private QueryRunner runner;

public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }

public List<Account> FindAll(){
    try{
        return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
    }catch(Exception e){
        throw new RuntimeException(e);
    }
}
           

我們來建立bean.xml來使得所設的對象彼此能夠形成聯系

//首先給它增加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标簽(這裡的id與class都是根據自己的程式來的)
	<bean id="accountService" class="goulizi.service.impl.AccountServiceImpl">
	//因為在AccountServiceImpl實作類中,我們定義了accountDao對象,且使用了set方法,是以這裡要對他進行注入
    //且這裡ref的使用是由下面那個bean所設定的,下面的亦是同理
    	<property name="accountDao" ref="accountDao"></property>
	</bean>
  
	<bean id="accountDao" class="goulizi.dao.impl.AccountDaoImpl">
    	<property name="runner" ref="runner"></property>    
    </bean>
        
    <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:3306/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>
</beans>
    
           

最後來建立測試類

//編寫測試方法
@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);
        }
}
           

然後就可以運作了,如下截圖:

基于xml的spring ioc的demo