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" ) ;
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" ) ;
System. setProperty ( "spring.profiles.active" , "1235456" ) ;
String activeProfile = System. getProperty ( "spring.profiles.active" ) ;
System. out. println ( "1111" + activeProfile) ;
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." ) ;
}
}
}