Spring学习一:环境搭建,Bean对象使用

Spring是一个IOC(DI)和AOP容器框架。

 

一:搭建Spring运行时环境

1:加入JAR包
  ① Spring自身JAR包:spring-framework-4.0.0.RELEASE\libs目录下
        spring-beans-4.0.0.RELEASE.jar
        spring-context-4.0.0.RELE2ASE.jar
        spring-core-4.0.0.RELEASE.jar
        spring-expression-4.0.0.RELEASE.jar
  ② commons-logging-1.1.1.jar

2:创建Spring的配置文件:applicationContext.xml

测试:

//1. application.xml中的配置信息:

	<bean id="student" class="com.spring.bean.Student">

		<property name="studentId" value="1001"/>
		<property name="stuName" value="Tom2015"/>
		<property name="age" value="20"/>
	</bean>
	    <!-- 使用bean元素定义一个由IOC容器创建的对象 -->
    	<!-- class属性指定用于创建bean的全类名 -->
	    <!-- id属性指定用于引用bean实例的标识 -->
		<!-- 使用property子元素为bean的属性赋值 -->


//2. 通过Spring的IOC容器创建对象实例
        //1.创建IOC容器对象
        ApplicationContext iocContainer = new ClassPathXmlApplicationContext("application.xml");
        //2.根据id值获取bean实例对象
        Student student = (Student) iocContainer.getBean("student");
        //Student student = (Student) iocContainer.getBean(Student.class);//这种方式也行,此时可以不用设置id,但是此时Bean配置中只能有一个Student的Bean。
        //3.打印bean
        System.out.println(student);

 

二、IOC容器和DI

  Inversion Of Control:翻转控制。把原来由程序员管理对象的权利,反转给程序本身,让程序自己管理。

  Dependency Inject 依赖注入。组件以一些预先定义好的方式(如:setter方法)接受来自容器的资源注入。注入就是赋值 。IOC是一种反转控制的思想,DI是对IOC的一种具体实现。

IOC容器【ApplicationContext】在Spring中的实现。

  1. 在通过IOC容器读取Bean的实例之前,需要先将IOC容器本身实例化。
  2. Spring提供IOC容器的两种方式:① BeanFactory[ 顶层 ]:IOC容器的基本实现,是Spring内部的基础设施,面向Spring本身,不是提供给开发人员使用。  ② ApplicationContext:BeanFactory的子接口,提供更高级的特性,面向Spring的使用者。

ApplicationContext的主要实现类

  1. ClassPathXmlApplicationContext:对应类路径下的XML格式的配置文件
  2. FileSystemXmlApplicationContext:对应文件系统中的XML格式的配置文件
  3. 在初始化时就创建单例的bean,也可以通过配置的方式指定创建的Bean是多实例的。

 

 

三、bean的属性赋值 

  1. set方法注入:<property name="id" value="10086"></property>配置信息时。    <property name="id" ref="xx"></property>
  2. 构造方法注入:<contructor-arg   value=" " index="参数位置"  type="参数类型">  </constructor-arg>  // 通过index,type区分重载的构造器

为属性赋值时可以用的值

1. 字面量  [ String,基本数据类型,包装类 ]

①.可以使用字符串表示的值,可以通过value属性或value子节点的方式指定
②.基本数据类型及其封装类、String等类型都可以采取字面值注入的方式
③.若字面值中包含特殊字符,可以使用<![CDATA[]]>把字面值包裹起来

2.外部已声明的bean、引用其他的bean

<bean id="shop" class="com.spring.bean.Shop" >
    <property name="book" ref="book"/>
</bean>

3.null值

<bean class="com.spring.bean.Book" id="bookNull" >
   <property name= "bookId" value ="2000"/>
   <property name= "bookName">
   <null/>
   </property>
   <property name= "author" value ="nullAuthor"/>
   <property name= "price" value ="50"/>
</bean >

 

集合赋值

① List与数组

配置java.util.List类型的属性,需要指定<list>标签,在标签里包含一些元素。这些标签	可以通过<value>指定简单的常量值,通过<ref>指定对其他Bean的引用。通过<bean>指定内置bean定义。通过<null/>指定空元素。甚至可以内嵌其他集合。
	数组的定义和List一样,都使用<list>元素。


<property name="students">
    <list>
        <ref bean="s1"/>
        <ref bean="s2"/>
        <ref bean="s3"/>
    </list>
</property>

② Map

Java.util.Map通过<map>标签定义,<map>标签里可以使用多个<entry>作为子标签。每个条目包含一个键和一个值。
	必须在<key>标签里定义键。
	因为键和值的类型没有限制,所以可以自由地为它们指定<value>、<ref>、<bean>或<null/>元素。
	可以将Map的键和值作为<entry>的属性定义:简单常量使用key和value来定义;bean引用通过key-ref和value-ref属性定义。

	<property name="bookMap">
		<map>
			<entry>
				<key>
					<value>bookKey01</value>
				</key>
				<ref bean="book01"/>
			</entry>
			<entry>
				<key>
					<value>bookKey02</value>
				</key>
				<ref bean="book02"/>
			</entry>
		</map>
	</property>

 

FactoryBean

Spring中有两种类型的bean,一种是普通bean,另一种是工厂bean,即FactoryBean。

         工厂bean跟普通bean不同,其返回的对象不是指定类的一个实例,其返回的是该工厂bean的getObject方法所返回的对象。

         工厂bean必须实现org.springframework.beans.factory.FactoryBean接口。

//1. 对象定义
public class Car {
	private String brand;	
	private Double price;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + "]";
	}	
}



//2. 工厂Bean定义
public class MyFactory implements FactoryBean<Car> {

	@Override
	public Car getObject() throws Exception {
		Car car = new Car();
		car.setBrand("奥迪");
		car.setPrice(200000.0);
		return car;
	}

	@Override
	public Class<?> getObjectType() {
		return Car.class;
	}

	@Override
	public boolean isSingleton() {
		return false;
	}

}
//3. xml定义
	<bean id="factory" class="com.spring.factorybean.MyFactory"></bean>

//3. 测试
public class Test {
	public static void main(String[] args) {		
		ApplicationContext ac = new ClassPathXmlApplicationContext("factory-bean.xml");
		Object object = ac.getBean("factory");
		System.out.println(object);		
	}	
}

四、bean的作用域与生命周期

作用域分四种:

  1. singleton类型的,会在初始化容器的时候创建bean。
  2. prototype类型的,就会在使用时创建bean。
  3. request类型的,一次请求中有效,一次请求中只会创建一个bean。
  4. session类型的,一次会话中有效,一次会话只会创建一个bean。

IOC可以管理Bean的生命周期,spring允许在bean生命周期内特定的时间点执行指定的任务。

  创建 ,依赖注入 ,初始化 ,使用 ,销毁 

      <bean id="" class="" init-method="" destory-method="">

  后置处理器

public class AfterHandler implements BeanPostProcessor{
    public Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException{  return null; }// 初始化之前。
    public Object postProcessAfterInitalization(Object bean,String beanName ) throws BeansException{ return null; }  // 初始化之后。
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值