Java Tutorial/Spring/Properties Injection — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:22, 31 мая 2010

Bean Injection: Collection List

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="collectionsDemo" class="CollectionsDemo">
        <property name="map">
            <map>
                <entry key="someValue">
                    <value>Hello World!</value>
                </entry>
                <entry key="someBean">
                    <ref local="oracle"/>
                </entry>
            </map>
        </property>
        <property name="props">
            <props>
                <prop key="firstName">
                    Jan
                </prop>
                <prop key="secondName">
                    Machacek
                </prop>
            </props>
        </property>
        <property name="set">
            <set>
                <value>Hello World!</value>
                <ref local="oracle"/>
            </set>
        </property>
        <property name="list">
            <list>
                <value>Hello World!</value>
                <ref local="oracle"/>
            </list>
        </property>
    </bean>
    <bean id="oracle" class="BookwormOracle"/>
</beans>





Bean Injection: Collection Map

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="collectionsDemo" class="CollectionsDemo">
        <property name="map">
            <map>
                <entry key="someValue">
                    <value>Hello World!</value>
                </entry>
                <entry key="someBean">
                    <ref local="oracle"/>
                </entry>
            </map>
        </property>
        <property name="props">
            <props>
                <prop key="firstName">
                    Jan
                </prop>
                <prop key="secondName">
                    Machacek
                </prop>
            </props>
        </property>
        <property name="set">
            <set>
                <value>Hello World!</value>
                <ref local="oracle"/>
            </set>
        </property>
        <property name="list">
            <list>
                <value>Hello World!</value>
                <ref local="oracle"/>
            </list>
        </property>
    </bean>
    <bean id="oracle" class="BookwormOracle"/>
</beans>





Bean Injection Collection Properties

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="collectionsDemo" class="CollectionsDemo">
        <property name="map">
            <map>
                <entry key="someValue">
                    <value>Hello World!</value>
                </entry>
                <entry key="someBean">
                    <ref local="oracle"/>
                </entry>
            </map>
        </property>
        <property name="props">
            <props>
                <prop key="firstName">
                    Jan
                </prop>
                <prop key="secondName">
                    Machacek
                </prop>
            </props>
        </property>
        <property name="set">
            <set>
                <value>Hello World!</value>
                <ref local="oracle"/>
            </set>
        </property>
        <property name="list">
            <list>
                <value>Hello World!</value>
                <ref local="oracle"/>
            </list>
        </property>
    </bean>
    <bean id="oracle" class="BookwormOracle"/>
</beans>





Bean Injection: Collection Set

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="collectionsDemo" class="CollectionsDemo">
        <property name="map">
            <map>
                <entry key="someValue">
                    <value>Hello World!</value>
                </entry>
                <entry key="someBean">
                    <ref local="oracle"/>
                </entry>
            </map>
        </property>
        <property name="props">
            <props>
                <prop key="firstName">
                    Jan
                </prop>
                <prop key="secondName">
                    Machacek
                </prop>
            </props>
        </property>
        <property name="set">
            <set>
                <value>Hello World!</value>
                <ref local="oracle"/>
            </set>
        </property>
        <property name="list">
            <list>
                <value>Hello World!</value>
                <ref local="oracle"/>
            </list>
        </property>
    </bean>
    <bean id="oracle" class="BookwormOracle"/>
</beans>





Bean Injection: Map

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="ftpDownloader" class="FtpDownloader"/>
    <bean id="httpDownloader" class="HttpDownloader"/>
    <bean id="sftpDownaloader" class="SftpDownloader"/>
    <bean id="downloadManager" class="DownloadManager">
        <property name="downloaders">
            <map>
                <entry key="ftp" value-ref="ftpDownloader"/>
                <entry key="http" value-ref="httpDownloader"/>
                <entry key="sftp" value-ref="sftpDownaloader"/>
            </map>
        </property>
    </bean>

</beans>





Create List, Map In Context

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <bean id="transactionManager" class="MyTransactionManager"/>
    <util:constant id="X" static-field="java.lang.Integer.MAX_VALUE"/>
    <util:list id="Y" list-class="java.util.ArrayList">
        <value>value1</value>
        <ref bean="X"/>
    </util:list>
    <util:list id="greetingsList">
        <value>Hello, world</value>
        <value>How are you doing today?</value>
    </util:list>
    <util:map id="Z" map-class="java.util.HashMap">
        <entry key="x" value="y"/>
        <entry key="y"><ref bean="X"/></entry>
    </util:map>
    <util:properties id="P" location="classpath:Main.properties"/>
    <bean id="simple" class="SimpleBean"/>
    <util:property-path id="Q" path="simple.name"/>
    <util:set id="S" set-class="java.util.HashSet">
        <value>foo</value>
        <ref bean="X"/>
    </util:set>
</beans>





Fill Calendar Object To List

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theList">
      <list>
        <value>red</value>
        <value>red</value>
        <value>blue</value>
        <ref local="curDate"/>
        
      </list>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>





Fill List To Another List

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theList">
      <list>
        <value>red</value>
        <value>red</value>
        <value>blue</value>
        <list>
          <value>one</value>
          <value>two</value>
          <value>three</value>
        </list>
        
      </list>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>





Fill Map

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theMap">
      <map>
        <entry key="left">
          <value>right</value>
        </entry>
        <entry key="up">
          <value>down</value>
        </entry>
        <entry key="date">
          <ref local="curDate"/>
        </entry>
      </map>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>





Fill Properties

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    

    <property name="theProperties">
      <props>
        <prop key="left">right</prop>
        <prop key="up">down</prop>
      </props>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>





Fill Set

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theSet">
      <set>
        <value>red</value>
        <value>red</value>
        <value>blue</value>
      </set>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>





Fill Value To List

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="collectionsExample" class="CollectionsBean">
    
    <property name="theList">
      <list>
        <value>red</value>
        <value>red</value>
        <value>blue</value>
      </list>
    </property>
  </bean>
  
  <bean id="curDate" class="java.util.GregorianCalendar"/>
  
</beans>





Load property File In Context

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <bean id="transactionManager" class="MyTransactionManager"/>
    <util:constant id="X" static-field="java.lang.Integer.MAX_VALUE"/>
    <util:list id="Y" list-class="java.util.ArrayList">
        <value>value1</value>
        <ref bean="X"/>
    </util:list>
    <util:list id="greetingsList">
        <value>Hello, world</value>
        <value>How are you doing today?</value>
    </util:list>
    <util:map id="Z" map-class="java.util.HashMap">
        <entry key="x" value="y"/>
        <entry key="y"><ref bean="X"/></entry>
    </util:map>
    <util:properties id="P" location="classpath:Main.properties"/>
    <bean id="simple" class="SimpleBean"/>
    <util:property-path id="Q" path="simple.name"/>
    <util:set id="S" set-class="java.util.HashSet">
        <value>foo</value>
        <ref bean="X"/>
    </util:set>
</beans>





Properties Setting: Date

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  
  <bean id="customEditorConfigurer"
        class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors">
      <map>
        <entry key="java.util.Date">
          <bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
            <constructor-arg index="0">
              <bean class="java.text.SimpleDateFormat">
                <constructor-arg><value>M/d/yy</value></constructor-arg>
              </bean>
            </constructor-arg>
            <constructor-arg index="1"><value>true</value></constructor-arg>
          </bean>
        </entry>
        
      </map>
    </property>
  </bean>
  <bean id="startEndDatesBean" class="StartEndDatesBean">
    <property name="startDate"><value>10/09/2001</value></property>
    <property name="endDate"><value>10/26/2008</value></property>
  </bean>
</beans>





Property Setting: BigDecimal

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myAmount" value="1000000"/>
  </bean>
</beans>





Property Setting Boolean

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myToggle" value="false"/>
  </bean>
</beans>





Property Setting Byte Array

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myBytes" value="some bytes"/>
  </bean>
</beans>





Property Setting Class type

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myClass" value="java.util.Collection"/>
  </bean>
</beans>





Property Setting File

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myFile" value="placeholder.txt"/>
  </bean>
</beans>





Property Setting Integer

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myNumber" value="500"/>
  </bean>
</beans>





Property Setting Properties

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
        <property name="myProperties">
          <value>
          firstname=f
          lastname=l
          </value>
        </property>
  </bean>
</beans>





Property Setting Stream From URL

File: Main.java



import java.io.InputStream;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    PropertyEditorTestBean testBean = (PropertyEditorTestBean) beanFactory.getBean("testBean");
    System.out.println(testBean.getMyInputStream());
  }
}
class PropertyEditorTestBean {
  private InputStream myInputStream;
  public InputStream getMyInputStream() {
    return myInputStream;
  }
  public void setMyInputStream(InputStream myInputStream) {
    this.myInputStream = myInputStream;
  }
}





Property Setting String Array

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myStrings" value="A,B,C,D"/>
  </bean>
</beans>





Property Setting: URL

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="testBean" class="PropertyEditorTestBean">
    <property name="myUrl" value="http://www.jexp.ru"/>
  </bean>
</beans>