zoukankan      html  css  js  c++  java
  • spring框架(3)— spring集合类的注入

    1.Car.java

    package com.eniac.beans;
    
    public class Car {
    	private String type;
    	private String factory;
    	private double price;
    	
    	public Car(){
    	}
    	
    	public Car(String type, String factory, double price) {
    		super();
    		this.type = type;
    		this.factory = factory;
    		this.price = price;
    	}
    	public String getType() {
    		return type;
    	}
    	public void setType(String type) {
    		this.type = type;
    	}
    	public String getFactory() {
    		return factory;
    	}
    	public void setFactory(String factory) {
    		this.factory = factory;
    	}
    	public double getPrice() {
    		return price;
    	}
    	public void setPrice(double price) {
    		this.price = price;
    	}
    	@Override
    	public String toString() {
    		return "Car [type=" + type + ", factory=" + factory + ", price="
    				+ price + "]";
    	}
    }
    

    2.Person.java

    package com.eniac.beans;
    
    import java.util.List;
    import java.util.Map;
    
    public class Person 
    {
    	private String name;
    	private int age;
    	private List<Car> cars;
    	private List<String> names;
    	private Map<String, Car> maps;
    	
    	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;
    	}
    	public List<String> getNames() {
    		return names;
    	}
    	public void setNames(List<String> names) {
    		this.names = names;
    	}
    	public Map<String, Car> getMaps() {
    		return maps;
    	}
    	public void setMaps(Map<String, Car> maps) {
    		this.maps = maps;
    	}
    	@Override
    	public String toString() {
    		return "Person [name=" + name + ", age=" + age + ", cars=" + cars
    				+ ", names=" + names + ", maps=" + maps + "]";
    	}
    }
    

      

    3.bean.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:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    	
    	<bean id="person1" class="com.eniac.beans.Person">
    		<property name="name" value="科比"/>
    		<property name="age" value="24"/>
    		<property name="cars">
    			<list>
    				<bean id="car1" class="com.eniac.beans.Car" p:type="BMW" p:factory="长安" p:price="4000"></bean>
    				<bean id="car2" class="com.eniac.beans.Car">
    					<property name="type" value="Benz"></property>
    					<property name="factory" value="一汽"></property>
    					<property name="price" value="300000"></property>
    				</bean>
    			</list>
    		</property>
    		
    		<property name="names">
    			<list>
    				<value>aaa</value>
    				<value>bbb</value>
    			</list>
    		</property>
    		<property name="maps">
    			<map>
    				<entry key="car1" value-ref="car1"/>
    				<entry key="car2" value-ref="car2"/>
    			</map>
    		</property>
    	</bean>
    </beans>
    

      1.使用了p命名空间;

      2.通过使用<list>子标签,将list的值注入;

      3.使用<map>子标签,将map的值注入。

  • 相关阅读:
    ubuntu server 14.04和18.04挂载vmware共享文件夹
    Ubuntu 无法进行SSH连接,开启22端口
    ubuntu切换到root用户
    VMware Workstation 15 Pro 永久激活密钥
    idea静态资源的访问问题,如HTML,css,js的加载
    idea在Tomcat服务器加载html文件出现乱码的解决方案
    html,js 打开时出现 Uncaught TypeError: Cannot read property 'addEventListener' of null at register.js:24错误的解决方法
    js判断input输入是不是含有中文,或者判断输入是不是全是中文
    PHP连接前端from数据的错误,如源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。
    PHP与Tomcat运行前的配置。
  • 原文地址:https://www.cnblogs.com/Mr24/p/6930719.html
Copyright © 2011-2022 走看看