简体   繁体   English

使用Hibernate / JPA设置密码运行时

[英]Set password runtime with Hibernate / JPA

I have a web app with a db connection properties defined in persistence.xml. 我有一个Web应用程序,在persistence.xml中定义了数据库连接属性。

I am using contained manager persistence with the entity manager injected using @PersistenceContext. 我正在使用包含管理器持久性,并使用@PersistenceContext注入了实体管理器。

Today the password to the DB is in the persistence.xml file, but I would rather set it runtime (ask the user for it). 今天,数据库的密码在persistence.xml文件中,但是我宁愿将其设置为运行时(向用户询问)。

I am able to create a new EntityManager with the password runtime, but how can I "override" the injected one? 我可以使用密码运行时创建一个新的EntityManager,但是如何“覆盖”已注入的EntityManager?

Any other good ideas/approaches? 还有其他好的想法/方法吗?

Yes, this is a bit worried with configuring the profiles for different environments. 是的,这对于为不同环境配置配置文件有些担心。

Though it wont suffice your "runtime" setting of the password and replace the object, but it would be maintained as the spring bean profiles. 尽管它不能满足密码的“运行时”设置并替换对象,但仍将作为spring bean配置文件进行维护。

Something like this; 像这样的东西;

@Configuration
@Profile("dev")
public class StandaloneDataConfig {

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.HSQL)
            .addScript("classpath:com/bank/config/sql/schema.sql")
            .addScript("classpath:com/bank/config/sql/test-data.sql")
            .build();
    }

}

Lets say you want to load the profile for production ? 假设您要加载配置文件以进行生产?

You have to define another profile with @Profile("production") and load the properties file from the classpath location. 您必须使用@Profile("production")定义另一个配置文件,然后从类路径位置加载属性文件。

With spring boot, you can activate your profile as 使用Spring Boot,您可以将个人资料激活为

SPRING_PROFILES_ACTIVE=production mvn spring-boot:run

This can make you to change the password and bounce the server whenever it is required. 这可以使您更改密码并在需要时弹回服务器。 Anyways as you change the password, the connection needs a reboot. 无论如何,当您更改密码时,连接都需要重新启动。 So ideally this should make the job easy. 因此,理想情况下,这应该使工作变得容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM