(六)Spring教程——Spring IoC容器(中)

 (一)Spring教程——Spring框架简介

 (二)Spring教程——Spring框架特点

 (三)Spring教程——依赖注入与控制反转

 (四)Spring教程——控制反转或依赖注入与Java的反射技术

 (五)Spring教程——Spring IoC容器(上)

 (六)Spring教程——Spring IoC容器(中)

 (七)Spring教程——Spring IoC容器(下)

目录

ApplicationContext接口方式

代码示例

1.增加配置类

2.增加测试类 


ApplicationContext接口方式

      ApplicationContext由BeanFactory派生而来,提供了更多面向实际应用的功能。在BeanFactory中,很多功能需要以编程的方式实现,而在ApplicationContext中则可以通过配置的方式实现。

       Spring支持基于类注解的配置方式,主要功能来自Spring的一个名为JavaConfig的子项目。JavaConfig现已升级为Spring核心框架的一部分。一个标注为@Configuration注解的POJO即可提供Spring所需的Bean配置信息。

代码示例

1.增加Car类

        在java目录下增加一个新的Package“com.example.reflect”包,在该包中增加一个Car类,该类的代码如下所示 

public class Car {
    private String brand;

    private String color;

    private int maxSpeed;

    public Car(){System.out.println("init car!!");}
    public Car(String brand,String color,int maxSpeed){
        this.brand = brand;
        this.color = color;
        this.maxSpeed = maxSpeed;
    }
    public void introduce() {
       System.out.println("brand:"+brand+";color:"+color+";maxSpeed:"+maxSpeed);
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }
}

        该类中有两个Car的构造函数,一个是不带参数的构造函数,一个是带参数的构造函数。然后实现了一个introduce方法,其它的是属性设置和获取方法。

2.增加配置类

        在项目中增加一个“com.example.context”包,在该包中增加一个Beans配置类,该类的代码如下

package com.example.context;



import com.example.reflect.Car;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;



//表示是一个配置信息提供类

@Configuration

public class Beans {

    //定义一个Bean

    @Bean(name="car")

    public Car buildCar(){

        Car car=new Car();

        car.setBrand("凯迪拉克");

        car.setMaxSpeed(300);

        return car;

    }

}

        该配置类实例化了一个Car对象,并设置Brand为“凯迪拉克”,设置MaxSpeed为300。 

2.增加测试类 

        然后增加一个测试类Test.java

package com.example.reflect;



import com.example.context.Beans;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.core.io.Resource;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.core.io.support.ResourcePatternResolver;



import java.io.IOException;



public class Test {

    public static void main(String[] args) throws IOException {

        //通过xml配置文件的方式装载bean

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        Resource res = resolver.getResource("beans.xml");

        System.out.println("输出路径:" + res.getURL());



        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();

        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);

        reader.loadBeanDefinitions(res);



//        Car car = factory.getBean("car", Car.class);

//        System.out.println("Car bean is ready for use");

//        car.introduce();



        //通过注解的方式装载bean

        ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class);

        Car car = ctx.getBean("car", Car.class);

        System.out.println("品牌:" + car.getBrand());

        System.out.println("最高速度:" + car.getMaxSpeed());

    }

}

运行该项目后的输出结构为

  • 19
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jerry95270628

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值