[Question] The springboot project restricts the instance port number through the database

        A small problem is recorded this time, in case it can be used in the future. When the springboot project starts, you can specify the port number to start it. But in the company's project, I found that he has made a control on the application port number , only a certain application can only run the specified port number, and this operation is recorded this time.


1. The first step

        Create a new database table to save the configuration of the port number and application restrictions , as follows:

CREATE TABLE `sys_service` (
  `service_code` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '服务名称',
  `service_desc` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '服务描述',
  `service_port` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '服务端口号',
  `status` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '状态,0是启用,1是停用'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin;

Two, the second step

        This operation is based on the fact that the project has integrated mybatis-plus . As mentioned earlier, I forgot to look it up.

(8 messages) [4] springboot integrates mybatis-plus (super detailed) (on)_小z♂'s Blog-CSDN Blog_springbootplus

Add the following class         to the project :

        Inject the class into the spring container, and inject the mapper of mybatis into the class. The mapper maps the entity class of the table just created. Specifically, you can turn over the previous integration of mabatis. Here, we will not take screenshots of the mapper layer and the entity class. Mainly this class.

  • Get the service name of the application through @Value("${spring.application.name}")
  • Get the port number of the application through @Value("${server.port}")

 

        This class implements the InitializingBean interface , which has only one method.

        The InitializingBean interface provides an initialization method for beans. It only includes the afterPropertiesSet method. All classes that inherit this interface will execute this method when the spring container initializes the bean.

        In this way, you can use it to do some things you want to do when the project is started, such as storing some global static attributes in redis or what you want to do in this article, to judge and limit the port number of the application.

Three, the third step

        View test results:

        The port of the demo service configured in the database is 8085 .

        It can be seen that the 8085 starts successfully .

        Now modify the port to 8086

        It can be seen that the startup failed .

Four, the fourth step

        Supplement : In addition, instead of implementing the above interface,a bean is injected into the spring container through a label, byadding the init-method attributeto specify a method, it is also possible to execute a certain method when initializing a certain bean.

For example:

<bean id="testBean" class="com.Test" init-method="testMethod"></bean>

Guess you like

Origin blog.csdn.net/weixin_56995925/article/details/125583168