[Spring實戰系列](15)使用Spring基於Java的配置

來源:互聯網
上載者:User

[Spring實戰系列](15)使用Spring基於Java的配置
並不是所有的開發人員都喜歡使用XML,所以Spring3.0 為這些人準備了一些特別的東西。可以幾乎不使用XML而使用純粹的Java代碼來配置Spring應用。並且基於Java的配置擁有一些XML配置所不具有的技巧。 1. 建立基於Java的配置 即使Spring的Java配置可以讓我們不使用XML就可以編寫大多數的Spring配置,但是我們仍然需要極少量的XML來啟用Java配置。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 我們已經知道是如何自動註冊那些使用某種構造型(stereotype)註解所標註的Bean的。但是它也會自動載入使用@Configuration註解所標註的類。在該例子中,base-package屬性告知Spring在com.sjf.bean包內尋找使用@Configuration註解所標註的所有類。  2. 定義一個配置類  在基於Spring的XML配置時,XML配置的根項目是來自Spring Bean命名空間的元素。而在基於Java配置裡使用@Configuration註解的Java類,來代替XML配置中的元素。
package com.sjf.bean;
import org.springframework.context.annotation.Configuration;/** * 基於Java的配置 * @author sjf0115 * */@Configurationpublic class SpringConfig { // Bean declaration methods }   @Configuration註解作為一個標示告知Spring:這個類將包含一個或者多個Spring Bean的定義。這些Bean的定義是使用@Bean註解所標註的方法。  3. 聲明一個簡單類  我們使用@Bean註解標註一個方法來定義studentBean Bean:
package com.sjf.bean;
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * 基於Java的配置 * @author sjf0115 * */@Configurationpublic class SpringConfig { // Bean declaration methods @Bean public Student studentBean(){ return new Student(); }}   這個簡單方法就是Java配置,它等價與我們之前使用XML所配置的元素:
@Bean告知Spring這個方法將返回一個對象,該對象應該被註冊為Spring應用上下文中的一個Bean。方法名作為該Bean的ID。在該方法中所實現的所有邏輯本質上都是為了建立Bean。上面例子中,該方法建立並返回一個Student的執行個體對象,該對象被註冊為Spring應用上下文中ID為studentBean的Bean。  優點:

在XML配置中,Bean的類型和ID都是由Spring屬性來表示的。Spring標識符的缺點是它們無法進行編譯器檢查。在Spring的基於Java的配置中並沒有String屬性。Bean的ID和類型都被視為方法簽名的一部分。Bean的實際建立是在方法體中定義的。因為它們全都是Java代碼,所以我們可以進行編譯器檢查確保Bean的類型是合法類型,並且Bean的ID是唯一的。
  4. 使用Spring的基於Java的配合進行注入  對於以前使用構造器注入的方式,在使用Java的配置,只需把數字直接傳入構造器中即可:
@Bean
public Student studentBean(){ return new Student("yoona",24);}   使用Java的配置,定義Bean就像我們使用Java編寫類的執行個體化代碼一樣。 我們再看看setter注入,在Java的配置中,如何??
@Bean
public Student studentBean(){ Student stu = new Student(); stu.setName("yoona"); stu.setAge(24); return stu;}   那麼Bean裝配另一個Bean的引用? 我們先建立一個待引用的Bean:
@Bean
public School schoolBean(){ School school = new School(); school.setName("西安電子科技大學"); school.setLocation("西安"); return school;} 現在來看看怎麼引用一個Bean,我們通過構造器為它裝配上面那個Bean:
@Bean
public Student yoonaStudent(){ return new Student(schoolBean());}   注意:

在Spring的Java配置中,通過聲明方法引用一個Bean並不等於等於調用該方法。如果真這樣,每次調用schoolBean(),都將得到該Bean的一個新的執行個體。通過使用@Bean註解標註schoolBean()方法,會告知Spring我們希望該方法定義的Bean要被註冊進Spring的應用上下文中。因此,其他Bean的聲明方法中運用這個方法時,Spring都會攔截該方法的調用,並嘗試在應用上下文中尋找該Bean,而不是讓方法建立一個新的執行個體。
  5. 運行  一旦配置類定義,可以載入和提供他們使用AnnotationConfigApplicationContext 如下:
package com.sjf.bean;
import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test { private static ApplicationContext context; public static void main(String[] args){ context = new AnnotationConfigApplicationContext(SpringConfig.class); Student student = (Student)context.getBean("yoonaStudent"); student.setName("yoona"); student.setAge(24); System.out.println(student.toString()); }}  

參考:《Spring實戰》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.