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

原创
2019/04/16 20:40
阅读数 10

构造注入

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"));
	}
}
展开阅读全文
打赏
3
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
3
分享
返回顶部
顶部