• 中文
    • English
  • 注册
  • 查看作者
  • Spring:实体类和集合配置

    一.  实体类

    《Spring:使用构造方法创建对象》一文中,Book类中的属性都是简单类型,所以可以直接使用value赋值,但是如果一个类对象中的属性包含其他类,则不能使用value进行赋值。首先添加Person类:

    package io.zhangjia.entity;
    
    public class Person {
        private String name;
        private Integer age;
        private Book book;
    
        public Person() {
        }
    
        public Person(String name, Integer age, Book book) {
            this.name = name;
            this.age = age;
            this.book = book;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public Book getBook() {
            return book;
        }
    
        public void setBook(Book book) {
            this.book = book;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", book=" + book +
                    '}';
        }
    }

    此时的Person中的name和age属性还是简单类型,但是Book已经却是一个单独的Java类,所以此时的bean中不能再使用value为其赋值,需要使用ref属性通过引用其他的bean的方式为其赋值,ref的值就是  <bean id=”book” 的id的值

    <?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="book" class="io.zhangjia.entity.Book">
            <property name="bookId" value="2"/>
            <property name="name" value="Spring开发"/>
            <property name="price" value="19.9"/>
            <property name="author" value="张三"/>
        </bean>
    
        <bean id="person" class="io.zhangjia.entity.Person">
    <!--     简单类型可以使用value完成属性的赋值,且property是单标签-->
            <property name="name" value="张三" />
            <property name="age" value="20" />
    <!--        使用ref引用其他的bean-->
            <property name="book" ref="book" />
        </bean>
    </beans>

    测试类:

    public class Test {
        public static void main(String[] args) throws Exception {
    //        初始化Spring的IOC容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    //        根据id获取bean
            Object person = context.getBean("person");
            System.out.println("person = " + person);
    
        }
    }
    输出:
    person = Person{name='张三', age=20, book=Book{bookId=2, name='Spring开发', author='张三', price=19.9}}

    除了上述方式外,我们可以通过内部bean的方式为其赋值:

    <?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="person2" class="io.zhangjia.entity.Person">
            <property name="name" value="王五"/>
            <property name="age" value="21"/>
            <!-- 此时的property是双标签-->
            <property name="book">
                 <!-- 内部bean-->
                <bean id="book" class="io.zhangjia.entity.Book">
                    <property name="bookId" value="2"/>
                    <property name="name" value="Spring开发"/>
                    <property name="price" value="19.9"/>
                    <property name="author" value="张三"/>
                </bean>
            </property>
        </bean>
    </beans>

    测试类输出:

    person2 = Person{name='王五', age=21, book=Book{bookId=2, name='Spring开发', author='张三', price=19.9}}

    值得注意的是,内部bean不能再被其他bean引用

    二.  List

    我们将Person类修改为:

    public class Person {
        private String name;
        private Integer age;
        private List<Book> books;
    
       .....
    }

    和上面的Book实体类一样,Person类中类型为 List<Book> 的books也不能直接使用value赋值,我们需要借助ref标签来为其赋值

    <?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="book" class="io.zhangjia.entity.Book">
            <property name="bookId" value="2"/>
            <property name="name" value="Spring开发"/>
            <property name="price" value="19.9"/>
            <property name="author" value="张三"/>
        </bean>
    
    
        <bean id="book2" class="io.zhangjia.entity.Book">
            <constructor-arg name="bookId" value="1"/>
            <constructor-arg name="name" value="Spring实战"/>
            <constructor-arg name="price" value="29.9"/>
            <constructor-arg name="author" value="王五"/>
        </bean>
       
        <bean id="person" class="io.zhangjia.entity.Person">
            <property name="name" value="张三"/>
            <property name="age" value="20"/>
            <property name="books">
                <list>
                    <ref bean="book"/>
                    <ref bean="book2"/>
                </list>
            </property>
    
        </bean>
    </beans>

    测试类输出:

    person = Person{name='张三', age=20, books=[Book{bookId=2, name='Spring开发', author='张三', price=19.9}, Book{bookId=1, name='Spring实战', author='王五', price=29.9}]}

    除了上述方式,我们同样可以采用内部bean的方式为其赋值:

    <?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="person2" class="io.zhangjia.entity.Person">
            <property name="name" value="张三"/>
            <property name="age" value="20"/>
            <property name="books">
                <list>
                    <bean  class="io.zhangjia.entity.Book">
                        <property name="bookId" value="2"/>
                        <property name="name" value="Spring开发"/>
                        <property name="price" value="19.9"/>
                        <property name="author" value="张三"/>
                    </bean>
                    <bean  class="io.zhangjia.entity.Book">
                        <constructor-arg name="bookId" value="1"/>
                        <constructor-arg name="name" value="Spring实战"/>
                        <constructor-arg name="price" value="29.9"/>
                        <constructor-arg name="author" value="王五"/>
                    </bean>
                </list>
            </property>
    
        </bean>
    </beans>

    测试类输出:

    person2 = Person{name='张三', age=20, books=[Book{bookId=2, name='Spring开发', author='张三', price=19.9}, Book{bookId=1, name='Spring实战', author='王五', price=29.9}]}

    三.  Set

    Set和上面的List几乎完全一样,这里只给出相关代码,不再赘述:

    Person类:

    public class Person {
        private String name;
        private Integer age;
        private Set<Book> books;
        ....
    }

    方法一:

    <?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="book" class="io.zhangjia.entity.Book">
            <property name="bookId" value="2"/>
            <property name="name" value="Spring开发"/>
            <property name="price" value="19.9"/>
            <property name="author" value="张三"/>
        </bean>
    
    
        <bean id="book2" class="io.zhangjia.entity.Book">
            <constructor-arg name="bookId" value="1"/>
            <constructor-arg name="name" value="Spring实战"/>
            <constructor-arg name="price" value="29.9"/>
            <constructor-arg name="author" value="王五"/>
        </bean>
        
    
        <bean id="person" class="io.zhangjia.entity.Person">
            <property name="name" value="张三"/>
            <property name="age" value="20"/>
            <property name="books">
                <set>
                    <ref bean="book"/>
                    <ref bean="book2"/>
                </set>
            </property>
        </bean>
    
    
    </beans>

    测试类输出:

    person = Person{name='张三', age=20, books=[Book{bookId=2, name='Spring开发', author='张三', price=19.9}, Book{bookId=1, name='Spring实战', author='王五', price=29.9}]}

    方法二:

    <?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="person2" class="io.zhangjia.entity.Person">
            <property name="name" value="张三"/>
            <property name="age" value="20"/>
            <property name="books">
                <set>
                    <bean class="io.zhangjia.entity.Book">
                        <property name="bookId" value="2"/>
                        <property name="name" value="Spring开发"/>
                        <property name="price" value="19.9"/>
                        <property name="author" value="张三"/>
                    </bean>
                    <bean class="io.zhangjia.entity.Book">
                        <constructor-arg name="bookId" value="1"/>
                        <constructor-arg name="name" value="Spring实战"/>
                        <constructor-arg name="price" value="29.9"/>
                        <constructor-arg name="author" value="王五"/>
                    </bean>
                </set>
            </property>
        </bean>
    
    </beans>

    测试类输出:

    person2 = Person{name='张三', age=20, books=[Book{bookId=2, name='Spring开发', author='张三', price=19.9}, Book{bookId=1, name='Spring实战', author='王五', price=29.9}]}

    三.  Map

    Map和List以及Set稍有不同,map标签中不再使用ref标签,而使用entry标签,其中key就是Map的key,value就是map的value

    实体类:

    public class Person {
        private String name;
        private Integer age;
        private Map<String,Object> books;
        ....
    }

    applicationContext.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="person" class="io.zhangjia.entity.Person">
            <property name="name" value="张三"/>
            <property name="age" value="20"/>
            <property name="books">
                <map>
                    <entry key="bookId" value="3" />
                    <entry key="name" value="css开发" />
                    <entry key="price" value="10" />
                    <entry key="author" value="李四" />
                </map>
            </property>
    
        </bean>
    </beans>

    测试类输出:

    person = Person{name='张三', age=20, books={bookId=3, name=css开发, price=10, author=李四}}

    四.  Properties

    实体类:

    public class Person {
        private String name;
        private Integer age;
        private Properties jdbcProperties;
        ....
    }

    Properties类继承了Hashtable,所以也可以将其看做一个map,其中prop标签中的key便是map的key,而prop标签的内容,便是map的value。applicationContext.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="person" class="io.zhangjia.entity.Person">
        <property name="name" value="张三"/>
        <property name="age" value="20"/>
        <property name="jdbcProperties">
            <props>
                <prop key="jdbc.driver">com.mysql.jdbc.Driver</prop>
                <prop key="jdbc.ur1">jdbc:mysql://localhost:3306/demo</prop>
                <prop key="jdbc.username">root</prop>
                <prop key="jdbc.password">zhangjia</prop>
            </props>
        </property>
        </bean>
    
    </beans>

    测试类输出:

    person = Person{name='张三', age=20, jdbcProperties={jdbc.username=root, jdbc.ur1=jdbc:mysql://localhost:3306/demo, jdbc.driver=com.mysql.jdbc.Driver, jdbc.password=zhangjia}}

  • 0
  • 0
  • 0
  • 1.7k
  • 请登录之后再进行评论

    登录
    单栏布局 侧栏位置: