首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Spring 注入 HibernateTemplate:容器管理 vs 手動獲取的本質區別

     編輯:程式設計綜合問答

疑問:控制台程序用 `new ClassPathXmlApplicationContext()` 獲取 Bean,Service 層用 `@Resource` 直接注入,為何不需要手動創建容器?

核心區別:誰負責創建和管理 Bean 生命周期

模式容器創建者Bean 獲取方式適用場景
**手動啟動**開發者代碼 (`main` 方法)`context.getBean()`獨立應用、批處理、測試、非 Web 環境
**Web 容器托管**Servlet 容器 (Tomcat/Jetty) 啟動時依賴注入 (`@Autowired`/`@Resource`)Spring MVC、Spring Boot Web 應用

Web 環境自動化流程

1. Tomcat 啟動
   ↓
2. 讀取 web.xml → 發現 ContextLoaderListener
   ↓
3. Listener.contextInitialized() → 
   new XmlWebApplicationContext() → 
   載入 applicationContext.xml → 
   實例化所有  (含 hibernateTemplate)
   ↓
4. 將容器存入 ServletContext 屬性 "org.springframework.web.context.WebApplicationContext.ROOT"
   ↓
5. DispatcherServlet 初始化 → 
   獲取父容器 → 創建子容器 (掃描 @Controller)
   ↓
6. 掃描 @Service/@Repository/@Component → 
   自動裝配 @Resource hibernateTemplate → 
   注入到 Service 字段
   ↓
7. 請求到達 → Controller → Service.method() → 直接使用 hibernateTemplate

代碼對比

控制台程序 (手動)

public class Main {
    public static void main(String[] args) {
        // 1. 手動創建容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2. 手動獲取
        HibernateTemplate ht = ctx.getBean("hibernateTemplate", HibernateTemplate.class);
        // 3. 使用
        ht.save(new User("test"));
        // 4. 關閉容器 (非 Web 必須)
        ((ConfigurableApplicationContext)ctx).close();
    }
}

Spring MVC Service (自動)

@Service
public class UserService {
    // 容器已在 Tomcat 啟動時創建完畢
    // @Resource 按名稱匹配 
    @Resource(name = "hibernateTemplate") 
    private HibernateTemplate ht; // 自動注入,無需手動獲取
    
    public void save(User u) { ht.save(u); }
}

關鍵配置:applicationContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.example.entity"/>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 掃描 Service 自動裝配 -->
<context:component-scan base-package="com.example.service" />

總結

  • **容器單例**:Web 應用中 `ApplicationContext` 由 `ContextLoaderListener` 在 Tomcat 啟動階段**創建一次**,全應用共享。
  • **反向控制 (IoC)**:框架調用你的代碼 (注入依賴),而非你調用框架 (`getBean`)。
  • **生命周期綁定**:Web 環境下 Bean 隨 ServletContext 銷毀而銷毀,無需手動 `close()`。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved