Bean   就是一个类

    一下代码都是基于spring之IOC和DI实现案例基础上进行解析

  Bean的实例化方式:

      1.无参构造

<bean id="UserService" class="com.baidu.test.UserServiceImp">
<property name="name" value="张三"></property>
</bean>

      2.静态工厂方法

      applicationContext.xml

     <bean id="UserService" class="com.baidu.test.BeanFactory2" factory-method="CreateUserServiceImp">
<property name="name" value="张三"></property>
</bean>

            创建BeanFactory2类

public class BeanFactory2 {
public static UserService CreateUserServiceImp(){
return new UserServiceImp();
}
}

      3.实例工厂方法

    <bean name="BeanFactory2" class="com.baidu.test.BeanFactory2"></bean>
<bean id="UserService" factory-bean="BeanFactory2" factory-method="CreateUserServiceImp">
<property name="name" value="张三"></property>
</bean>

  创建BeanFactory2类

public class BeanFactory2 {
public UserService CreateUserServiceImp(){
return new UserServiceImp();
}
}

    Bean的作用域

在bean的属性中有个scope,用来描述bean的作用域.

singleton 单例   spring IOC容器中只有一个Bean实类(默认的scope)
prototype 多例   从spring容器中获取时都会返回一个新的实例
request 将bean对象request.setAttribute()存储到request域中
session 将bean对象session.setAttribute()存储到session域中

开发中常用singleton和prototype(单例和多例)

    Bean的属性注入

      1.构造方法注入

        提供构造方法

    public UserServiceImp(String name) {
super();
this.name = name;
}

        修改applicationContext.xml文件

 <bean id="UserService" class="com.baidu.test.UserServiceImp">
<constructor-arg index="0" value="张三" type="java.lang.String"></constructor-arg>
</bean>

      2.Setter注入

    修改applicationContext.xml

     <bean id="user" class="com.baidu.test.User">
<property name="name" value="uuu"></property>
<property name="age" value="15"></property>
</bean>
<bean id="UserService" class="com.baidu.test.UserServiceImp">
<property name="name" value="uu"></property>
<property name="u" ref="user"></property>//引入另一个bean
</bean>

      3.map注入

       <bean id="user" class="com.baidu.test.User">
<property name="name" value="uuu"></property>
<property name="age" value="15"></property>
</bean>
<bean id="UserService" class="com.baidu.test.UserServiceImp">
<property name="name" value="uu">
</property>
<property name="list">
<list>
<value>10</value>
<value>张三</value>
<ref bean="user"/>
</list>
</property>
<property name="set">
<set>
<value>张三</value>
<ref bean="user"/>
</set>
</property>
<property name="map">
<map>
<entry key="1" value-ref="user"></entry>
<entry key="2" value="12"></entry>
</map>
</property>
</bean>

  空间名称p和c

  引入schma

      xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
首先它们不是真正的名称空间,是虚拟的。它是嵌入到 spring 内核中的。
使用 p 名称空间可以解决我们 setter 注入时<property>简化
使用 c 名称空间可以解决我们构造器注入时<constructor-arg>简化

applicationContext.xml

    p名称空间 ---------    setter注入

    <bean id="user" class="com.baidu.test.User">
<property name="name" value="uuu"></property>
<property name="age" value="15"></property>
</bean>
<bean id="UserService" class="com.baidu.test.UserServiceImp" p:name="uu" p:u-ref="user" >
</bean>

  

    c名称空间-------  构造器注入

 <bean id="UserService" class="com.baidu.test.UserServiceImp" c:name="uu" c:u-ref="user" > </bean>

    SPEL(spring expression language)  spring 3.0版本以后

它类似于 ognl 或 el 表达式,它可以提供在程序运行时构造复杂表达式来完成对象属性存储
及方法调用等。
Spel 表达式的格式 #{表达式}

  applicationContext.xml

  1.完成bean之间的注入

     <bean id="user" class="com.baidu.test.User">
<property name="name" value="uuu"></property>
<property name="age" value="15"></property>
</bean>
<bean id="UserService" class="com.baidu.test.UserServiceImp">
<property name="name" value="uu"></property>
          <!-- <property name="u" ref="user"></property>->
<property name="u" value="#{user}"></property>替代上面的ref值(完成bean之间的注入)
</bean>

  2.支持属性调用和即方法调用

    <bean id="user" class="com.baidu.test.User">
<property name="name" value="uuu"></property>
<property name="age" value="15"></property>
</bean>
<bean id="user1" class="com.baidu.test.User">
<property name="name" value="uuu"></property>
<property name="age" value="#{user.getAge()+5}"></property>
</bean>
<bean id="UserService" class="com.baidu.test.UserServiceImp">
<property name="name" value="uu"></property>
<property name="u" value="#{user1}"></property>
</bean>

  

  

spring配置Bean的更多相关文章

  1. spring 配置bean

    Main(测试方法) public class Main { public static void main(String[] args) { //1.创建Spring 的IOC容器对象: //spr ...

  2. Spring配置bean的详细知识

    在Spring中配置bean的一些细节.具体信息请参考下面的代码及注释 applicationContext.xml文件 <?xml version="1.0" encodi ...

  3. Spring -- 配置bean的三种方法

    配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...

  4. Spring配置Bean,为属性赋值

    SayHello的实体类: package com.langchao; /** * @ClassName: SayHello * @description: * @author: ZhangYawei ...

  5. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  6. Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系

    XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...

  7. Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节

    在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...

  8. spring 配置bean的方法及依赖注入发方式

    Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWo ...

  9. Spring入门第二课:Spring配置Bean的细节

    1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...

  10. spring 配置bean-自己主动装配

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28260477 概要:(蓝色为本节所讲) ...

随机推荐

  1. React-router4 第四篇 Custom Link 自定义链接

    直接贴代码 虽说我这么懒的人应该不会自定义标签,何必呢,,但是我还是看了官方的例子 直接抄过来, exact 属性:根据我的测试,这个属性应该和路由的精确匹配有关有关,当值为true时,路由是会精确匹 ...

  2. Jenkins与SVN持续集成

    官网下载Jenkins&SVN&eclipse,版本号没要求,建议使用最新稳定版本 登录Jenkins:http://localhost:8080 登录SVN:http://local ...

  3. Soa思想分布式服务webservice WCF

    什么是分布式事务 分布式事务就是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.以上是百度百科的解释,简单的说,就是一次大的操作由不同的小操作组成,这 ...

  4. angular2.0学习笔记1.开发环境搭建 (node.js和npm的安装)

    开发环境, 1.安装Node.js®和npm, node 6.9.x 和 npm 3.x.x 以上的版本. 更老的版本可能会出现错误,更新的版本则没问题. 控制台窗口中运行命令 node -v 和 n ...

  5. DataStructure.BloomFilter

    Bloom Filters Ref[1] 1. 简介 Bloom filter(布隆过滤器:有更好的或正确的翻译,告诉我) 是一个数据结构,该数据结构快速并且内存高效,它可以告诉你某个元素是否在集合中 ...

  6. 简单的node 服务端 响应get,返回json数据;

    原文:http://blog.csdn.net/xn_28/article/details/50837019 const http = require('http'); const hostname ...

  7. Eclipse中配置Tomcat服务器并创建标准Web目录

    Eclipse创建 Java Web 项目,并生成标准的目录结构 file --> New --> Dynamic Web project 填写 Project name (该名称项目的名 ...

  8. @PostConstruct和@PostConstruct 注解 及ehcache 缓存 执行过程 小记

    @PostConstruct 和@PostConstruct 注解 从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstr ...

  9. [Hbase]Hbase容灾方案

    介绍两种HBase的数据备份或者容灾方案:Snapshot,Replication: 一.Snapshot 开启快照功能,在hbase-site.xml文件中添加如下配置项: <property ...

  10. 解疑网络监控卡壳 视觉体验400ms延时

    http://security.zol.com.cn/413/4130220.html 我的眼睛可以轻松判断出400ms延时误差,你可以吗?很多用户都反映手机移动监控.PC远程监控视频会卡顿,抛开设备 ...