首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Spring 入門(二):第一個完整 Spring 案例實戰

     編輯:Java 相關知識

項目結構概覽

項目目錄結構

核心代碼實現

1. DAO 接口與實現

package www.csdn.spring.dao;

public interface HelloDao {
    void sayHello();
}
package www.csdn.spring.dao;

public class HelloDaoImpl implements HelloDao {
    public HelloDaoImpl() {
        System.out.println("HelloDaoImpl 實例化.......");
    }

    @Override
    public void sayHello() {
        System.out.println("say:hello");
    }
}

2. Service 層(依賴注入演示)

package www.csdn.spring.service;

public interface HelloService {
    void sayHello();
}
package www.csdn.spring.service;

import www.csdn.spring.dao.HelloDao;

public class HelloServiceImpl implements HelloService {
    private HelloDao helloDao; // 由容器注入

    public void setHelloDao(HelloDao helloDao) {
        this.helloDao = helloDao;
    }

    @Override
    public void sayHello() {
        helloDao.sayHello();
    }
}

3. 配置文件

spring-dao.xml —— 定義 DAO Bean,採用單例且立即初始化:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl"
          scope="singleton" lazy-init="default"></bean>
</beans>

spring-service.xml —— 定義 Service Bean,通過 <property> 注入 DAO:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl"
          scope="singleton" lazy-init="false">
        <property name="helloDao" ref="helloDaoImpl" />
    </bean>
</beans>

spring.xml —— 主配置,聚合子配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring-dao.xml" />
    <import resource="spring-service.xml" />
</beans>

4. 測試類

package www.csdn.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import www.csdn.spring.dao.HelloDao;
import www.csdn.spring.service.HelloService;

public class DemoTest {
    @Test
    public void test() {
        // 手動創建對比(非 Spring 管理)
        HelloDao helloDao = new HelloDaoImpl();
        helloDao.sayHello();

        // Spring 容器管理模式(註釋部分為演示代碼)
        /*
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        HelloDao dao = context.getBean("helloDaoImpl", HelloDao.class);
        dao.sayHello();

        HelloService service = context.getBean("helloServiceImpl", HelloService.class);
        service.sayHello();
        */
    }
}

運行結果分析

HelloDaoImpl 實例化.......
say:hello

控制台輸出證明:

  1. DAO 實例在容器啟動時被創建(lazy-init="default" 等同於 false)
  2. Service 通過屬性注入獲得 DAO 引用,調用鏈路完整

關鍵點總結

  • 接口與實現分離,面向接口編程
  • 使用 <import> 實現配置模塊化
  • Setter 注入體現依賴倒置原則
  • 單例模式下 Bean 生命周期由容器全權管理
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved