springFrameWork 第三章:setter注入器注入

14 篇文章 0 订阅

接上上个项目进行继续改进

需要准备

 

然后写出需要的对象:Model

package com.ioc;

public class UserModel {
	private String id;
	private String name;
	private String sex;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public UserModel() {
		
	}
	
	public UserModel(String id, String name, String sex) { this.id = id;
	 this.name = name; this.sex = sex; }
	 
	
}

写出调用的服务类:service

package com.ioc;

public class UserService {
	private UserModel userModel;
	public UserService() {
		super();
	}

	public UserService(UserModel userModel) {
		super();
		this.userModel = userModel;
	}
	
	public UserModel getUserModel() {
		return userModel;
	}

	public void setUserModel(UserModel userModel) {
		this.userModel = userModel;
	}

	public void findUser() {
		System.out.println("-------------user id:" + userModel.getId());
		System.out.println("-------------user name:" + userModel.getName());
	}
	
}

 

按照正常的测试方法是通过new:

即:

package com.ioc;

import static org.junit.jupiter.api.Assertions.*;

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

class testUserService {

	@Test
	void testFindUser() {
		UserModel userModel = new UserModel();
		userModel.setId("id");
		userModel.setName("zzzz");
		UserService userService = new UserService(userModel);
		userService.findUser();

//		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContent.xml");
//		UserService userService = context.getBean("userService", UserService.class);
//		userService.findUser();
	}
}

就可以调用到所需要的功能、

 

然而这样的耦合性非常强,就是userMomel和userservice的联系很强。需要进行解耦,就可以通过xml配置文件进行解耦。

创建applicationContent.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"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
 
	 <!-- 属性setter注入  -->
	 <bean id="userModel" class="com.ioc.UserModel">
	 	<property name="id" value="terry123"/>
	 	<property name="name" value="Terry"/>
	 	<property name="sex" value="man"/>
	 </bean> 
 
	 <bean id="userService" class="com.ioc.UserService">
		<property name="userModel" ref="userModel"/>
	 </bean> 
	 

	 
</beans>

此时的测试类中就可以通过解析配置文件进行调用

package com.ioc;

import static org.junit.jupiter.api.Assertions.*;

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

class testUserService {

	@Test
	void testFindUser() {
//		UserModel userModel = new UserModel();
//		userModel.setId("id");
//		userModel.setName("zzzz");
//		UserService userService = new UserService(userModel);
//		userService.findUser();

		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContent.xml");
		UserService userService = context.getBean("userService", UserService.class);
		userService.findUser();
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逼哥很疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值