接上上个项目进行继续改进
需要准备
然后写出需要的对象: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();
}
}