Spring 装配Bean
Spring 装配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: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>

- 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 支持属性占位符操作

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 }

ApplicationContext ctx = new classPathXmlApplicationContext(“spring-beans.xml”); // 加载spring-beans.xml 文件
BeanIntance beanIntance = (BeanIntance)ctx.getBean(“myBean”); // 读取mybean组件,
<bean id=“mybean” class=“com.springDemo.beanDemo.BeanIntance">
<constructor-arg value=“hello”/>
</bean>

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 id=“Sonbean” class=“com.springDemo.beanDemo.SonBeanIntance”/>
<bean id=“mybean” class=“com.springDemo.beanDemo.BeanIntance">
<constructor-arg value=“hello”/>
<constructor-arg ref=“Sonbean”/>
</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容器管理,并由容器处理请求,生产动态内容)

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>

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>


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>

- <list> : 装配list类型的值,允许重复
- <set> : 装配set类型的值,不允许重复
- <map> : 装配map类型的值,名和值允许类型
- <props> : 装配properties类型的值,名称和值必须都是string类型

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>


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>


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>

- key : 指定map中entry的值为string
- key-ref :指定map中entry的值为spring上下文中的其它bean的引用
- value :指定map中entry值为string
- value-ref : 指定map中entry的值为spring上下文中其它bean的引用

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的ID来引用Bean
- 调用方法和访问对象的属性
- 对值进行算数,关系和逻辑运算
- 正则表达式匹配
- 集合操作

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 name=“student” ref=“getStudent”/>
<property name=“multiplier” value=“#(T(java.lang.Math).Random())">
Spring 装配Bean的更多相关文章
- Spring装配bean
Spring配置的可选方案 Spring提供了如下三种装配机制: (1)在XML中显式配置 (2)在Java中显式配置 (3)隐式的bean发现机制和自动装配 Spring有多种方式可以装配bean, ...
- Spring装配Bean之XML装配bean
在Spring刚出现的时候,XML是描述配置的主要方式,在Spring的名义下,我们创建了无数行XML代码.在一定程度上,Spring成为了XML的同义词. 现在随着强大的自动化配置和Java代码的配 ...
- Spring装配Bean的过程补充
对上一篇的<Spring装配Bean的过程>的过程说一下,不然真产生了误区. 误区在哪里呢?那就是spring bean的作用域问题. 说哈常用的两种作用域:默认是scope = sing ...
- Spring装配Bean的过程
首先说一个概念:“懒加载” 懒加载:就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. spring配置文件中be ...
- Spring 装配Bean入门级
装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介绍 1 :声明Bean B ...
- 【转】spring 装配Bean中构造参数的注入
转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...
- Spring装配Bean之组件扫描和自动装配
Spring从两个角度来实现自动化装配: 组件扫描:Spring会自动发现应用上下文中所创建的bean. 自动装配:Spring自动满足bean之间的依赖. 案例:音响系统的组件.首先为CD创建Com ...
- Spring装配Bean之Java代码装配bean
尽管通过组件扫描和自动装配实现Spring的自动化配置很方便也推荐,但是有时候自动配置的方式实现不了,就需要明确显示的配置Spring.比如说,想要将第三方库中的组件装配到自己的应用中,这样的情况下, ...
- spring装配Bean过程
主要流程: 1.读取配置文件 2.实例化bean和填充bean属性 这个粗略的流程感觉更像是一个需求,有了这个需求,那么spring内部是怎么处理的呢? 我们知道spring的两个核心接口BeanFa ...
随机推荐
- JAVA 年老代收集器 第10节
JAVA 年老代收集器 第10节 上一章我们讲了新生代的收集器,那么这一章我们要讲的就是关于老年代的一些收集器.老年代的存活的一般是大对象以及生命很顽强的对象,因此新生代的复制算法很明显不能适应该区域 ...
- 如何在Eclipse中开发并调试自己的插件(或者说如何将自己的代码插件化)
Setting up Eclipse to create and debug plugins for ImageJ 最近在做一个关于卫星遥感全链路仿真的项目,由于项目是基于ImageJ开发,而Imag ...
- 加密传输SSL协议7_SSL协议概述
SSL(Secure Sockets Layer) SSL的功能,可以在通信的双方中建立一个加密的通信通道 同时还可以确认通信的双方是不是就是其声称的人,防止被钓鱼. SSL在网络协议栈中的位置:可以 ...
- OpenID倡议:别再创建新的用户名和密码
原文作者:Jeff Atwood 随着Stack Overflow开发工作的逐步深入,我们不可回避地碰到了这个问题:我们需要让用户登录,即使网络上用户名/密码已经泛滥成灾,我们也只能随波逐流.我已经有 ...
- php随笔2-php+ajax 实现输入读取数据库显示匹配信息
dropbox_index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- 得到IP包的数据意义(简单实现例子)
#include <stdio.h> #include <unistd.h> #include <linux/if_ether.h> #include <li ...
- 如何在Delphi中调用VC6.0开发的COM
上次写了如何在VC6.0下对Delphi写的COM进行调用,原本想马上写如何在Delphi中调用VC6.0开发的COM时,由于在写事例程序中碰到了个很怪的问题,在我机子上用VC写的接口程序编译能通过. ...
- DbConnectionFactory 数据库连接
/** * */package com.sprucetec.dbatch.tmsfee;import java.io.Serializable;import java.sql.Connection;i ...
- poj2136---输出特殊图形
#include <stdio.h> #include <stdlib.h> #include<string.h> ]; int find(int pos,int ...
- CouldnotcreateServerSocketonaddress0.0.0.0/0.0.0.0:9083
错误记录 安装的时候遇到了如下错误 Exception in thread "main" org.apache.thrift.transport.TTransportExcepti ...