spring-ioc-fangneng

文件目录:

 

在pom.xml中导入:

<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jcl -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jcl</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
</dependencies>
UserController文件:
//和前端页面交互
public class UserController {
private UserServices userService;

public UserVo getVo(Integer id, ApplicationContext context) {
// 从Spring容器拿Service
userService = (UserServices) context.getBean("userServices");
// 执行Service层方法,因为之后还需要用到context对象,故下传
return userService.getVo(id,context);
}
}
UserDaoImpl文件:
public class UserDaoImpl implements UserDao {
public User getEntity(Integer id) {
//读取数据库的数据
User user = new User();
user.setId(1);
user.setName("fangneng");
user.setGender(0);
return user;
}
}
UserDao文件:
//和数据库交互
public interface UserDao {
User getEntity(Integer id);
}
User文件:

//User实体类 数据库交互
public class User {
private Integer id;
private String name;
private Integer gender;
public Integer getId() {
return id;
}

public String getName() {
return name;
}

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

public void setName(String name) {
this.name = name;
}

public void setGender(Integer gender) {
this.gender = gender;
}

public Integer getGender() {
return gender;
}
}
UserServiceImpl文件:
//处理逻辑
public class UserServiceImpl implements UserServices {
@Autowired
private UserDao userDao;
public UserVo getVo(Integer id, ApplicationContext context) {
// 从Spring容器拿Dao
userDao = (UserDao) context.getBean("userDao");
// 执行Dao层方法
User user = userDao.getEntity(id);
// 省略业务逻辑处理。。。
UserVo userVo = new UserVo(user);
userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
return userVo;
}
}
UserServices文件:
public interface UserServices {
UserVo getVo(Integer id, ApplicationContext context);
}
UserVo文件:
//和前端页面交互
public class UserVo {
private Integer id;
private String name;
private Integer gender;

public UserVo(User user) {
this.id = user.getId();
this.name = user.getName();
this.gender = user.getGender();
}

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

public void setName(String name) {
this.name = name;
}

public void setGender(Integer gender) {
this.gender = gender;
}

public void setGenderName(String genderName) {
this.genderName = genderName;
}

public Integer getId() {
return id;
}

public String getName() {
return name;
}

public Integer getGender() {
return gender;
}

public String getGenderName() {
return genderName;
}

private String genderName;

public UserVo() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", genderName=" + genderName +
", gender=" + gender +
'}';
}
}
applicationContext.xml配置文件:

<bean id="userDao" class="dao.impl.UserDaoImpl"/>
<bean id="userServices" class="services.impl.UserServiceImpl"/>
<bean id="userController" class="controller.UserController"/>
UserTest文件:

public class UserTest {
public static void main(String[] args) {
// 读取配置文件刷新Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// 从Spring容器拿Controller
UserController userController = (UserController) context.getBean("userController");
// 执行Controller层方法,因为之后还需要用到context对象,故下传
UserVo userVo = userController.getVo(1, context);
System.out.println(userVo);
}
}

测试结果:

spring-ioc-fangneng

 

 

 



上一篇:认识XML


下一篇:Python 文本转字典/字典转文本/文本字典转字典