Spring-IOC【02-xml配置方式】

构造注入

package com.sxt.pojo;

public class User {

	private int id;
	private String name;
	private int age;
	
	public User() {
	}
	/**
	 * 构造注入
	 * @param id
	 * @param name
	 * @param age
	 */
	public User(int id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}	
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

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">
 	<!-- 注册User对象 -->
 	<bean class="com.sxt.pojo.User">
 		<!-- 构造注入 根据index -->
 		<!-- <constructor-arg index="0" value="1"/>
 		<constructor-arg index="1" value="dpb"/>
 		<constructor-arg index="2" value="20"/> -->
 		<constructor-arg name="id" value="1"/>
 		<constructor-arg name="name" value="zed"/>
 		<constructor-arg name="age" value="18"/>
 	</bean>
 </beans>
package com.sxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sxt.pojo.User;

public class Demo {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		User bean = ac.getBean(User.class);
		System.out.println(bean);
	}
}

设值注入

package com.sxt.pojo;
/**
 * 通过设值注入的方式实现属性的设置
 * @author Administrator
 *
 */
public class User {

	private int id;
	private String name;
	private int age;
	
	public int getId() {
		return id;
	}
	/**
	 * 设值注入
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	/**
	 * 设值注入
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	/**
	 * 设值注入
	 * @param age
	 */
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

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">
 	<!-- 注册User对象 -->
 	<bean class="com.sxt.pojo.User">
 		<!-- 设值注入 -->
 		<property name="id" value="1"/>
 		<property name="name" value="jax"/>
 		<property name="age" value="19"/>
 	</bean>
 </beans>
package com.sxt.test;

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

import com.sxt.pojo.User;

public class Demo {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		User bean = ac.getBean(User.class);
		System.out.println(bean);
	}
}

简化版的设值注入

引入P标签库
在这里插入图片描述

package com.sxt.pojo;
/**
 * 通过设值注入的方式实现属性的设置
 * @author Administrator
 *
 */
public class User {

	private int id;
	private String name;
	private int age;
	
	public int getId() {
		return id;
	}
	/**
	 * 设值注入
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	/**
	 * 设值注入
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	/**
	 * 设值注入
	 * @param age
	 */
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 	<!-- 注册User对象 通过p名称空间注入 简化版的设值注入-->
 	<bean class="com.sxt.pojo.User" p:id="3" p:name="jinx" p:age="16"/>
 </beans>
package com.sxt.test;

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

import com.sxt.pojo.User;

public class Demo {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		User bean = ac.getBean(User.class);
		System.out.println(bean);
	}
}

注入自定义对象

package com.sxt.pojo;

public class Cat {

	private String nick;
	private String color;
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	@Override
	public String toString() {
		return "Cat [nick=" + nick + ", color=" + color + "]";
	}
}
package com.sxt.pojo;
/**
 * 通过设值注入的方式实现属性的设置
 * @author Administrator
 *
 */
public class User {

	private int id;
	private String name;
	private int age;
	private Cat cat;
	
	
	public User(Cat cat) {
		super();
		this.cat = cat;
	}
	public Cat getCat() {
		return cat;
	}
	/**
	 * 设值注入
	 * @param cat
	 */
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	public int getId() {
		return id;
	}
	/**
	 * 设值注入
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	/**
	 * 设值注入
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	/**
	 * 设值注入
	 * @param age
	 */
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 	<!-- 注册Cat对象 -->
 	<bean class="com.sxt.pojo.Cat" id="catId">
 		<!-- 设值注入 -->
 		<property name="nick" value="花花"/>
 		<property name="color" value="白色"/>
 	</bean>
 	<!-- 注册User对象 同时定义对自定义对象赋值 -->
 	<bean class="com.sxt.pojo.User">
 		<!-- 通过构造注入的方式设置属性 -->
 		<constructor-arg name="cat" ref="catId"/>
 		<!-- <constructor-arg name="cat">
 			<bean class="com.sxt.pojo.Cat">
 				设值注入
 				<property name="nick" value="花花1"/>
 				<property name="color" value="黄色"/>
 			</bean>
 		</constructor-arg> -->
 	</bean>
 </beans>

------------------------------------------------------------------------------

package com.sxt.pojo;

public class Cat {

	private String nick;
	private String color;
	
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	@Override
	public String toString() {
		return "Cat [nick=" + nick + ", color=" + color + "]";
	}
}

package com.sxt.pojo;
/**
 * 通过设值注入的方式实现属性的设置
 * @author Administrator
 *
 */
public class User {

	private int id;
	private String name;
	private int age;
	private Cat cat;
	
	public Cat getCat() {
		return cat;
	}
	/**
	 * 设值注入
	 * @param cat
	 */
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	public int getId() {
		return id;
	}
	/**
	 * 设值注入
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	/**
	 * 设值注入
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	/**
	 * 设值注入
	 * @param age
	 */
	public void setAge(int age) {
		this.age = age;
	}	
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 	<!-- 注册Cat对象 -->
 	<bean class="com.sxt.pojo.Cat" id="catId">
 		<!-- 设值注入 -->
 		<property name="nick" value="花花"/>
 		<property name="color" value="白色"/>
 	</bean>
 	<!-- 注册User对象 同时定义对自定义对象赋值 -->
 	<bean class="com.sxt.pojo.User">
 		<!-- 通过设值注入的方式设置属性 -->
 		<property name="cat" ref="catId"/>
 	</bean>
 </beans>
package com.sxt.test;

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

import com.sxt.pojo.User;

public class Demo {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		User bean = ac.getBean(User.class);
		System.out.println(bean.getCat());
	}
}

操作其他对象

package com.sxt.pojo;

public class Cat {

	private String nick;
	private String color;
	
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	@Override
	public String toString() {
		return "Cat [nick=" + nick + ", color=" + color + "]";
	}	
}
package com.sxt.pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 通过设值注入的方式实现属性的设置
 * @author Administrator
 *
 */
public class User {

	private int id;
	private String name;
	private int age;
	private Cat[] cats;
	private List<String> list;
	private Map<String, String> map;
	private Properties props;
	
	public int getId() {
		return id;
	}
	/**
	 * 设值注入
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	/**
	 * 设值注入
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	/**
	 * 设值注入
	 * @param age
	 */
	public void setAge(int age) {
		this.age = age;
	}
	public Cat[] getCats() {
		return cats;
	}
	public void setCats(Cat[] cats) {
		this.cats = cats;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public Properties getProps() {
		return props;
	}
	public void setProps(Properties props) {
		this.props = props;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + ", cats=" + Arrays.toString(cats) + ", list="
				+ list + ", map=" + map + ", props=" + props + "]";
	}	
}
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 	<!-- 注册User对象 同时定义对自定义对象赋值 -->
 	<bean class="com.sxt.pojo.User">
 		<!-- 通过设值注入相关的属性 -->
 		<property name="id" value="1"/>
 		<property name="name" value="dpb"/>
 		<property name="age" value="18"/>
 		
 		<!-- 数组类型注入 -->
 		<property name="cats">
 			<array>
 				<bean class="com.sxt.pojo.Cat">
 					<!-- 设值注入 -->
 					<property name="nick" value="花花"/>
 					<property name="color" value="红色"/>
 				</bean>
 				<bean class="com.sxt.pojo.Cat">
 					<!-- 设值注入 -->
 					<property name="nick" value="花花"/>
 					<property name="color" value="白色"/>
 				</bean>
 				<bean class="com.sxt.pojo.Cat">
 					<!-- 设值注入 -->
 					<property name="nick" value="花花"/>
 					<property name="color" value="黄色"/>
 				</bean>
 			</array>
 		</property>
 		
 		<!-- List类型 -->
 		<property name="list">
 			<list>
 				<value>a</value>
 				<value>b</value>
 				<value>c</value>
 			</list>
 		</property>
 		
 		<!-- Map类型 -->
 		<property name="map">
 			<map>
 				<entry key="stuId" value="1001"/>
 				<entry key="stuName" value="尼古拉斯"/>
 			</map>
 		</property>
 		
 		<!-- properties类型 -->
 		<property name="props">
 			<props>
 				<prop key="url">192.168.0.116:8080</prop>
 				<prop key="username">cleanlove</prop>
 				<prop key="password">7777777</prop>
 			</props>
 		</property>
 	</bean>
 </beans>
package com.sxt.test;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

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

import com.sxt.pojo.Cat;
import com.sxt.pojo.User;

public class Demo {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		User bean = ac.getBean(User.class);
		System.out.println(bean.getList());
		Cat[] cats = bean.getCats();
		for (Cat cat : cats) {
			System.out.println(cat);
		}
		Map<String, String> maps = bean.getMap();
		Set<String> keySet = maps.keySet();
		for (String key : keySet) {
			System.out.println(key +":"+maps.get(key));
		}
		Properties props = bean.getProps();
		System.out.println(props.getProperty("url"));
		System.out.println(props.getProperty("username"));
		System.out.println(props.getProperty("password"));
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值