Spring之配置Bean(一)

        1. 配置形式: 基于XML文件的方式、基于注解的方式(此处不讲解)

           创建applicationContext.xml文件并且将xml配置文件置于src路径下面

           新建的配置文件:

           

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	
</beans>

      2. bean的配置方式:通过全类名(反射)、通过工厂方法(静态工厂方法&实例工厂方法)、FactoryBean

         (1)通过全类名来配置bean

                   


 <bean id="helloWorld" class="com.syh.spring1.HelloWorld">
    <property name="name3" value="李四"/>
 </bean>

       id:Bean的名称

        -在IOC容器中是唯一的

        -如果没有指定id,默认将全类名作为Bean的名字

       -id可以指定多个名字,名字之间可以用逗号、分号或者空格分割

     class:bean的全类名,通过反射方式在IOC容器中创建Bean,所以要求Bean中必须有无参构造器

    3. IOC容器 BeanFactory&ApplicationContext概述

       在SpringIOC容器读取Bean配置创建的Bean实例之前,必须对它进行实例化,只有在容器实例化后,才可以从IOC容器中获取Bean实例,而IOC容器实现hi靠一下两种来实现的

       BeanFactory:IOC容器的基本实现

       applicationContext:提供了更多的高级特性,是BeanFactory的子类(更多的使用该方式)

    上面两种方式都是基于的配置文件都是相同的

      实例:

      创建Spring的IOC容器    

      ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

      其中ApplicationContext 代表IOC容器;ClassPathXmlApplicationContext接口的实现类,该实现类从类路径下来加载配置文件

      从IOC容器获取Bean实例 有两种方式,通过bean唯一表示id,通过Bean类型获取

        HelloWorld  helloWorld = (HelloWorld)ctx.getBean("bean的id");  因为id是唯一的这样获取是完全没有问题的

        HelloWorld  helloWorld = ctx.getBean(HelloWorld.class)  因为同一个类可能在IOC容器中创建两个bean,这样的话就不是为了


    4.依赖注入的方式:属性注入、构造器注入、工厂方法模式(很少使用不推荐)

       (1)属性注入

              通过setter方法进行注入属性值或者依赖对象,在开发中是最常用的,强烈推荐使用

              <property name= "属性名称" value="属性值"/>

       (2)构造方法注入

             通过构造方法注入Bean属性值或者依赖的对象,它保证Bean实例在实例后就可以使用,构造器注入在<constructor-arg>元素里面声明属性,在<constructor-arg>中没有name属性

      Car.java

       

package com.syh.spring1;

public class Car {
	private String brand ;
	private String corp;
	private int price;
	private int maxSpeed;
	public Car(String brand, String corp, int price, int maxSpeed) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.price = price;
		this.maxSpeed = maxSpeed;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
				+ ", maxSpeed=" + maxSpeed + "]";
	}	
}

applicationContext.xml

<!-- 通过构方法来配置bean    给属性赋值时可以通过索引index来改变,还可以通过type来区分重载的构造器参数-->
    <bean id= "car" class="com.syh.spring1.Car">
        <constructor-arg value="AoDi" index="0"/>
        <constructor-arg value="ShangHai" index="1"/>
        <constructor-arg value="300000" index="2"/>
        <constructor-arg value="102" index="3"/>
    </bean>

测试:

		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car = (Car) ctx.getBean("car");
		System.out.println(car);

结果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值