• Spring 装配Bean


    Spring 装配Bean

    装配解释:
    创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质
    依赖注入是Spring的基础要素
    一 : 使用spring装配Bean基础介绍
    1 :声明Bean 
    Bean的概念:beans 本身是一个大工厂,beans中的每一个bean就等于定义了一个组件,每个组件中就是我们具体的某个功能
    1.1 :创建Spring      
      配置Spring是很重要的,如果没有配置Spring,那么就等于声明了一个空的容器,毫无意义。
     通过配置Spring容器来告诉它需要加载哪些Bean和如何装配这些Bean,如此才能保证他们彼此的协作。
    Spring容器配置Bean的方式:
    1 : 基于一个或多个XML配置文件 -- 配置文件配置 (传统意义配置)
    2:java注解的配置方式
    1.1.1 : 基于xml配置文件的配置方式
    复制代码
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans
     6         http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
     7 >
     8      
     9      <bean id=“myBean” class=“com.springDemo.beanDemo.BeanIntance”></bean>
    10 </beans>
    复制代码
    在上面的XML配置中,我们声明了一个<beans>元素,可以在里面放置所有的Spring配置信息,包括<bean>声明
    但是 在spring中,并非只有一个beans命名空间,spring为我们提供了10个命名空间配置如下:
    • aop:为声明切面以及将@AspectJ注解的类代理为Spring切面提供了配置信息
    • beans:支持声明bean和装配bean,是spring最原始也是最核心的命名空间
    • context:为配置spring应用上下文提供了配置元素,包括自动监测和自动装配Bean
    • jee:提供与java EE API的集成,例如:JNDI和EJB
    • jms:为声明消息驱动的POJO提供了配置元素
    • lang:支持配置由Grooby,JRuby或BeanShell等脚本实现的Bean
    • mvc:启用spring mvc的能力,例如面向注解的控制器,视图控制器和拦截器
    • oxm:支持spring对象到XML配置的映射
    • tx:声明式事务配置
    • util:提供各种各样的工具类元素,包括把集合配置为Bean 支持属性占位符操作
    在上面的配置中,<bean> 元素中所绑定的组件指定的是com.springDemo.beanDemo包中的BeanIntance类 ,id为myBean 将BeanIntance类的中的方法myBean注入到Spring容器中。
    当spring容器加载该Bean时,Spring将使用默认的构造器来实例化myBean Bean 实际其内部代码是 :
    复制代码
     1 “ new com.springDemo.beanDemo.BeanIntance(); ”
     2 package com.springDemo.beanDemo
     3  
     4 public class BeanIntance{
     5      public BeanIntance myBean(){ BeanIntance intance = new BeanIntance(); intance.setBeanname = “bean,….” ; return intance }
     6      private String beanName;
     7      public void setBeanName(string beanName){  this.beanName = beanName; }
     8          public String getBeanName(){ return  beanName;}
     9      
    10 }
    复制代码
    spring是通过反射来创建Bean的;
    在程序中调用时,我们通过一下方式:
    ApplicationContext ctx = new classPathXmlApplicationContext(“spring-beans.xml”); // 加载spring-beans.xml 文件
    BeanIntance beanIntance = (BeanIntance)ctx.getBean(“myBean”); // 读取mybean组件,
    此时myBean已经是返回了BeanIntance类型(上面说过,在加载时,会自动的实例化此处我们ctx就等于是一个BeanIntance的实例 .getBean(“myBean”)) 就是调用了MyBean方法。
     
    Bean中通过构造器注入配置方式 (在如下的代码中,我们是在上面<beans>的整体容器结构中添加的,只写出bean元素部分配置)
    <bean id=“mybean” class=“com.springDemo.beanDemo.BeanIntance">
         <constructor-arg value=“hello”/>
    </bean>
    当我们指定了bean元素下   <constructor-arg> 子元素后,spring容器再启动并实例化时,会自动的去实例化BeanIntance类中String类型的构造类。并将“hello”赋值给它
    复制代码
    1 public class BeanIntance{
    2      public BeanIntance myBean(){ BeanIntance intance = new BeanIntance(); intance.setBeanname = “bean,….” ; return intance }
    3      private String beanName;
    4      public void setBeanName(string beanName){  this.beanName = beanName; }
    5          public String getBeanName(){ return  beanName;}
    6          public BeanIntance( String str ){ this.beanName = str ; }
    7 }
    复制代码
    注入对象引用
    当构造器是一个对象时,那么我们需要将在bean中定义该对象的组件,并在调用时注入到我们的myBean中。比如:
    <bean id=“Sonbean” class=“com.springDemo.beanDemo.SonBeanIntance”/>
    <bean id=“mybean” class=“com.springDemo.beanDemo.BeanIntance">
         <constructor-arg value=“hello”/>
      <constructor-arg ref=“Sonbean”/>
    </bean>
    工厂方法静态注入
    Spring支持通过<bean>元素额factory-method 属性来装配工厂创建的bean,如果装配的对象是一个静态的方法那么可以使用factory-method来调用
    Bean的作用域:
    所有的Spring Bean默认都是一个单例,也就是只会创建一个实例对象,那么这样的话,每次调用都会得到相同的值,如果我们要得到不同的值,返回不通的实例该如何做?
    配置如下属性
    1 <bean id=“Sonbean” class=“com.springDemo.beanDemo.SonBeanIntance” scope=“protoType”/>
    • scope=“protoType” 属性就是让每次调用都会重新new一个实例出来 ,当然除了 protoType之后,Bean还提供了其他的作用域选项,如下:
    • singleton :在每一个spring容器中,每一个bean都是一个唯一的实例(默认)
    • protoType:允许Bean的定义可以被实例化任意次(每次调用都会创建一个实例)
    • request:在一次http请求中,每个bean定义对应一个实例,该作用域仅在基于web的上下文中有效 比如:spring mvc
    • session:在一个http session 中,每个Bean定义为一个实例,该作用域仅在基于web的上下文中有效 比如:spring mvc
    • global-session:在一个全局http session中,每个Bean定义为一个实例,该作用域仅在portlet的上下文中有效 (Portlet是基于Java的Web组件,由Portlet容器管理,并由容器处理请求,生产动态内容)
    bean属性注入:
    看实例:
    复制代码
    1 pulic class student{
    2      private String name;
    3      public void setName(String name){this.name = name;}
    4      public String getName(){return this.name;}
    5      private int age;
    6      public void setAge(int age){ this.age=age; }
    7      public int getAge(){ return this.age; }
    8 } 
    复制代码
    1 <bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
    2      <property name=“name” value=“liming”/>
    3      <property name=“age” value=“18”/>
    4 </bean>
    在上面的bean配置xml代码中,我们通过bean的元素<property> 的value为bean注入的了一个String类型的值,注意一点:property并没有限制我们value中注入的值的类型,value中可以注入:int,float,String,double,bool,所以在我们注入age 年龄value时,类型为 int 也是可以自动感知的。
    注入内部Bean
    java中有一个叫做内部类,就是类中类 。那么内部bean就是 bean中bean了。
    复制代码
    1 <bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
    2      <property name=“name” value=“liming”/>
    3      <property name=“age” value=“18”/>
    4      <property name=“teacher” >
    5           <bean class=“com.springDemo.beanDemo.Teacher">
    6      </property>
    7 </bean>
    复制代码
    在这里teacher作为一个内部bean被创建,并作为参数传递给 student 的构造器。
    内部bean仅适用于一次注入,而且也不能被其他的bean使用,所以内部bean时没有id的。外部也会去调用它
     
    使用spring的命名空间p装配属性
    Spring的命名空间p为我们提供了另一种bean属性的装配方式,这样就不需要添加过多的尖括号。如下:
    复制代码
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:p=“http://www.springframeword.org/schema/p">
     5     <bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”
     6      p:name=“liming”
     7      p.age=“18"
     8      p.teacher-ref=“Teacher"
     9 />
    10 </beans>
    复制代码
    P 是spring提供的一种简洁写法。
    p.teacher-ref=“Teacher” 将使用一个ID为Teacher的的bean引用了来装配teacher属性 -ref后缀作为一个标识来告知Spring应该装配一个引用而不是一个字面值
     
    装配集合:
    在前面我们演示了spring中单个属性以及对象的装配,那么在多个属性或者对象时一个集合类时,该如何来配置呢?
    spring为我们提供了4种类型的集合配置元素 
    • <list> : 装配list类型的值,允许重复
    • <set>  : 装配set类型的值,不允许重复
    • <map>  : 装配map类型的值,名和值允许类型
    • <props> : 装配properties类型的值,名称和值必须都是string类型
    当装配类型为数组或者collection类型时,那么list和set就非常有用了 
    <map>和<props> 对应的java中map和properties
    实例如下:
    复制代码
    1 <bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
    2      <property name=“studentList">
    3      <list>
    4           <ref bean=“getTeacherInfo”/>
    5           <ref bean=“getFirstStudnetInfo”/>
    6           <ref bean=“getscondStudentInfo”/>
    7      </list>
    8      </property>
    9 </bean>
    复制代码
    list中包含多个元素,ref用来定义spring上下文中的其他bean引用
    另外list类型的集合也可以用set来装配,不过也要确保list中的数据是唯一的。
    复制代码
    1 <bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
    2      <property name=“studentList">
    3      <set>
    4           <ref bean=“getTeacherInfo”/>
    5           <ref bean=“getFirstStudentInfo”/>
    6           <ref bean=“getsecondStudentInfo”/>
    7      </set>
    8      </property>
    9 </bean>
    复制代码
    装配map集合:
    复制代码
    1 <bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
    2      <property name=“studentList">
    3      <map>
    4           <entry key=“getTeacher” value-ref=“getTeacherInfo" />
    5           <entry key=“getFirseStudent” value-ref=“getFirstStudentInfo" />
    6           <entry key=“getSecondStudent” value-ref=“getsecondStudentInfo" />    
    7      </map>
    8      </property>
    9 </bean>
    复制代码
    <map>元素声明了一个java.util.map类型的值,每个entry元素定义一个map的一个成员,key属性定义了entry的值,value-ref属性定义了entry的值,并引用上下文中的其他bean的值。
    在entry中还有另外的属性来指定键和值
    • key : 指定map中entry的值为string
    • key-ref :指定map中entry的值为spring上下文中的其它bean的引用
    • value :指定map中entry值为string
    • value-ref : 指定map中entry的值为spring上下文中其它bean的引用
    装配properties集合
    如果所配置的map 键值对都是String时,可以使用properties代替properties,properties提供了和map相同的功能,但是它限定键值对都必须是string类型
    复制代码
    1 <bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
    2      <property name=“studentList">
    3      <props>
    4           <prop key=“getTeacher”>getTeacherInfo”</prop>
    5           <prop key=“getFirseStudent>getFirstStudentInfo</prop>
    6           <prop key=“getSecondStudent”>getsecondStudentInfo”</prop>
    7      </props>
    8      </properties>
    9 </bean>
    复制代码
    • <property> 元素用于把值或bean引入到Bean的属性中
    • <props> 元素用于定义一个 java properties 类型的集合值
    • <prop> 元素用于定义</prop> 元素中的成员
    使用表达式装配
    在前面装配bean中,所有的值在我们开发编程代码时就已经确定了,但是,当我们为属性装配的值只有在运行时才能知道该如何表示呢?
    spring3 为我们引入了spring表达式语言(spring Expression Language ,SpEL)SpEL是一种强大,简洁的装配Bean的方式,它通过运行期执行表达式的方式将值装配到Bean的属性和构造器参数中。使用SpEl,可以达到超乎想象的装配效果,这是使用传统的spring方式难以达到的。
    SpEL的使用特性包括:
    • 使用Bean的ID来引用Bean
    • 调用方法和访问对象的属性
    • 对值进行算数,关系和逻辑运算
    • 正则表达式匹配
    • 集合操作
    SpEL基本原理
    SpEL的首要目标是通过运算来计算来获取某个值。在计算这个数值的过程中,会使用到其他的值并会对这些值进行操作。
    字面值:
    复制代码
    1 <bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
    2      <property name=“studentList">
    3      <props>
    4           <prop key=“getTeacher”>getTeacherInfo”</prop>
    5           <prop key=“getFirseStudent>getFirstStudentInfo</prop>
    6           <prop key=“getSecondStudent”>getsecondStudentInfo”</prop>
    7      </props>
    8      </properties>
    9 </bean>
    复制代码
    property 通过在value中通过#{} 界定符把这个值装配到Bean中。
     
    引用Bean,Properties 和方法
    1 : 引用Bean 
    非SpEL 引用
    <property name=“student”  ref=“getStudent”/>
    SpEL引用
     
    在上面的SpEL表达式中,#{getTeacher.getName} getTeacher 为bean的ID,getName为该ID下的属性
    可见。它是可以直接 . 出来属性,方法,对象的
     
    <property name=“name” value=“#{getTeacher.getName()?.getage()}”/>
    ?. 表示判断一下 前面的getName() 是否为null 再来执行下面的getAge 否则会报异常的。
     
    操作类
    在SpEL中,使用T()会调用类作用域的方法和常量
    <property name=“multiplier” value=“#(T(java.lang.Math).PI)">
    T(java.lang.Math) 会返回一个 Math类型的对象。
    使用T 也可以调用一个静态方法
    <property name=“multiplier” value=“#(T(java.lang.Math).Random())"> 
    在表达式中也可以进行数值运算
    <property name=“add” value=“#{counter.tatol + 2}">
    <property name=“add” value=“#{counter.tatol - 2}">
    <property name=“add” value=“#{ 2 * T(java.lang.Math).Random() + counter.tatol - 2}">
    <property name=“add” value=“#{str1 + “ ” + str2 }”> 用于字符串连接
    比较值:
    <property name=“equal” value=“#{a+b==100}>
    如果value中的值为 true 则 会将 true装配给equal
     
  • 相关阅读:
    以正确的方式开源 Python 项目
    如果你喜欢Python 那么你不得不知的几个开源项目
    如何自学 Python(干货合集)
    不老的新丁 Python何以让人着迷
    流行的Python项目汇总
    AccountManager使用教程
    设计模式大全
    ubuntu12.04安装深度音乐播放器和深度影音
    完毕port(CompletionPort)具体解释
    关于jdbc注冊驱动的那点事
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/5679675.html
Copyright © 2020-2023  润新知