PhysicalNamingStrategy bean仅通过配置文件实例化 [英] PhysicalNamingStrategy bean only instantiates via config file
问题描述
我在带有Hibernate的Spring应用程序中有一个自定义命名策略:
I have a custom naming strategy in a Spring app with Hibernate:
public class MyCustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
@Override
public Identifier toPhysicalTableName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return Identifier.toIdentifier("my_custom_table_name");
}
// ...
}
The Spring documentation says I can tell Hibernate to use it either by setting spring.jpa.hibernate.naming.physical-strategy
(which works fine), or like this:
或者,如果在应用程序上下文中有
ImplicitNamingStrategy
或PhysicalNamingStrategy
bean,则Hibernate将被自动配置为使用它们.
Alternatively, if
ImplicitNamingStrategy
orPhysicalNamingStrategy
beans are available in the application context, Hibernate will be automatically configured to use them
我需要使用第二种方法,因为我需要将一些信息从Spring的上下文传递到我的命名策略.但是,我无法正常工作.
I need to use this second method, as I need to pass some info from Spring's context to my naming strategy. However, I can't get it to work.
我正在以下配置类中创建Bean:
I'm creating the bean in the following configuration class:
@Configuration
public class PersistenceConfiguration {
@Bean
public PhysicalNamingStrategy physicalNamingStrategy() {
return new MyCustomPhysicalNamingStrategy();
}
}
我在这里想念什么?
谢谢.
我正在使用Spring Boot/Spring JPA 1.5.9.RELEASE,它提供了Hibernate core v.5.0.12.Final和Hibernate JPA API 2.1.
I'm on Spring Boot/Spring JPA 1.5.9.RELEASE, which gives me Hibernate core v.5.0.12.Final and Hibernate JPA API 2.1.
推荐答案
似乎只有Spring Boot 2支持.
It seems that this is only supported with spring boot 2.
请参阅:该文件在Spring Boot 1.5中不存在: https://github.com/spring-projects/spring-boot/tree/v1.5.14.RELEASE/spring-boot -autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa
This file is not present in spring boot 1.5: https://github.com/spring-projects/spring-boot/tree/v1.5.14.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa
(HibernateJpaAutoConfiguration.java
没有它)
这篇关于PhysicalNamingStrategy bean仅通过配置文件实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!