Spring-IOC实现[02-XML配置文件方式]

XML配置方式

属性注入

构造注入: 创建有参构造方法,同时必须创建无参构造方法

在这里插入图片描述

applicatio.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	
		 <bean class="com.sxt.bean.User">
		<!--构造方法通过属性注入-->
			<constructor-arg name="id" value="21"/>
			<constructor-arg name="name" value="lisi"/>
			<constructor-arg name="age" value="21"/>
		</bean> 
</beans>

还可以通过下标定位参数

<?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">
		<<bean class="com.sxt.bean.User"> 
				<constructor-arg index="0" value="12"/>
				<constructor-arg index="1" value="we"/>
				<constructor-arg index="2" value="12"/>
		</bean> 
</beans>

测试结果

在这里插入图片描述

设置注入

Java对象提供对象的setter方法

有参构造不是必须的,但是无参构造方法是必须的!
set方法注入就是利用对象属性的set方法给对像赋值,相当于使用无参构造方法创建一个user对象,然后调用对象中的set方法给各个属性赋值

package com.sxt.bean;

public class User {
	private Integer id;
	
	private String name;
	
	private Integer age;

	public Integer getId() {
		return id;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	

	public User() {
		super();
	}

	public void setId(Integer id) {
		this.id = id;
	}

	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;
	}
}

application.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
		<bean class="com.sxt.bean.User">
			<!--设值注入-->
			<property name="id" value="22"/>
			<property name="name" value="xn"/>
			<property name="age" value="12"/>
		</bean> 
</beans>

测试

在这里插入图片描述

P名称空间注入

p名称空间注入本质上还是set方法注入,只是写法不同

package com.sxt.bean;

public class User {
	private Integer id;
	
	private String name;
	
	private Integer age;

	public Integer getId() {
		return id;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	

	public User() {
		super();
	}

	public void setId(Integer id) {
		this.id = id;
	}

	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;
	}
}

application.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
		<!-- p名称注入 -->
		<bean class="com.sxt.bean.User" p:id="11" p:name="nx" p:age="122"/>	
</beans>

测试

在这里插入图片描述

对像,数组,list集合,map和Properties属性注入的XML配置方式

User类和Cat类

package com.sxt.bean;

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

public class User {
	private Integer id;
	
	private String name;
	
	private Integer age;

	private Cat cat;//对象注入
	
	private Cat[] cats;//数组注入
	
	private List<Cat> stu;//集合注入
	
	private Map<String, String> maps;//map注入
	
	private Properties prope;//Properties 属性注入

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + ", cat=" + cat + ", cats=" + Arrays.toString(cats)
				+ ", stu=" + stu + ", maps=" + maps + ", prope=" + prope + "]";
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	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 Cat getCat() {
		return cat;
	}

	public void setCat(Cat cat) {
		this.cat = cat;
	}

	public Cat[] getCats() {
		return cats;
	}

	public void setCats(Cat[] cats) {
		this.cats = cats;
	}

	public List<Cat> getStu() {
		return stu;
	}

	public void setStu(List<Cat> stu) {
		this.stu = stu;
	}

	public Map<String, String> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, String> maps) {
		this.maps = maps;
	}

	public Properties getPrope() {
		return prope;
	}

	public void setPrope(Properties prope) {
		this.prope = prope;
	}

	public User() {
		super();
	}
	
}

package com.sxt.bean;

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;
	}

}

application.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: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.bean.User">
		<property name="id" value="10" />
		<property name="name" value="lisi" />
		<property name="age" value="20" />
		<!-- cat对象注入 -->
		<property name="cat">
			<bean class="com.sxt.bean.Cat">
				<property name="nick" value="花花" />
				<property name="color" value="白色" />
			</bean>
		</property>
		<!-- 数组注入 -->
		<property name="cats">
			<array>
				<bean class="com.sxt.bean.Cat">
					<property name="nick" value="花花1" />
					<property name="color" value="白色1" />
				</bean>
				<bean class="com.sxt.bean.Cat">
					<property name="nick" value="花花2" />
					<property name="color" value="白色2" />
				</bean>
				<bean class="com.sxt.bean.Cat">
					<property name="nick" value="花花3" />
					<property name="color" value="白色3" />
				</bean>
			</array>
		</property>
		<!-- list集合注入 -->
		<property name="stu">
			<list>
				<bean class="com.sxt.bean.Cat">
					<property name="nick" value="猫猫" />
					<property name="color" value="红色1" />
				</bean>
				<bean class="com.sxt.bean.Cat">
					<property name="nick" value="猫猫" />
					<property name="color" value="红色1" />
				</bean>
				<bean class="com.sxt.bean.Cat">
					<property name="nick" value="猫猫" />
					<property name="color" value="红色1" />
				</bean>
			</list>
		</property>
		<!-- map集合注入 -->
		<property name="maps">
			<map>
				<entry key="address" value="深圳" />
				<entry key="job" value="程序员" />
			</map>
		</property>
		<!-- Properties 属性注入 -->
		<property name="prope">
			<props>
				<prop key="username">admin</prop>
				<prop key="password">123</prop>
			</props>
		</property>
	</bean>
</beans>

测试

package com.sxt.test;



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

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

import com.sxt.bean.Cat;
import com.sxt.bean.User;

public class UserTest {

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
		User bean = (User) ac.getBean(User.class);
		System.out.println(bean);
		System.out.println("打印数组");
		Cat[] cats = bean.getCats();
		for (Cat cat : cats) {
			System.out.println(cat.getNick()+"+"+cat.getColor());
		}
		System.out.println("打印list集合");
		List<Cat> stu = bean.getStu();
		for (Cat cat : stu) {
			System.out.println(cat.getNick()+"+"+cat.getColor());
		}
		System.out.println("打印map集合");
		Map<String, String> maps = bean.getMaps();
		Set<String> set = maps.keySet();
		for (String sys : set) {
			System.out.println(maps.get(sys)+"+"+sys);
		}
		System.out.println("打印Properties 属性");
		Properties prope = bean.getPrope();
		System.out.println(prope.getProperty("username")+"+"+prope.getProperty("password"));
	}

}

在这里插入图片描述

创建综合案例

引入相关jar包

在这里插入图片描述

1.创建项目dao层,service层,和他们实现类

在这里插入图片描述

package com.sxt.dao;
/**
 * dao层代码
 * @author Administrator
 *
 */
public interface IUserDao {
	String add(String msg);
}

package com.sxt.dao.impl;

import com.sxt.dao.IUserDao;
/**
 * dao层实现类代码
 * @author Administrator
 *
 */
public class UserDaoImpl implements IUserDao{
	
	@Override
	public String add(String msg) {
		System.out.println("----->"+msg);
		return "hello";
	}

}

package com.sxt.service;
/**
 * service层代码
 * @author Administrator
 *
 */
public interface IUserService {
	String add(String msg);
}

package com.sxt.service.impl;

import com.sxt.dao.IUserDao;
import com.sxt.service.IUserService;
/**
 * service层实现类代码
 * @author Administrator
 *
 */
public class UserServiceImpl implements IUserService{
	private IUserDao dao;
	
	/**
	 * 这是设值注入的方法
	 * @param dao
	 */
	public void setDao(IUserDao dao) {
		this.dao = dao;
	}
	@Override
	public String add(String msg) {
		return dao.add(msg);
	}

}

2.创建Controller层

package com.sxt.controller;

import com.sxt.service.IUserService;
/**
 * controller层代码
 * @author Administrator
 *
 */
public class UserConrtroller {
	
	private IUserService userService;
	/**
	 * 
	 * 这是设值注入的方法 
	 * @param userService
	 */
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void add(String msg) {
		System.out.println("-------////"+userService.add(msg));
	}

}

3.在application.xml中注册

<!-- 注册dao层实现类 -->
	<bean class="com.sxt.dao.impl.UserDaoImpl" id="userDaoImpl"></bean>
	<!-- 注册service层实现类 -->
	<bean class="com.sxt.service.impl.UserServiceImpl" id="userServiceImpl">
			<property  name="dao" ref="userDaoImpl"></property>
	</bean>
	<!-- 注册controller层实现类 -->
	<bean class="com.sxt.controller.UserConrtroller" id="userConrtroller">
		<property name="userService" ref="userServiceImpl"></property>
	</bean>

4.测试

public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
		UserConrtroller bean = ac.getBean(UserConrtroller.class);
		bean.add("aaa");
	}
}

测试结果

猜你喜欢

转载自blog.csdn.net/qq_30984561/article/details/89329045
0条评论
添加一条新回复