【Spring之旅】Spring中的bean配置

来源:互联网 发布:unity3d 刚体不受重力 编辑:程序博客网 时间:2024/06/06 14:05

内容概述:

Spring bean的配置:

-- 配置形式:基于XML文件的方式;基于注解的方式

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

-- IOC 容器 依赖BeanFactory & ApplicationContext 概述

-- 依赖注入的方式:属性注入;构造器注入;工厂方法注入(很少使用,不推荐)

-- 注入属性值细节

-- 自动装配

-- bean 之间的关系:继承;依赖

-- bean 的作用域:singleton;prototype;WEB 环境作用域

-- 使用外部属性文件

-- spEL

-- IOC 容器中Bean的声明周期

-- Spring 4.x新特性:泛型依赖注入


ApplicationContext:

-- ApplicationContext的主要实现类:

-- ClassPathXmlApplicationContext:从类路径下加载配置文件

-- FileSystemXmlApplicationContext:从文件系统中加载配置文件

-- ConfigurableApplicationContext扩展于ApplicationContext,新增加两个主要方法:refresh()和close(),让ApplicationContext具有启动、刷新和关闭上下文的能力。

-- WebApplicationContext是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作。

-- ApplicationContext在初始化上下文时就实例化所有的单例的Bean。




属性注入:

--属性注入即通过setter方法注入Bean的属性值或者依赖的对象。

--属性注入使用<property>元素,使用name属性指定Bean的属性名称,value属性或<value>子节点指定属性值。

--属性注入是实际应用中最常见的注入方式。


applicationContext.xml中配置如下:

<!-- 
<bean> 标记用来定义Bean的实例化信息,class 属性指定类全名(包名+类名),id属性指定生成的Bean实例名称。
<property> 标记是<bean>的子标记,用来调用Bean实例中的相关Set方法完成属性值的赋值,从而实现依赖关系的注入。
<property> 标记中的name属性指定Bean实例中的相应属性的名称,name属性的值可以通过ref属性或者value属性指定。
当使用ref属性时,表示对Bean工厂中某个Bean的实例的引用。这里引用了bean id为userDao的<bean>标记中创建的UserdaoImpl类的
实例useDao,并将该实例赋值给UserdaoImpl类中的userDao属性,用于依赖注入到UserdaoImpl类中。
 -->
<!-- 配置创建helloWorld的实例 -->
<bean id="helloWorld" class="com.atguigu.spring.beans.HelloWorld">
   <property name="name" value="Spring"></property>
</bean>


<!-- 配置创建UserDaoImpl的实例 -->
<bean id="userDao" class="com.springtest1.dao.UserDaoImpl">
</bean>


<!-- 配置创建UserBizImpl的实例 -->
<bean id="userBiz" class="com.springtest1.biz.UserBizImpl">
   <!-- 依赖注入访问层组件-->
   <property name="userDao" ref="userDao"></property>
</bean>




Spring_属性配置细节

字面值

1--字面值:可用字符串表示的值,可以通过<value>元素标签或value属性进行注入。
2--基本数据类型及其封装类、String等类型都可以采取字面值注入的方式。
3--若字面值中包含特殊字符,可以使用<![CDATA]>把字面值包裹起来。

通过<value>元素标签进行注入范例如下:
<!-- 配置创建helloWorld的实例 -->
<bean id="helloWorld" class="com.atguigu.spring.beans.HelloWorld">
   <property name="name" value="Spring"></property>
</bean>

package com.atguigu.spring.beans;


public class HelloWorld 
{
private String name;


public void setName(String name) {
this.name = name;
}


public String getName() {
return name;
}

public void hello()
{
System.out.println("Hello" + name);
}
}

2、通过构造器对属性进行注入:
配置信息如下:
<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型,以区分重载的构造器 -->
<!-- 通过构造方法来配置bean 的属性 -->
<bean id="carBeanID" class="com.atguigu.spring.beans.Car">
   <constructor-arg value="Audi" index="0"></constructor-arg>
   <constructor-arg value="ShangHai" index="1"></constructor-arg>
   <constructor-arg value="30000" index="2"></constructor-arg>
</bean>


<!-- 通过构造方法来配置bean 的属性 -->
<bean id="carBeanID2" class="com.atguigu.spring.beans.Car">
   <constructor-arg value="Baoma"  type="java.lang.String"></constructor-arg>
  <!--<constructor-arg value="nanjing" type="java.lang.String"></constructor-arg>   -->
   <!--如果字面值包括特殊字符的可以使用<![CDATA[]]>包裹起来  -->
   <!-- 属性值也可以使用value子节点进行配置 -->
   <constructor-arg>
          <value><![CDATA[<%nanjing%>]]></value>
   </constructor-arg>
   <constructor-arg value="400"  type="int"></constructor-arg>
</bean>

java代码:
package com.atguigu.spring.beans;

/**
 * 通过构造方法来配置bean 属性实例
 * 使用构造器注入属性值可以指定参数的位置和参数的类型,以区分重载的构造器
 * @author dapeng
 * @date 2017-06-04
 *
 */
public class Car 
{
private String brand;
private String corp;
private double price;
private int maxSpeed;

/**
* 构造器Car1 
* @param brand
* @param corp
* @param price
*/
public Car(String brand, String corp, double price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
    
/**
* 构造器2
* @param brand
* @param corp
* @param maxSpeed
*/
public Car(String brand, String corp, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
     
/**
* 重载toString 方法
*/
@Override
public String toString() {
return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
+ ", maxSpeed=" + maxSpeed + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
}


原创粉丝点击