Spring笔记 - Bean xml装配
命名空间表
|
aop |
Provides elements for declaring aspects and for automatically proxying @AspectJannotated classes as Spring aspects. |
|
beans |
The core primitive Spring namespace, enabling declaration of beans and how they |
|
context |
Comes with elements for configuring the Spring application context, including the abil- |
|
jee |
Offers integration with Java EE APIs such as JNDI and EJB. |
|
jms |
Provides configuration elements for declaring message-driven POJOs. |
|
lang |
Enables declaration of beans that are implemented as Groovy, JRuby, or BeanShell |
|
mvc |
Enables Spring MVC capabilities such as annotation-oriented controllers, view control- |
|
oxm |
Supports configuration of Spring’s object-to-XML mapping facilities. |
|
tx |
Provides for declarative transaction configuration. |
|
util |
A miscellaneous selection of utility elements. Includes the ability to declare collec- |
简单Bean配置
基本配置
<bean id="sonnet29" class="ch2_idol.Sonnet29"/>
构造器注入
<bean id="poeticJuggler" class="ch2_idol.PoeticJuggler">
<constructor-arg value="15"/>
<constructor-arg ref="sonnet29"/>
</bean>
静态方法注入
<bean id="stage" class="ch2_idol.Stage" factory-method="getInstance"/>
scoping方式默认是单例singletons.
<bean id="ticket" class="com.springinaction.springidol.Ticket" scope="prototype"/>
| singleton | Scopes the bean definition to a single instance per Spring container (default). |
| prototype | Allows a bean to be instantiated any number of times (once per use). |
| request | Scopes a bean definition to an HTTP request. Only valid when used with a web-capable Spring context (such as with Spring MVC). |
| session | Scopes a bean definition to an HTTP session. Only valid when used with a web-capable Spring context (such as with Spring MVC). |
| global-session | Scopes a bean definition to a global HTTP session. Only valid when used in a portlet context. |
初始化bean调用方法,销毁方法
<bean id="auditorium" class="ch2_idol.Auditorium" init-method="turnOnLights" destroy-method="turnOffLights"/>
也可以不配置,但实现InitializingBean ,DisposableBean接口
也可以在头部设置 default-init-method="turnOnLights" default-destroy-method="turnOffLights"所有bean初始化时调用,除非它有
属性注入
<bean id="instrumentalist" class="ch2_idol.Instrumentalist" >
<property name="song" value="JingleBells"></property>
<property name="instrument" ref="saxophone"></property>
</bean>
内部类
<bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
<property name="song"value="JingleBells"/>
<property name="instrument">
<bean class="org.springinaction.springidol.Saxophone"/>
</property>
</bean>
p标记
命名空间xmlns:p="http://www.springframework.org/schema/p
<bean id="kenny" class="com.springinaction.springidol.Instrumentalist"
p:song="JingleBells"
p:instrument-ref="saxophone"/>
集合
| <list> | Wiring a list of values, allowing duplicates |
| <set> | Wiring a set of values, ensuring no duplicates |
| <map> | Wiring a collection of name-value pairs where name and value can be of any type |
| <props> | Wiring a collection of name-value pairs where the name and value are both Strings |
<bean id="oneManBand1" class="ch2_idol.OneManBand">
<property name="instruments">
<list>
<ref bean="saxophone"/>
<ref bean="piano"/>
<ref bean="piano"/>
</list>
</property>
</bean>
<!-- 会去重 -->
<bean id="oneManBand" class="ch2_idol.OneManBand">
<property name="instruments">
<set>
<ref bean="saxophone"/>
<ref bean="piano"/>
<ref bean="piano"/>
</set>
</property>
</bean>
<property name="instruments">
<map>
<entry key="GUITAR" value-ref="guitar"/>
<entry key="CYMBAL" value-ref="cymbal"/>
<entry key="HARMONICA" value-ref="harmonica"/>
</map>
</property>
<property name="instruments">
<props>
<prop key="GUITAR">STRUMSTRUMSTRUM</prop>
<prop key="CYMBAL">CRASHCRASHCRASH</prop>
<prop key="HARMONICA">HUMHUMHUM</prop>
</props>
</property>
空标记
<property name="instruments"><null/></property>
Spring笔记 - Bean xml装配的更多相关文章
- Spring 之通过 XML 装配 bean
1.关于 使用传统标签还是 c- p- 命名空间定义的标签, 我的观点是能用 c- p- 命名空间定义的标签 就不用 传统标签(这样会比较简洁... 2.强依赖使用构造器注入,可选性依赖使用属性注入 ...
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...
- Spring温故而知新 - bean的装配(续)
按条件装配bean 就是当满足特定的条件时Spring容器才创建Bean,Spring中通过@Conditional注解来实现条件化配置bean package com.sl.ioc; import ...
- Spring温故而知新 - bean的装配
Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...
- Spring 中Bean的装配方式
最近又买了一本介绍SSM框架的书,是由黑马程序员编写的,书上讲的很好理解,边看边总结一下.主要总结一下bean的装配方式. Bean的装配可以理解为依赖系统注入,Bean的装配方式即Bean依赖注入的 ...
- Spring温故而知新 – bean的装配
Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...
- Spring ( 三 ) Spring的Bean的装配与生命周期、专用测试
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.对象的生命周期 1.IOC之Bean的生命周期 创建带有生命周期方法的bean public cla ...
- java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊
Spring 1.1.1.1 创建一个bean package com.zt.spring; public class MyBean { private String userName; privat ...
- [spring]spring的bean自动装配机制
7.bean的自动装配 是spring满足bean依赖的一种方式 spring会在上下文中自动寻找,并自动给bean装配属性 spring的装配方式: (1)手动装配 在people类中依赖了cat和 ...
随机推荐
- SpringMVC请求访问不到静态文件解决方式
如何你的DispatcherServlet拦截"*.do"这样的有后缀的URL,就不存在访问不到静态资源的问题. 如果你的DispatcherServlet拦截"/&qu ...
- Apache+php+mysql+phpadmin搭建
一,准备工具. httpd-2.4.12-win32-VC9.zip php-5.4.40-Win32-VC9-x86.zip phpMyAdmin-4.2.13.2-all-languages my ...
- windos下安装PEAR 注意
1.在这里下载PEAR http://pear.php.net/go-pear.phar 在页面右键另存为 go-pear.phar 到PHP的根目录,并去目录查看是否保存为了go-pear.phar ...
- Nginx阅读笔记(四)之root和alias
nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri ...
- maven GroupID和ArtifactID填什么
GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构. ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.一般Gro ...
- perl学习(1) 入门
Perl 被设计成90%擅长处理文本,10%处理其余的问题.因此Perl 有强大的文本处理能力,包括正则表达式. 第一个程序 hello world #! /usr/bin/perl -w use s ...
- JAVA中IO和异常处理练习
1.SystemI\O练习:1)通过键盘输入若干字符,并通过显示器输出:2)定义一个静态方法validOrNot()用于对输入的字符进行合法性校验,若包含非英文字符,则抛出IllegalStringE ...
- Android apk逆向实战
简介 逆向Android apk其实是一个分析Android apk的一个过程,必须了解Android程序开发的流程.结构.语句分支.解密原理等等. 功能 破解一个注册验证程序(自写一个简单的注册验证 ...
- 利用CentOS系统IPtables防火墙添加网站IP白名单
参考博文: 利用CentOS系统IPtables防火墙添加360网站卫士节点IP白名单 centos6.5添加白名单如下: 在防火墙 配置文件中加入白名单 ip -A INPUT -s 183.13 ...
- Python监控网站运行状况
利用python便捷的类库,可以方便快速实现对网站运行状况的监控,主要包括对80端口(即网站运行端口),其它tcp服务等端口的监控就可以了解服务器大概的一个运行状况,使用的库主要为urllib2及so ...