3.spring进阶运用
来源:互联网 发布:网络摄像头破解原理 编辑:程序博客网 时间:2024/06/06 09:33
1.配置Bean的属性和依赖关系
注入集合值:使用<list> <map> <set> <props>分别表示List、Map、Set或者Properties对象
<bean id="InjectCollections" class="com.deciphering.InjectCollections.InjectCollections"> <property name="sets"> <set> <value>1</value> <value>2</value> </set> </property> <property name="lists"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> <property name="maps"> <map> <entry key="1" value="1"></entry> <entry key="2" value="2"></entry> <entry key="3" value="3"></entry> <entry key="4" value="4"></entry> </map> </property> </bean>
2.管理Bean的生命周期
Spring容器中Bean的作用域:
- Singleton
- Prototype
- Request
- Session
- Global session
Spring提供两种方法在Bean全部属性设置成功后执行指定行为:
- 使用init-method
- 实现initializingBean接口
Spring提供两种方法在Bean实例销毁之前执行指定行为:
- 使用destory-method属性
- 实现DisposableBean接口
使用方法注入-协调作用域不同的Bean(singleton依赖prototype)
<bean id="helper" class="com.deciphering.bean.MyHelper" scope="prototype"> </bean> <bean id="abstractLookupBean" class="com.deciphering.bean.AbstractLookupDemo"> <lookup-method name="getMyHelper" bean="helper"/> </bean> <bean id="standardLookupBean" class="com.deciphering.bean.StandardLookupDemo"> <property name="myHelper" ref="helper"/> </bean>
public class StandardLookupDemo implements DemoBean {private MyHelper myHelper;public MyHelper getMyHelper() {return myHelper;}public void setMyHelper(MyHelper myHelper) {this.myHelper = myHelper;}@Overridepublic MyHelper getHelper() {// TODO Auto-generated method stubreturn this.myHelper;}@Overridepublic void someOperation() {// TODO Auto-generated method stubmyHelper.doSomethingHelpful();}}
public abstract class AbstractLookupDemo implements DemoBean {public abstract MyHelper getMyHelper();<span style="white-space:pre"></span>//spring框架负责实现@Overridepublic MyHelper getHelper(){return getMyHelper();}@Overridepublic void someOperation() {// TODO Auto-generated method stubgetMyHelper().doSomethingHelpful();}}
让Bean可以感知Spring容器
- 使用BeanNameAware接口
- 使用BeanFactoryAware接口、ApplicationContextAware接口
Spring的国际化支持:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basenames" ><list><value>message</value><!--如果有多个资源文件,全部列举在此--></list></property> </bean>
//实例化ApplicationContextApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");String [] a = {"读者"};//使用getMessage方法获得本地化信息。//返回计算机环境默认的LocaleString hello = ctx.getMessage("hello", a, Locale.getDefault());Object[] b = { new Date() };String now = ctx.getMessage("now", b, Locale.getDefault());//将两条本地化信息打印出来System.out.println(hello);System.out.println(now);//认为设置成英文环境hello = ctx.getMessage("hello", a, Locale.US);now = ctx.getMessage("now", b, Locale.US);//将两条英文信息打印出来System.out.println(hello);System.out.println(now);//认为设置成中文环境hello = ctx.getMessage("hello", a, Locale.CHINA);now = ctx.getMessage("now", b, Locale.CHINA);//将两条中文信息打印出来System.out.println(hello);System.out.println(now);
0 0
- 3.spring进阶运用
- Spring进阶运用
- vim运用进阶
- spring进阶
- Spring进阶
- spring 实例运用
- Spring 的AOP运用
- spring注解的运用
- Spring框架运用ajaxFileUpload
- Spring AOP 实战运用
- Google Analytics进阶运用之_setVar一点运用
- 进阶AlertView运用 - 登入设计
- Google Analytics进阶运用之搜索引擎跟踪
- Google Analytics进阶运用之事件跟踪
- Google Analytics进阶运用之调试代码
- grep进阶——运用正则表达式
- JavaScript进阶 系统对象的学习运用
- Java爬虫进阶-Selenium+PhantomJs的运用
- pthread_cleanup_push/pop
- mysql UNIX时间戳与日期的相互转换
- 【ios开发】关于NSPredicate的详解
- mysql修改密码
- Ant build.xml 批量打渠道包 加混淆功能
- 3.spring进阶运用
- Tweenjs中的Ease示例
- 打印 上一主题 下一主题 J2EE学习中一些值得研究的开源项目
- 连接池与数据源:DBCP以及C3P0的使用
- windows pipe
- SASS基础知识
- 基数排序
- 写在6月底
- 从苹果做搜索引擎想到的