1. 程式人生 > 其它 >Spring深入淺出(九),註解,@Autowired

Spring深入淺出(九),註解,@Autowired

在 Spring 中,儘管可以使用 XML 配置檔案實現 Bean 的裝配工作,但如果應用中 Bean 的數量較多,會導致 XML 配置檔案過於臃腫,從而給維護和升級帶來一定的困難。
Java 從 JDK 5.0 以後,提供了 Annotation(註解)功能,Spring 2.5 版本開始也提供了對 Annotation 技術的全面支援,我們可以使用註解來配置依賴注入。

一、先重溫一遍配置檔案的方式

1. 建立主業務類

package com.clzhang.spring.demo;

public class TextEditor {
    private SpellChecker spellChecker;
    
private String name; public TextEditor(SpellChecker spellChecker, String name) { this.spellChecker = spellChecker; this.name = name; } public SpellChecker getSpellChecker() { return spellChecker; } public String getName() { return name; }
public void spellCheck() { System.out.println("Inside TextEditor,Name : " + name); spellChecker.checkSpelling(); } }

2. 建立業務邏輯實現類

package com.clzhang.spring.demo;

public class SpellChecker {
    public SpellChecker() {
        System.out.println("Inside SpellChecker constructor.");
    }

    
public void checkSpelling() { System.out.println("Inside checkSpelling."); } }

3. 建立主程式

package com.clzhang.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   }
}

4.1. 配置檔案傳統寫法

<?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-3.0.xsd">

   <bean id="textEditor" class="com.clzhang.spring.demo.TextEditor">
      <constructor-arg ref="spellChecker"/>
      <constructor-arg value="Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.clzhang.spring.demo.SpellChecker">
   </bean>
</beans>

4.2 配置檔案自動裝配寫法

<?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-3.0.xsd">

   <bean id="textEditor" class="com.clzhang.spring.demo.TextEditor" autowire="constructor">
      <constructor-arg value="Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.clzhang.spring.demo.SpellChecker">
   </bean>
</beans>

5. 執行

Inside SpellChecker constructor.
Inside TextEditor,Name : Generic Text Editor
Inside checkSpelling.

二、使用@Autowired的方式

1. 修改主業務類如下

package com.clzhang.spring.demo;

import org.springframework.beans.factory.annotation.Autowired;   

public class TextEditor {
    private SpellChecker spellChecker;
    private String name;
    
    @Autowired 
    public TextEditor(SpellChecker spellChecker, String name) {
        this.spellChecker = spellChecker;
        this.name = name;
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    public String getName() {
        return name;
    }

    public void spellCheck() {
        System.out.println("Inside TextEditor,Name : " + name);
        spellChecker.checkSpelling();
    }
}

2. 配置檔案修改如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
   <context:annotation-config/>
  
   <bean id="textEditor" class="com.clzhang.spring.demo.TextEditor">
      <constructor-arg value="Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.clzhang.spring.demo.SpellChecker">
   </bean>
</beans>

Spring 預設不使用註解裝配 Bean,因此需要在配置檔案中新增 <context:annotation-config/>,啟用註解。

3. 執行

Inside SpellChecker constructor.
Inside TextEditor,Name : Generic Text Editor
Inside checkSpelling.

本文參考:

http://c.biancheng.net/spring/config-autowiring.html

https://www.w3cschool.cn/wkspring/rw2h1mmj.html

https://blog.csdn.net/l1212xiao/article/details/80424064