1. 程式人生 > >基於Java的Spring容器配置一(AnnotationConfigApplicationContext例項化Spring容器)

基於Java的Spring容器配置一(AnnotationConfigApplicationContext例項化Spring容器)

基本概念:
Spring的新Java配置支援@Configuration註解與@Bean註解
@Configuration表示類可以使用Spring Ioc容器作為bean的定義的來源類似於,而@Bean註解的方法返回一個物件。@Bean註解的方法名稱為該Bean的ID,建立並返回Bean,配置類可以宣告多個@Bean
示例:
@Configuraion 裝載類
BasedJava.java

package com.demo.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by ruanjianlin on 2017/10/9.
 */
@Configuration
public class BasedJava {
@Bean
public Hello hello(){
    return new Hello();
}
}

Hello.java

package com.demo.configuration;

/**
 * Created by ruanjianlin on 2017/10/9.
 */
public class Hello {
private String message;

public Hello() {
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}
}

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"
   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">

<context:component-scan base-package="com.demo.configuration"/>

AnnotationConfigApplicationContext 與ApplicationContext的關係
AnnotationConfigApplicationContext extends GenericApplicationContext
AbstractApplicationContext implements ConfigurableApplicationContext extends ApplicationContext(介面)

使用AnnotationConfigApplicationContext(繼承GenericApplicationContext類實現AnnotationConfigRegistry介面)例項化Spring容器

@Configuration 標註的類已經提供容器, @Configuration標註的類會被註冊成一個Bean的定義,而其裡面宣告@Bean的方法裡面的類也會被定義
通常Spring XML檔案用來作為ClassPathXmlApplicationContext 的初始化。@Configuration 標註的類能夠用來初始化AnnotationConfigApplicationContext類,這樣就可以提供Spring容器的完全的自由的無XML用法
示例:

ApplicationContext app=new AnnotationConfigApplicationContext(BasedJava.class);
Hello hello= (Hello)app.getBean(Hello.class);
hello.setMessage("configuration test");
System.out.println(hello.getMessage());

AnnotationConfigApplicationContext不僅支援@Configuration註解的類,任何@Compnent註解的類或者 按照JSR-330(Dependency Injection for Java)註解的類都被AnnotationConfigApplicationContext支援
示例:
ComponentTest.java

package com.demo.configuration;

import org.springframework.stereotype.Component;

/**
 * Created by ruanjianlin on 2017/10/9.
 */
@Component
public class ComponentTest {
private String name;
private int id;

public ComponentTest() {
}

public ComponentTest(String name, int id) {
    this.name = name;
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Override
public String toString() {
    return "ComponentTest{" + "name='" + name + '\'' + ", id=" + id + '}';
}
}

呼叫時的主要程式碼:

ApplicationContext ap=new AnnotationConfigApplicationContext(ComponentTest.class);
ComponentTest comp=(ComponentTest) ap.getBean(ComponentTest.class);
comp.setId(12);
comp.setName("componentTest");
System.out.println(comp);

用register方法以程式設計方式建立Spring 容器

使用AnnotationConfigApplicationContext類的無參建構函式然後用register方法進行配置,這比較適合programmatically 建立該類的情況

AnnotationConfigApplicationContext appl=new AnnotationConfigApplicationContext();
appl.register(ComponentTest.class);
appl.refresh();//此步驟必不可少
            ComponentTest com=(ComponentTest) appl.getBean(ComponentTest.class);
com.setId(122);
com.setName("registerTest");
System.out.println(com);

實現元件掃描(AnnotationConfigApplicationContext的scan方法)

AnnotationConfigApplicationContext appl=new AnnotationConfigApplicationContext();
appl.scan("com.demo.configuration");
appl.refresh();//此步驟必不可少
ComponentTest com=(ComponentTest) appl.getBean(ComponentTest.class);
com.setId(122);
com.setName("registerTest");
System.out.println(com);

@Configuration由@Comonent衍生而來,因此它也是元件掃描的候選方案,在上個例項中scan方法呼叫時其將會到該包下去掃描,refresh方法呼叫時所有的@Bean標註的方法將會執行,並完成Bean的定義

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}

用AnnotationConfigWebApplicationContext支援Web應用

WebApplicationContext 介面繼承了ApplicationContext,它可以由AnnotationConfigWebApplicationContext. 產生,這種實現應用於配置Spring ContextLoaderListener 類(servlet監聽器) Spring MVC DispatcherServlet等等,其示例請檢視API或者其他文件,後續到Spring MVC時將繼續更新。若感覺不夠清楚請參考本文件編寫依據的api,可自行在本人csdn下載資源spring-framework-4.0.1.RELEASE