ssm项目启动的时候 修改 配置文件 定义的值

ssm项目启动的时候 修改 配置文件 定义的值


import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.env.ConfigurableEnvironment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class PasswordInitializer implements InitializingBean {

    private final ConfigurableEnvironment environment;

    public PasswordInitializer(ConfigurableEnvironment environment) {
        this.environment = environment;
    }

    @Override
    public void afterPropertiesSet() throws IOException {
        String encryptedUsername = environment.getProperty("jdbc.username");
//        String decryptedUsername = decryptUsername(encryptedUsername);

        writeDecryptedPasswordToFile(encryptedUsername);
        verifyFileContent(encryptedUsername);
    }
    //修改
    private void writeDecryptedPasswordToFile(String decryptedPassword) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("ty-jdbc.properties"))) {
            writer.write("jdbc.username=" + "decryptedPassword");
        } catch (IOException e) {
            throw new RuntimeException("Failed to write decrypted password to ty-jdbc.properties", e);
        }
    }
    //修改后查询
    private void verifyFileContent(String expectedValue) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader("ty-jdbc.properties"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("jdbc.username=")) {
                    String actualValue = line.split("=")[1].trim();
                    if (actualValue.equals("decryptedPassword")) {
                        System.out.println("Successfully updated jdbc.username in ty-jdbc.properties.");
                        return;
                    } else {
                        System.err.println("Update failed: Expected value does not match the one in file.");
                        return;
                    }
                }
            }
            System.err.println("Update failed: jdbc.username property not found in ty-jdbc.properties.");
        }
    }

}

xml 随便一个文件加上这个

        <bean id="passwordInitializer" class="文件包名.PasswordInitializer" init-method="afterPropertiesSet"/>

或者用这种方式

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Configuration
public class PasswordInitializer {

    private final ConfigurableEnvironment environment;

    public PasswordInitializer(ConfigurableEnvironment environment) {
        this.environment = environment;
    }

    @PostConstruct
    public void initialize() throws IOException {
        String encryptedUsername = environment.getProperty("jdbc.connectionSecret");
//        String decryptedUsername = decryptUsername(encryptedUsername);

//方式 1
// 设置系统属性
        System.setProperty("spring.profiles.active", "1235456");

// 读取系统属性
        String activeProfile = System.getProperty("spring.profiles.active");
        
        System.out.println("1111"+activeProfile);
//方式 2
        String password = "123456";
        writeDecryptedPasswordToFile(password);
        verifyFileContent(password);
    }

    private void writeDecryptedPasswordToFile(String decryptedPassword) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("ty-jdbc.properties"))) {
            writer.write("jdbc.connectionSecret=" + decryptedPassword);
        } catch (IOException e) {
            throw new RuntimeException("Failed to write decrypted password to ty-jdbc.properties", e);
        }
    }

    private void verifyFileContent(String expectedValue) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader("ty-jdbc.properties"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("jdbc.connectionSecret")) {
                    System.out.println("111111" +line);
                    String actualValue = line.split("=")[1].trim();
                    if (actualValue.equals(expectedValue)) {
                        System.out.println("Successfully updated jdbc.username in ty-jdbc.properties.");
                        return;
                    } else {
                        System.err.println("Update failed: Expected value does not match the one in file.");
                        return;
                    }
                }
            }
            System.err.println("Update failed: jdbc.username property not found in ty-jdbc.properties.");
        }
    }

}
上一篇:C++11 数据结构5 队列的概念,队列的顺序存储,实现,测试


下一篇:SQLite运行时可加载扩展(三十五)