Spring 学习笔记(9-30)
来源:互联网 发布:apache velocity 编辑:程序博客网 时间:2024/05/22 14:54
Spring 学习笔记9-30
一.自动匹配:
1.Byname模式指的是通过Bean属性的名字进行自动匹配、在Spring的配置文档XML中,查找一个与将要装配的属性同样的名字的Bean
2.ByType如果XML中正好存在一个与属性类型一样的Bean,则自动填充,若有多个则抛出异常
3.ByConstructor如果XML中正好存在一个与构造函数参数一样的Bean,则自动填充
4.autodetect模式指的是通过对Bean检查类的内部选择constructor或byType先找到哪个则用哪个
5.no模式则代表不使用自动装置
二.Bean的依赖检查的四种模式:
自动装配是隐式的不能像ref属性指定依赖那么直接
1.Simple:对基本类型 字符串 集合进行依赖检查
2.Object模式指对依赖的对象进行检查
3.All模式指的是对全部属性进行依赖检查
4.None模式指的是不进行依赖检查
三。集合的注入
<bean id="collectTest"class="com.spring.demo.CollectionDemo">
<property name="list"><!--set注入 -->
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="set"><!--set注入 -->
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="map"><!--set注入 -->
<map>
<entry value="map"key="1"></entry>
<entry value="map"key="2"></entry>
<entry value="map"key="3"></entry>
<entry value="map"key="4"></entry>
</map>
</property>
<property name="properties"><!--set注入 -->
<props>
<prop key="jdbc">
mysql
</prop>
</props>
</property>
</bean>
四.管理Bean
1.BeanWarpper:org.springframework.beans包中的BeanWarpper接口及它的实现BeanWarpperImpl BeanWarpper封装了一个Bean的行为具有设置和获得属性的功能
Object ob=Class.forName("com.spring.demo.InitDemo").newInstance();
BeanWrapper bp=newBeanWrapperImpl(ob);
bp.setPropertyValue("id",1);
bp.setPropertyValue("name","BeanWarpper");
System.out.println(ob.toString());
2.BeanFactory管理:(已过时)
3.ApplicationContext管理Bean
ApplicationContext app=newFileSystemXmlApplicationContext("path");
CollectionDemo collect=(CollectionDemo)app.getBean("collectTest");
ApplicationContext 接口实现:
public interfaceApplicationContext extendsEnvironmentCapable,ListableBeanFactory,HierarchicalBeanFactory,MessageSource,ApplicationEventPublisher,ResourcePatternResolver
可以看出Application可以看做是BeanFactory的一个超集 其继承的其他BeanFactory接口类为ListableBeanFatory HierarchicalBeanFactory接口类的子类
五.国际化支持
ApplicationContext实现了MessageSource接口,通过其getMessage各个方法获得信息资源
配置文档配置:
必须将Bean定义为messageSource,通过org.springframework.context.support.ResourceBudleMessageSource来获得国际化信息
六.事件传递
通过ApplicationEvent类和ApplicationListener接口来提供,通过ApplicationContextAware中的publishEvent()方法通知ApplicationListener
(1)实现继承ApplicationEvent的类LogEvent,这个类通过ApplicationContext被发布出去
(2)实现ApplicationListener接口的类LogListener发布logevent事件时通知LogListener,并输出相应的消息
(3)实现一个ApplicationContextAware接口的类log,通过PublishEvent()方法,带入LogEvent作为参数通知LogListener
(1)被发布的事件:在Spring中,这个角色继承了ApplicationEvent类。
public classLogEvent extendsApplicationEvent {
publicLogEvent(Object object){
super(object);
}
}
(2)实现了ApplicationListener接口的监听者
public classLogListener implementsApplicationListener {
public voidonApplicationEvent(ApplicationEvent applicationEvent) {
if(applicationEventinstanceof LogEvent ){
SimpleDateFormat format=newSimpleDateFormat("yyyy-MM--dd HH:mm:ss");
format.setLenient(false);
String currentDate=format.format(newDate());
System.out.println(currentDate+":"+applicationEvent);
}
}
}
(3)事件的发布者则实现了ApplicationContextAware接口,并调用publishEvent方法发布事件。
public classJavaAware implementsApplicationContextAware{
privateApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
this.applicationContext=applicationContext;
}
public intlog(String log){
LogEvent event=newLogEvent(log);
this.applicationContext.publishEvent(event);
return 0;
}
}
方法调用:(Spring配置文件xml的地址自己进行配置)
ApplicationContext app=newFileSystemXmlApplicationContext("");
JavaAware aware=(JavaAware)app.getBean("log");
aware.log("testLog");
运行结果:
2017-10--01 20:01:06:com.spring.demo.LogEvent[source=testLog]
- Spring 学习笔记(9-30)
- Spring学习笔记9
- spring学习笔记(9)--@Resource
- spring gossip(spring 学习笔记)
- spring学习笔记: Spring AOP
- spring学习笔记: Spring Jdbc
- spring学习笔记:spring事务管理
- spring学习笔记:spring事务管理
- spring学习笔记:spring controller
- spring学习笔记:spring事务管理
- Spring 学习笔记:浅谈 Spring
- spring学习笔记: Spring AOP
- Spring学习笔记-Spring容器
- Spring学习笔记 - Spring Data
- Spring学习笔记-Spring Cache
- Spring学习笔记 - Spring Boot
- Spring学习笔记: Spring概述
- Spring学习笔记一(2005.12.30)
- Codeforces Round #437 (Div. 2) B 865ASave the problem! (思维)
- 树遍历方式总结:层次遍历、先序遍历、中序遍历、后序遍历
- [数论][莫比乌斯反演] BZOJ 4816: 数字表格
- 单源最短路径:迪杰斯特拉算法
- JAVA异常
- Spring 学习笔记(9-30)
- KMP算法过程
- PHP JavaScript横向总结、对比(2.数组,类(对象))
- TX2刷机
- PHP字符串函数
- poj3565 Ants(KM)
- hive一直卡住不动,jps时ResourceManager 没启动
- 关于char 与cchar[]的输入流
- python的range()函数用法