• spring框架学习笔记(三)


    接上一节,配置bean的关联关系:

     新增bean实体类Manufacture 代码如下:

     其中要在Manufacture中包含对ProductEntity的引用。

    public class Manufacture {
    	private String manName;
    	private ProductEntity product;
    	
    	
    	/**
    	 * 
    	 * @return the manName
    	 */
    	public String getManName() {
    		return manName;
    	}
    
    
    	/**
    	 * @param manName the manName to set
    	 */
    	public void setManName(String manName) {
    		this.manName = manName;
    	}
    
    
    	/**
    	 * 
    	 * @return the product
    	 */
    	public ProductEntity getProduct() {
    		return product;
    	}
    
    
    	/**
    	 * @param product the product to set
    	 */
    	public void setProduct(ProductEntity product) {
    		this.product = product;
    	}
    
    
    	@Override
    	public String toString() {
    		return "Manufacture [manName=" + manName + ", product=" + product + "]";
    	}
    	
    

     在配置文件的属性部分,增加对ProductEntity的引用,如下:

    <bean id="productBean" class="com.pfSoft.beans.ProductEntity">
            <property name="prodNo" value="牙膏"></property>
            <property name="prodName" value="筷子"></property>
    </bean>

    <bean id="ManufactureBean" class="com.pfSoft.beans.Manufacture"> <property name="manName"> <value>三精制药六厂</value> </property> <property name="product" ref="productBean"></property> </bean>

    测试代码:

    @Test
    	public void testBeanRelationship() {
    		Manufacture bean = (Manufacture) applicationContext.getBean("ManufactureBean");
    		System.out.println(bean);
    		
    	}
    

     最后输出:Manufacture [manName=三精制药六厂, product=ProductEntity [prodNo=牙膏, prodName=筷子]]

     可见在Manufacture中包含了ProductEntity。

     同样的支持属性和元素的配置方式。如下:

    	<bean id="ManufactureBean" class="com.pfSoft.beans.Manufacture">
    		<property name="manName">
    			<value>三精制药六厂</value>
    		</property>
    		<property name="product">
    			<ref bean="productBean"/>
    		</property>
    	</bean>
    

    内部bean

    内部bean类似于引用其他的bean,但是该bean是在属性内部的,无法被外部调用。

    配置如下:

    <bean id="ManufactureBean" class="com.pfSoft.beans.Manufacture">
            <property name="manName">
                <value>三精制药六厂</value>
            </property>
            <property name="product">
                <ref bean="productBean"/>
            </property>
            <!-- 内部bean -->
            <property name="myOrder">
                <bean class="com.pfSoft.beans.Order">
                    <constructor-arg value="1001" index="0" ></constructor-arg>
                    <constructor-arg index="1"><value>44.444</value></constructor-arg>   
                    <constructor-arg index="2"><value>刘德华</value></constructor-arg>  
                </bean>
            </property>
        </bean>
    

     测试输出如下:

    Manufacture [manName=三精制药六厂, product=ProductEntity [prodNo=牙膏, prodName=筷子], myOrder=Order [orderNo=1001, price=44.444, customerName=刘德华]]

    级联属性

    就是在引用其他bean的时候通过属性给该bean的属性赋值

    spring配置如下:

    <bean id="ManufactureBean" class="com.pfSoft.beans.Manufacture">
    		<property name="manName">
    			<value>三精制药六厂</value>
    		</property>
    		<property name="product">
    			<ref bean="productBean"/>
    		</property>
    		<property name="product.prodNo"><value>级联属性赋值prodNo</value></property>
    		<property name="product.prodName"><value>级联属性赋值prodName</value></property>
    </bean>
    

     其中ManufactureBean包含productBean的引用,通过<property name="product.prodNo">和<property name="product.prodName">分别对productBean的prodNo和prodName赋值。

    测试代码如下:

    	@Test
    	public void testBeanRelationship() {
    		Manufacture bean = (Manufacture) applicationContext.getBean("ManufactureBean");
    		System.out.println(bean);
    		
    	}
    

     输出为:Manufacture [manName=三精制药六厂, product=ProductEntity [prodNo=级联属性赋值prodNo, prodName=级联属性赋值prodName], myOrder=null]

    集合属性

    使用List节点配置集合属性。可以通过内置的xml标签,如list、set、map等来配置集合属性。map集合配置有所不同,list配置如下:

    <!-- 测试集合属性 -->
    	 <bean id="company1" class="com.pfSoft.beans.Company">
    		<property name="companyName" value="哈药一场"></property>
    		<property name="address" value="哈尔滨"></property>
    	</bean>
    	<bean id="company2" class="com.pfSoft.beans.Company">
    		<property name="companyName" value="哈药二场"></property>
    		<property name="address" value="天津"></property>
    	</bean>
    	 
    	 <bean id="ManufactureBean" class="com.pfSoft.beans.Manufacture">
    		<property name="manName">
    			<value>三精制药六厂</value>
    		</property>
    		<property name="product">
    			<ref bean="productBean"/>
    		</property>
    		<property name="product.prodNo"><value>级联属性赋值prodNo</value></property>
    		<property name="product.prodName"><value>级联属性赋值prodName</value></property>
    		<property name="subCompanies">
    			<list>
    				<ref bean="company1"></ref>
    				<ref bean="company2"></ref>
    			</list>
    		</property>
    	</bean>
    

    测试代码:

    	@Test
    	public void testBeanRelationship() {
    		Manufacture bean = (Manufacture) applicationContext.getBean("ManufactureBean");
    		System.out.println(bean);
    		
    	}
    

     测试代码输出如下:

    Manufacture [manName=三精制药六厂, product=ProductEntity [prodNo=级联属性赋值prodNo, prodName=级联属性赋值prodName], subCompanies=[Company [companyName=哈药一场, address=哈尔滨], Company [companyName=哈药二场, address=天津]], myOrder=null]

    ps:常用的properties(jdbc,用户密码等的)也是集合属性的一种

    配置独立的集合bean,以供多个bean引用

    配置如下:

    <!-- 测试独立的集合bean, 需要引用 util命名空间 -->
     	<util:list id="companiesListBean">
     		<ref bean="company1"/>
     		<ref bean="company2"/>
     	</util:list>
    	<bean id="manfBean" class="com.pfSoft.beans.Manufacture">
    		<property name="manName" value="脑白金制药厂"></property>
    		<property name="subCompanies" ref="companiesListBean"></property>
    	</bean>
    

     测试代码如下:

    	@Test
    	public void testBeanList() {
    		Manufacture bean = (Manufacture) applicationContext.getBean("manfBean");
    		System.out.println(bean);
    	}
    

     输出如下:

    Manufacture [manName=脑白金制药厂, product=null, subCompanies=[Company [companyName=哈药一场, address=哈尔滨], Company [companyName=哈药二场, address=天津]], myOrder=null]

    使用p元素简化bean的配置

    配置如下:

    <!-- 测试使用p 命名空间简化bean的配置 ,需要添加p命名空间-->
    	<bean id="simplepProdBean" class="com.pfSoft.beans.ProductEntity" p:prodName="奥特曼" p:prodNo="001"></bean>
    

     测试代码如下:

    	@Test
    	public void testSimpleBeanByP() {
    		ProductEntity bean = (ProductEntity) applicationContext.getBean("simplepProdBean");
    		System.out.println(bean);
    	}
    

     输出如下:  ProductEntity [prodNo=001, prodName=奥特曼]

    使用注解的方式配置bean

  • 相关阅读:
    全文搜索(AB-2)-权重
    全文搜索(A-2)-推荐算法
    全文检索(AB-1)-相关领域
    全文搜索(A)-相关性
    ElasticSearch全文搜索引擎(A)
    mvc 的HtmlHelper
    left join 与left outer join的区别
    ms sqlserver数据库建索引
    w3c xml
    System and method for critical address space protection in a hypervisor environment
  • 原文地址:https://www.cnblogs.com/falcon-fei/p/5406312.html
Copyright © 2020-2023  润新知