Skip to content

SpringBoot Hot Reload beta

  • Since there is no update recognition for XML files at present, the newly added Beans in the XML configuration of the Spring project cannot be recognized, but hot reload can be performed on the already registered Beans.

Bean Hot Reload

The core generates BeanDefinition by parsing class file information, and calls ClassPathBeanDefinitionScanner.registerBeanDefinition() to register it in the Spring container. In theory, all Bean changes managed by Spring can be hot-reloaded, and of course, new Beans are also supported.

Controller

Additions or modifications to Controller layer classes can be hot-reloaded and Mapping annotations (such as @RequestMapping, @GetMapping, @PostMapping, etc.) information will be reparsed.

java
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserComponent userComponent; 
    
    private final OrderService orderService; 
    
    public UserController(UserComponent userComponent) { 
        this.userComponent = userComponent; 
    } 

    @GetMapping("/addUser") 
    public String addUser() { 
        return "addUser"; 
    @GetMapping("/saveUser") 
    public String saveUser() { 
        return "saveUser"; 
    }

    @GetMapping("/order") 
    public String order(@RequestParam("userId") Long userId) { 
        User user = userComponent.selectUser(userId); 
        if (user == null) { 
            return "user is null";  
        } 
        return orderService.order(user); 
    } 
}
  • The previous /user/addUser is no longer accessible, but the newly added /user/saveUser can be accessed.
  • The newly added /user/order can be accessed, and the used UserService and OrderService will also be injected into UserController.

Component

java

@Service
public class UserComponent {
    
    private final UserService userService;
    
    public UserComponent(UserService userService) {
        this.userService = userService;
    }

    public User deleteUser(Long userId) { 
        return userService.selectUser(userId);
    }

    public User getUser(Long userId) {
        return userService.getUser(userId);
    public User selectUser(Long userId) {
        return userService.selectUser(userId);
    }
}
  • You can call the newly added deleteUser()
  • The previous getUser() method cannot be accessed, but the newly added selectUser() method can be accessed.

Service

java

@Service
public class UserService {
    
    private final UserDao userDao;
    
    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public User deleteUser(Long userId) { 
        return userDao.selectUser(userId);
    }

    public User getUser(Long userId) {
        return userDao.getUser(userId);
    public User selectUser(Long userId) {
        return userDao.selectUser(userId);
    }
}
  • You can call the newly added deleteUser()
  • The previous getUser() method cannot be accessed, but the newly added selectUser() method can be accessed.

Repository

Currently supports MybatisPlus hot reload, for details, click MyBatisPlus hot reload to view

Configuration

java
@Configuration
public class UserConfig {

    @Value("${user.name}") 
    private String userName;

    @Value("${user.avatar}") 
    private String userAvatar;
    @Value("${user.image}") 
    private String userImage;
}
  • Load the newly added user.name configuration
  • Load the modified user.image configuration
java
@Configuration
public class UserConfig {
    
    @Bean
    public UserBean userBean() {
        return new UserBean();
    }
}
  • Inject the newly added UserBean Bean

Abstract Class

When the abstract class is modified, all inherited subclass beans will be hot reloaded.

java
public abstract class User {
    
    @Resource
    protected UserDao userDao; 
    
    public User getUser(Long userId) { 
        return userDao.getUser(userId); 
    } 
    
}

@Component
public class DebugUser extends User {
    
}

@Component
public class ToolsUser extends User {

}

When the abstract class User is hot reloaded, both DebugUser and ToolsUser will be hot reloaded and can use the userDao property and the getUser(Long userId) method.

Interface

When the default method of an interface is modified, the changes will take effect on all beans in the implementation class.

java
public interface User {
    
    default User getDefaultUser() { 
        return new DebugUser(); 
    } 
    
}

@Component
public class DebugUser implements User {
    
}

@Component
public class ToolsUser implements User {

}

When the interface User is hot-reloaded, DebugUser and ToolsUser will be hot-reloaded and can use the getDefaultUser() method.

More

In theory, all business classes managed by Spring Bean can be hot-reloaded.