Spring IOC & DI

引入IOC之前

代码实现

package entity;

public class User {
private Integer id;
private String name;
private Integer gender;
// 省略getter&setter方法
}
package vo; public class UserVo { private Integer id; private String name; private Integer gender; private String genderName; public UserVo() { } public UserVo(User user) { this.id = user.getId(); this.name = user.getName(); this.gender = user.getGender(); } } package dao; public interface UserDao { User getEntity(Integer id); } package dao.impl; public class UserDaoImpl implements UserDao { public User getEntity(Integer id) { User user = new User(); user.setId(1); user.setName("Anne"); user.setGender(0); return user; } } package services; public interface UserService { UserVo getVo(Integer id); } package services.impl; public class UserServiceImpl implements UserService { private UserDao userDao; public UserVo getVo(Integer id) { userDao = new UserDaoImpl(); User user = userDao.getEntity(id); UserVo userVo = new UserVo(user); userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male"); return userVo; } } package controller; public class UserController { private UserService userService; public UserVo getVo(Integer id) { userService = new UserServiceImpl(); return userService.getVo(id); } } public class UserTest { public static void main(String[] args) { UserController userController = new UserController(); UserVo userVo = userController.getVo(1); System.out.println(userVo); } }

引入IOC(XML)

代码实现

<bean id="userDao" class="dao.impl.UserDaoImpl"/>

<bean id="userService" class="services.impl.UserServiceImpl"/>

<bean id="userController" class="controller.UserController"/>

public class UserTest {

    public static void main(String[] args) 

        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

        UserController userController = (UserController) context.getBean("userController");

        UserVo userVo = userController.getVo(1, context);

        System.out.println(userVo);

    }

}

package controller;

 

public class UserController {

    private UserService userService;

 

    public UserVo getVo(Integer id, ApplicationContext context) {

        userService = (UserService) context.getBean("userService");

        return userService.getVo(id, context);

    }

}

package services.impl;

 

public class UserServiceImpl implements UserService {

    private UserDao userDao;

 

    public UserVo getVo(Integer id, ApplicationContext context) {

        userDao = (UserDao) context.getBean("userDao");

        User user = userDao.getEntity(id);

        UserVo userVo = new UserVo(user);

        userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");

        return userVo;

    }

 

}

 

XML改注解(IOC)

<?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:util="http://www.springframework.org/schema/util"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="

        http://www.springframework.org/schema/beans

        https://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/util

        https://www.springframework.org/schema/util/spring-util.xsd

        http://www.springframework.org/schema/context

        https://www.springframework.org/schema/context/spring-context.xsd

       ">

 

    <!-- bean definitions here -->

    <context:component-scan base-package="dao"/>

    <context:component-scan base-package="services"/>

    <context:component-scan base-package="controller"/>

 

</beans>

引入DI

package controller;

@Controller
public class UserController {
@Autowired
private UserService userService;

public UserVo getVo(Integer id, ApplicationContext context) {
return userService.getVo(id, context);
}
}
package dao.impl;

@Repository
public class UserDaoImpl implements UserDao {
public User getEntity(Integer id) {
User user = new User();
user.setId(1);
user.setName("Anne");
user.setGender(0);
return user;
}
}
package services.impl;

@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDaoImpl")
private UserDao userDao;
public UserVo getVo(Integer id) {
User user = userDao.getEntity(id);
UserVo userVo = new UserVo(user);
userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
return userVo;
}
}

posted @ 2021-04-14 21:03  至安望月  阅读(59)  评论(0)    收藏  举报