spring 杂烩
来源:互联网 发布:网络代销免费货源 编辑:程序博客网 时间:2024/05/01 06:20
Spring IoC容器的实例化非常简单,如下面的例子:Resource resource = new FileSystemResource("beans.xml");BeanFactory factory = new XmlBeanFactory(resource);ClassPathResource resource = new ClassPathResource("beans.xml");BeanFactory factory = new XmlBeanFactory(resource);ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"applicationContext.xml", "applicationContext-part2.xml"});// of course, an ApplicationContext is just a BeanFactoryBeanFactory factory = (BeanFactory) context;
在容器内部,这些bean定义由BeanDefinition 对象来表示,该定义将包含以下信息:
从本质上讲,BeanFactory仅仅只是一个维护bean定义以及相互依赖关系的高级工厂接口。通过BeanFactory我们可以访问bean定义。下面的例子创建了一个bean工厂,此工厂将从xml文件中读取bean定义:InputStream is = new FileInputStream("beans.xml");BeanFactory factory = new XmlBeanFactory(is);在<constructor-arg/>或<property/>元素内部还可以使用ref元素。<ref bean="someBean"/><ref local="someBean"/><!-- in the child (descendant) context --><bean id="accountService" <-- notice that the name of this bean is the same as the name of the 'parent' bean class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref parent="accountService"/> <-- notice how we refer to the parent bean </property> <!-- insert other configuration and dependencies as required as here --></bean>通过<list/>、<set/>、<map/>及<props/>元素可以定义和设置与Java Collection类型对应List、Set、Map及Properties的值。<bean id="moreComplexObject" class="example.ComplexObject"> <!-- results in a setAdminEmails(java.util.Properties) call --> <property name="adminEmails"> <props> <prop key="administrator">administrator@somecompany.org</prop> <prop key="support">support@somecompany.org</prop> <prop key="development">development@somecompany.org</prop> </props> </property> <!-- results in a setSomeList(java.util.List) call --> <property name="someList"> <list> <value>a list element followed by a reference</value> <ref bean="myDataSource" /> </list> </property> <!-- results in a setSomeMap(java.util.Map) call --> <property name="someMap"> <map> <entry> <key> <value>yup an entry</value> </key> <value>just some string</value> </entry> <entry> <key> <value>yup a ref</value> </key> <ref bean="myDataSource" /> </entry> </map> </property> <!-- results in a setSomeSet(java.util.Set) call --> <property name="someSet"> <set> <value>just some string</value> <ref bean="myDataSource" /> </set> </property></bean>注意:map的key或value值,或set的value值不能是以下元素:bean | ref | idref | list | set | map | props | value | null这等同于Java代码: exampleBean.setEmail("")。而null值则可以使用<null>元素可用来表示。例如:<bean class="ExampleBean"> <property name="email"><null/></property></bean>在XML配置文件中,延迟初始化将通过<bean/>元素中的lazy-init属性来进行控制。例如:在容器层次中通过在<beans/>元素上使用'default-lazy-init'属性来控制延迟初始化也是可能的UrlResource ClassPathResource FileSystemResource ServletContextResource InputStreamResource ByteArrayResource 实现org.springframework.validation.Validator接口中的两个方法,我们将为对Person类加上校验行为
0 0
- spring 杂烩
- 杂烩
- 杂烩
- 杂烩
- Java杂烩
- 电影杂烩
- vi杂烩
- Hibernate杂烩
- Linux杂烩
- 技术杂烩
- SSO杂烩
- iOS 杂烩
- iOS 杂烩
- iOS 杂烩
- 后端杂烩
- 杂烩一
- SNIFF的杂烩
- 服饰搭配杂烩
- java类方法对传入的参数的内存分配
- Golang 文件读取
- 基于大数据分析的异常检测方法及其思路实例
- java后台获取web项目全路径的方法
- 配置win7 iis后 本地连接网址 打不开网站或者一直在加载 网页加载不出来并且 提示下列错误
- spring 杂烩
- NOIP ++RP;
- Linux环境下的Mysql数据库安装与配置
- 软键盘弹出,ListView或者RecycleView中的内容上移实现
- android shape的使用详解以及常用效果(渐变色、分割线、边框、半透明阴影效果等)
- C++笔记--_CRT_SECURE_NO_WARNINGS
- leetcode_122. Best Time to Buy and Sell Stock II 多次买卖股票,求交易的最大利润
- iOS 保持界面流畅的技巧
- Jsp的pageContext,request,session,application4个域对象的属性作用范围