基于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();
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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());
  • 1
  • 2
  • 3
  • 4
  • 5

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 + '}';
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

调用时的主要代码:

ApplicationContext ap=new AnnotationConfigApplicationContext(ComponentTest.class);
ComponentTest comp=(ComponentTest) ap.getBean(ComponentTest.class);
comp.setId(12);
comp.setName("componentTest");
System.out.println(comp);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

用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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

实现组件扫描(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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

@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 "";
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

用AnnotationConfigWebApplicationContext支持Web应用

WebApplicationContext 接口继承了ApplicationContext,它可以由AnnotationConfigWebApplicationContext. 产生,这种实现应用于配置Spring ContextLoaderListener 类(servlet监听器) Spring MVC DispatcherServlet等等,其示例请查看API或者其他文档,后续到Spring MVC时将继续更新。若感觉不够清楚请参考本文档编写依据的api,可自行在本人csdn下载资源spring-framework-4.0.1.RELEASE

猜你喜欢

转载自blog.csdn.net/aa6751789079/article/details/81172181