Spring 装配Bean入门级
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
> <bean id=“myBean” class=“com.springDemo.beanDemo.BeanIntance”></bean>
</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 支持属性占位符操作
“ new com.springDemo.beanDemo.BeanIntance(); ”
package com.springDemo.beanDemo public class BeanIntance{
public BeanIntance myBean(){ BeanIntance intance = new BeanIntance(); intance.setBeanname = “bean,….” ; return intance }
private String beanName;
public void setBeanName(string beanName){ this.beanName = beanName; }
public String getBeanName(){ return beanName;} }
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>
public class BeanIntance{
public BeanIntance myBean(){ BeanIntance intance = new BeanIntance(); intance.setBeanname = “bean,….” ; return intance }
private String beanName;
public void setBeanName(string beanName){ this.beanName = beanName; }
public String getBeanName(){ return beanName;}
public BeanIntance( String str ){ this.beanName = str ; }
}
<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>
<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容器管理,并由容器处理请求,生产动态内容)
pulic class student{
private String name;
public void setName(String name){this.name = name;}
public String getName(){return this.name;}
private int age;
public void setAge(int age){ this.age=age; }
public int getAge(){ return this.age; }
}
<bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
<property name=“name” value=“liming”/>
<property name=“age” value=“18”/>
</bean>
<bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
<property name=“name” value=“liming”/>
<property name=“age” value=“18”/>
<property name=“teacher” >
<bean class=“com.springDemo.beanDemo.Teacher">
</property>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p=“http://www.springframeword.org/schema/p">
<bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”
p:name=“liming”
p.age=“18"
p.teacher-ref=“Teacher"
/>
</beans>
- <list> : 装配list类型的值,允许重复
- <set> : 装配set类型的值,不允许重复
- <map> : 装配map类型的值,名和值允许类型
- <props> : 装配properties类型的值,名称和值必须都是string类型
<bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
<property name=“studentList">
<list>
<ref bean=“getTeacherInfo”/>
<ref bean=“getFirstStudnetInfo”/>
<ref bean=“getscondStudentInfo”/>
</list>
</property>
</bean>
<bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
<property name=“studentList">
<set>
<ref bean=“getTeacherInfo”/>
<ref bean=“getFirstStudentInfo”/>
<ref bean=“getsecondStudentInfo”/>
</set>
</property>
</bean>
<bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
<property name=“studentList">
<map>
<entry key=“getTeacher” value-ref=“getTeacherInfo" />
<entry key=“getFirseStudent” value-ref=“getFirstStudentInfo" />
<entry key=“getSecondStudent” value-ref=“getsecondStudentInfo" />
</map>
</property>
</bean>
- key : 指定map中entry的值为string
- key-ref :指定map中entry的值为spring上下文中的其它bean的引用
- value :指定map中entry值为string
- value-ref : 指定map中entry的值为spring上下文中其它bean的引用
<bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
<property name=“studentList">
<props>
<prop key=“getTeacher”>getTeacherInfo”</prop>
<prop key=“getFirseStudent>getFirstStudentInfo</prop>
<prop key=“getSecondStudent”>getsecondStudentInfo”</prop>
</props>
</properties>
</bean>
- <property> 元素用于把值或bean引入到Bean的属性中
- <props> 元素用于定义一个 java properties 类型的集合值
- <prop> 元素用于定义</prop> 元素中的成员
- 使用Bean的ID来引用Bean
- 调用方法和访问对象的属性
- 对值进行算数,关系和逻辑运算
- 正则表达式匹配
- 集合操作
<bean id=“getStudentInfo” class=“com.springDemo.beanDemo.Student”>
<property name=“studentList">
<props>
<prop key=“getTeacher”>getTeacherInfo”</prop>
<prop key=“getFirseStudent>getFirstStudentInfo</prop>
<prop key=“getSecondStudent”>getsecondStudentInfo”</prop>
</props>
</properties>
</bean>
<property name=“student” ref=“getStudent”/>
<property name=“multiplier” value=“#(T(java.lang.Math).Random())">
Spring 装配Bean入门级的更多相关文章
- Spring 装配Bean
Spring 装配Bean 装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用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中构造参数的注入
转载自: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 ...
随机推荐
- 三.野指针和free
在C语言项目中,经常会遇到需要程序员手动分配内存的地方.这样做能够节省大量的内存空间,也让程序更加灵活.只要你有一定的基础,那么肯定用过 malloc 或者 ralloc和free的组合.这个组合使用 ...
- 从0开始学习 GITHUB 系列之「GIT 进阶」【转】
本文转载自:http://stormzhang.com/github/2016/06/16/learn-github-from-zero5/ 版权声明:本文为 stormzhang 原创文章,可以随意 ...
- HDU 3820 Golden Eggs
http://acm.hdu.edu.cn/showproblem.php?pid=3820 题意:n*m的格子,每个格子放金蛋或银蛋,每个格子的金蛋和银蛋都有一个对应的点权,如果有两个金蛋相连,则需 ...
- Android -- Activity的生命周期,Activity四种启动模式 Standard, SingleTop,SingleTask,SingleInstance
1. 示例图 . 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: Activity的完整生命周期自第一次调用onCreate()开始,直至调 ...
- 优化 Redis 的使用策略
Redis Key 的命名策略 Redis 是 K-V 形式的缓存数据库,每一个需要缓存的 Object 都需要唯一的 Key 来标识.但是,我们日常在做开发的时候,经常会出现一个公司或者部门之间共用 ...
- Hive 建外链表到 Hbase(分内部表、外部表两种方式)
一. Hive 建内部表,链到hbase :特点:Hive drop表后,Hbase 表同步删除 drop table if exists hbase_kimbo_test1;CREATE TABLE ...
- office-word
目录(大纲) word中大纲的视图(也就是目录)是根据1/2/3级大纲决定的 格式刷 可以刷成一样的格式,字体,编号以及大纲等等. 主要用于编号和目录,快捷键(ctrl+shift) 编号设置(不建议 ...
- Python环境管理--virtualenvwrapper
遇到问题: 当最近的开发和部署过程中,多个服务器部署的时候发现对于库和包的管理非常混乱,主要有俩个版本问题: 因为业务需要,代码得分别部署在不同的服务器上面,每次部署的时候都得重复的安装包而且不能确定 ...
- 唯一索引 && 主键索引
唯一索引唯一索引不允许两行具有相同的索引值. 如果现有数据中存在重复的键值,则大多数数据库都不允许将新创建的唯一索引与表一起保存. 当新数据将使表中的键值重复时,数据库也拒绝接受此数据.例如,如果在 ...
- 关闭定时器(setTimeout/clearTimeout|setInterval/clearInterval)
1.1 开启Timeout程序: scope.setTimeout("functionName()" | functionHandle, timeValue) 返回值:timerI ...