<bean 多个value怎么配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了<bean 多个value怎么配置相关的知识,希望对你有一定的参考价值。

参考技术A 配置文件本身和其子元素import. 可用于导入其他配置文件。具体的配置示例如下:
<?xml version="1.0" encod工口g="gb2312"?>
<!一指定Spring 配置文件的dtd>
<!DOCTYPE beans PUBLIC "-IISPR工NGIIDTD BEANIIEN"
''htt p:/ /ww w.springframe work.o rg/dtd/spring-beans.dtd''>
<!-- Spring 配置文件的根元素->
<beans>
<!一导入第→份配置文件: serv工ces.xml一〉
<import resource="serv工ces.xml"l>
<!-- 导入第二份配置文件: resources/messageSource.xml 一〉
<import resource="resources/messageSource.xml"l>
<!-- 导入第二份配置文件: resourcesl themeSource.xml -->
<import resource="/resources/themeSource.xml"l>
<!-- 下面定义该文件垦的其他bean…〉
<bean id="beanl" class=". .."1>
<bean id="bea口2" class="..."I>
</beans>本回答被提问者采纳

Spring框架学习笔记——配置bean more

1、配置List属性

<!-- 配置List属性 -->
<bean id="person4" class="com.broadtext.beans.collection.Person">
    <property name="name" value="hjj"></property>
    <property name="age" value="24"></property>
    <property name="cars">
        <!-- 使用list节点为List类型属性值赋值 -->
        <list>
            <ref bean="car"/>
            <ref bean="car2"/>
            <bean class="com.broadtext.beans.Car">
             <constructor-arg value="Ford"></constructor-arg>
             <constructor-arg value="Shanghai"></constructor-arg>
             <constructor-arg type="double" value="300000"></constructor-arg>
         </bean>
        </list>
    </property>
</bean>

2、配置Map属性

<!-- 配置Map属性 -->
<bean id="newPerson" class="com.broadtext.beans.collection.NewPerson">
    <property name="name" value="hjjj"></property>
    <property name="age" value="28"></property>
    <property name="cars">
        <map>
            <entry key="A_Car" value-ref="car"></entry>
            <entry key="B_Car" value-ref="car2"></entry>
        </map>
    </property>
</bean>

3、可以使用util命名空间单独配置一个集合bean以供其他bean应用

<!-- 单独配置一个集合bean以供其他bean应用 -->
<util:list id="cars">
    <ref bean="car" />
</util:list>
<!-- 配置List属性 -->
<bean id="person4" class="com.broadtext.beans.collection.Person">
    <property name="name" value="hjj"></property>
    <property name="age" value="24"></property>
    <!-- <property name="cars">
        使用list节点为List类型属性值赋值
        <list>
            <ref bean="car"/>
            <ref bean="car2"/>
            <bean class="com.broadtext.beans.Car">
             <constructor-arg value="Ford"></constructor-arg>
             <constructor-arg value="Shanghai"></constructor-arg>
             <constructor-arg type="double" value="300000"></constructor-arg>
         </bean>
        </list>
    </property> -->
    <property name="cars" ref="cars"></property>
</bean>

4、模拟配置properties

<!-- 模拟配置properties -->
<bean id="dataSource" class="com.broadtext.beans.collection.DataSource">
    <property name="properties">
        <props>
            <prop key="user">root</prop>
            <prop key="password">12345</prop>
            <prop key="jdbcUrl">jdbc:mysql://test</prop>
            <prop key="driverClass">com.jdbc.mysql</prop>
        </props>
    </property>
</bean>

5、Person.java、NewPerson.java、DataSource.java、Main.java

Person.java

package com.broadtext.beans.collection;

import java.util.List;

import com.broadtext.beans.Car;

public class Person {
    private String name;
    private int age;
    private List<Car> cars;

    public Person(String name, int age, List<Car> cars) {
        super();
        this.name = name;
        this.age = age;
        this.cars = cars;
    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;
    }

}

NewPerson.java

package com.broadtext.beans.collection;

import java.util.Map;

import com.broadtext.beans.Car;

public class NewPerson {
    private String name;
    private int age;
    private Map<String, Car> cars;

    public NewPerson(String name, int age, Map<String, Car> cars) {
        super();
        this.name = name;
        this.age = age;
        this.cars = cars;
    }

    public NewPerson() {
    }

    @Override
    public String toString() {
        return "NewPerson [name=" + name + ", age=" + age + ", cars=" + cars + "]";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Map<String, Car> getCars() {
        return cars;
    }

    public void setCars(Map<String, Car> cars) {
        this.cars = cars;
    }

}

DataSource.java

package com.broadtext.beans.collection;

import java.util.Properties;

public class DataSource {
    private Properties properties;

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "DataSource [properties=" + properties + "]";
    }
}

Main.java

package com.broadtext.beans.collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * <p>
 * Description: 测试bean的List属性、Map属性的配置,模拟配置properties
 * </p>
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) ctx.getBean("person4");
        System.out.println(person);
        NewPerson newPerson = (NewPerson) ctx.getBean("newPerson");
        System.out.println(newPerson);
        DataSource dataSource = ctx.getBean(DataSource.class);
        System.out.println(dataSource.getProperties());
    }
}

 

以上是关于<bean 多个value怎么配置的主要内容,如果未能解决你的问题,请参考以下文章

spring怎么管理有多个方法的action,求指教

spring 配置多个properties

Spring框架学习笔记——配置bean more

Spring 简化装配Bean的配置方式

Spring之XML配置Bean的属性注入

spring mvc +ibatis 怎样配置自动加载多个sqlmap.xml文件