Spring-03--Spring配置Bean

来源:互联网 发布:11对战平台for mac 编辑:程序博客网 时间:2024/06/05 17:07


本人初学Spring框架,新手一个,菜鸟一个。写这类文章并非炫耀知识,我也没能力炫耀,
只是作为本人的习总结、当笔记用。希望各位前辈看到了我总结有错的地方可以多多指
,如果是对大家有帮助的,也希望大家可以赏脸浏览;

-------------------------------------------------------------------------------------------------------------------
今天开始学习配置Bean,分为几个小节来完成(学习的内容为红色字体的内容)
内容提要
配置Bean
--配置形式:基于XML文件的方式;基于注解的方式;
--Bean的配置方式:通过全类名(反射)、通过工厂方法(静态工厂方法&实例工厂方法)、FactoryBean
--IOC容器 BeanFactory&ApplicationContext概述
--依赖注入的方式:属性注入、构造器注入
--注入属性值的细节
--自动转配
--Bean 之间的关系:继承、依赖
--Bean 的作用域:singleton、prototype、WEB 环境作用域
-- 使用外部属性文件
--spEL
--IOC 容器中Bean的生命周期
--Spring 4.x新特性:泛型依赖注入
-------------------------------------------------------------------------------------------------------------------

--配置形式:基于XML文件的方式;
所谓基于XML文件的方式去配置Bean,就是在ApplicationContext中去配置<Bean></Bean>

--Bean的配置方式:通过全类名(反射)
<!-- 配置Beanclass:Bean的全类名,通过反射的方式在IOC容器(指ApplicationContext)中创建Bean,
   所以要求Bean中必须拥有无参的构造函数id:标识容器的Bean,id唯一 --><bean id="helloWorld" class="com.ioc.demo.HelloWorld"><property name="name" value="Spring 4"></property></bean>

--IOC容器 BeanFactory&ApplicationContext概述
我们在 Spring IOC容器中读取Bean配置的时候就要对IOC容器进行实例化,然后才能通过getBean()方法
拿到Bean的实例并进行使用。Spring为我们提供了两种对IOC容器进行实例化的方法
--BeanFactory :IOC容器的基本实现
--ApplicationContext:提供了更多的高级特性,是BeanFactory的子接口

区别:BeanFactory是Spring框架的基础设施,面向Spring本身;
ApplicationContext面向使用Spring框架的开发者,几乎所有的场合都是使用ApplicationContext的
ApplicationContext的主要实现类:
--ClasspathXMLApplicationContext:从类路径下加载配置文件
--FileSystemXmlApplicationContext:从文件系统中加载配置文件

--依赖注入的方式:属性注入、构造器注入
--属性注入
name:用于指明类HelloWorld中的属性的名称
value:用于给属性进行赋值
其实下面这一小段语句就是系统自己调用HelloWorld类的无参的构造方法HelloWorld()
和调用了属性name的setName()方法
<bean id="helloWorld" class="com.ioc.demo.HelloWorld"><property name="name" value="Spring 4"></property></bean>
--构造器注入
<!-- 
构造注入
index用于标识参数的角标
type用于指明参数的数据类型
两者结合可以用于区分重载的构造函数
-->
<bean id="car" class="com.ioc.demo.Car">
<constructor-arg value="AuDi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="500000" index="2"></constructor-arg>
</bean>

<bean id="car2" class="com.ioc.demo.Car">
<constructor-arg value="AuDi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>

源代码
类Car
package com.ioc.demo;public class Car {private String brand;private String city;private double price;private int maxSpeed;public Car(String brand, String city, double price) {super();this.brand = brand;this.city = city;this.price = price;}@Overridepublic String toString() {return "Car [brand=" + brand + ", city=" + city + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";}public Car(String brand, String city, int maxSpeed) {super();this.brand = brand;this.city = city;this.maxSpeed = maxSpeed;}}
:HelloWorld
package com.ioc.demo;public class HelloWorld {private String name;public void setName(String name) {this.name = name;}public void helloWorld(){System.out.println("HelloWorld---"+name);}}

类:Main
package com.ioc.demo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {// TODO Auto-generated method stub/* *ApplicationContext作为IOC容器,是一个接口; *实现类: *--ClassPathXmlApplicationContext :从类路径下加载配置文件 *--FileSystemXmlApplicationContext:从文件系统中加载配置文件 */ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");HelloWorld helloWorld = (HelloWorld) act.getBean("helloWorld");helloWorld.helloWorld();Car car = (Car) act.getBean("car");System.out.println(car.toString());car = (Car) act.getBean("car2");System.out.println(car.toString());}}

ApplicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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"><!-- 配置Beanclass:Bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须拥有无参的构造函数id:标识容器的Bean,id唯一 --><bean id="helloWorld" class="com.ioc.demo.HelloWorld"><property name="name" value="Spring 4"></property></bean><!-- 构造注入index用于标识参数的角标type用于指明参数的数据类型两者结合可以用于区分重载的构造函数 --><bean id="car" class="com.ioc.demo.Car"><constructor-arg value="AuDi" index="0"></constructor-arg><constructor-arg value="ShangHai" index="1"></constructor-arg><constructor-arg value="500000" index="2"></constructor-arg></bean><bean id="car2" class="com.ioc.demo.Car"><constructor-arg value="AuDi" index="0"></constructor-arg><constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg><constructor-arg value="240" type="int"></constructor-arg></bean></beans>






本人初学Spring框架,新手一个,菜鸟一个。写这类文章并非炫耀知识,我也没能力炫耀,
只是作为本人的习总结、当笔记用。希望各位前辈看到了我总结有错的地方可以多多指
,如果是对大家有帮助的,也希望大家可以赏脸浏览;

-------------------------------------------------------------------------------------------------------------------
0 0
原创粉丝点击