Java Web实战04--Spring之属性注入和构造器注入


和前面一样,建立maven工程,然后进行以下操作过程,

1、新建一个bag类,设置四个字段,如下所示:

package com.yefeng.spring.spring2;


/**
 * @author yefengzhichen
 * 2016年7月3日
 */
public class Bag {
	private String brand;
	private String country;
	private double price;
	private int capacity;
	
	public Bag() {
		super();
	}

	public Bag(String brand, String country, double price) {
		super();
		this.brand = brand;
		this.country = country;
		this.price = price;
	}

	public Bag(String brand, String country, double price, int capacity) {
		super();
		this.brand = brand;
		this.country = country;
		this.price = price;
		this.capacity = capacity;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getCapacity() {
		return capacity;
	}

	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

	@Override
	public String toString() {
		return "Bag [brand=" + brand + ", country=" + country + ", price=" 
				+ price + ", capacity=" + capacity + "]";
	}
	
	
}

在本bag类中,建立了三个构造器,其中无参数的构造器,是属性注入必须建立的。分别设置所有参数的setter和getter方法,并重新toString方法。

2、同上一篇博客,在pom.xml中增加以下依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.yefeng.spring</groupId>
	<artifactId>spring1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring1</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.0.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.0.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.3.0.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>4.3.0.RELEASE</version>
		</dependency>

	</dependencies>
</project>

3、建立Spring的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="adi" class="com.yefeng.spring.spring2.Bag">
		<property name="brand" value="adi"></property>
		<property name="country" value="America"></property>
		<property name="price" value="250"></property>
	</bean>
	<!-- 构造器注入,可以指定index或者type -->
	<bean id="nike" class="com.yefeng.spring.spring2.Bag">
		<constructor-arg value="nike" index="0"></constructor-arg>
		<constructor-arg value="America" index="1"></constructor-arg>
		<!-- <constructor-arg value="10" index="3"></constructor-arg> -->
		<constructor-arg value="10" type="int"></constructor-arg>
		<constructor-arg value="200.0" type="double"></constructor-arg>
	</bean>

	<bean id="ata" class="com.yefeng.spring.spring2.Bag">
		<constructor-arg value="ata" type="java.lang.String"></constructor-arg>
		<constructor-arg value="China"></constructor-arg>
		<constructor-arg value="120"></constructor-arg>
		<constructor-arg value="5">
		</constructor-arg>

	</bean>

</beans>
4、测试代码,在app类中代码如下:

package com.yefeng.spring.spring2;

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


/**
 * @author yefengzhichen
 * 2016年7月3日
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Bag adi = (Bag) applicationContext.getBean("adi");
        System.out.println(adi);
        
        Bag nike = (Bag) applicationContext.getBean("nike");
        System.out.println(nike);
        
        Bag ata = (Bag) applicationContext.getBean("ata");
        System.out.println(ata);    
    }
}


结果如下:









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值