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
原创粉丝点击