title: Spring not injecting @Value or @Autowired annotated variables in PhysicalNamingStrategy Implementations:
body: <p>I am struggling to get my <code>PhysicalNamingStrategy</code> Implementation in a Spring Boot + Hibernate Project to work as expected. The problem is that both, <code>@Value</code> and <code>@Autowired</code> annotated variables are always null / not properly injected with values from my <code>application.properties</code>. I need this in order to be able to externalize parameters like schema and table names and i can't use the fixed variant like this:</p>
  <pre><code>@Entity
  @Table(name = &quot;table_name&quot;, schema = &quot;primary_schema&quot;)
  </code></pre>
  <p>Well actually I do, but the <code>PhysicalNamingStrategy</code> comes in and &quot;translates&quot; the static names to the parameterized names. At least that is what should happen.</p>
  <p>I am 3/4 sure, that Spring is aware of my Class (Package and Class is scanned / found via ComponentScan) because when I create duplicates I get an expected Exception during bootup telling me that Spring found 2 identical beans. So I have no clue why the variables are not injected properly in this Class. Other classes (in the same and in other packages!) work fine with <code>@Value</code> and <code>@Autowired</code> annotations.</p>
  <p>My project structure looks like this (reduced to the relevant parts):</p>
  <pre><code>project/src/main/java/
  ----------------------org.packagename
  -------------------------configuration
  -----------------------------BeanConfig.java
  -----------------------------DataSourceConfiguration.java
  -----------------------------PhysicalNamingStrategyImpl.java
  -------------------------constant
  -------------------------controller
  -------------------------exception
  -------------------------model
  -------------------------repository
  -------------------------scheduler
  -------------------------service
  -------------------------AppMainClass.java
  project/src/main/resources
  ----------------------application.properties
  ----------------------application-devel.properties
  ----------------------application-prod.properties
  ----------------------schema-h2.sql
  ----------------------log4j2.xml
  </code></pre>
  <p>I am using:</p>
  <pre><code>- spring-boot-2.3.2 (multiple dependencies like spring-boot-autoconfigure, etc.)
  - spring-5.2.8 (multiple dependencies like spring-beans, etc.
  - hibernate-5.4.18
  </code></pre>
  <p>I configured Spring to use that Naming Strategy Implementation in the <code>application.properties</code> using this parameter:</p>
  <pre><code>spring.jpa.properties.hibernate.physical_naming_strategy=org.packagename.configuration.PhysicalNamingStrategyImpl
  </code></pre>
  <p>The Class is definitly used because i see the expected Log-Outputs as well as reach my breakpoints in the methods of the Class. So up until here everything is working as it should.
  But the <code>@Value</code> annotated variables are always null while in other Classes the same values have the expected values from the properties file.</p>
  <p>Here is the (test) class in Question (added those manual variable initialization after the <code>//FIXME</code> Comments in order to make it work at all, this will be removed as soon as the injection is working):</p>
  <pre class="lang-java prettyprint-override"><code>package org.packagename.configuration;
  
  import java.io.Serializable;
  
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
  import org.hibernate.boot.model.naming.Identifier;
  import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
  import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.stereotype.Component;
  
  /**
   * //stripped
   */
  @Component
  public class PhysicalNamingStrategyImpl implements PhysicalNamingStrategy, Serializable {
      //public class PhysicalNamingStrategyImpl extends PhysicalNamingStrategyStandardImpl implements Serializable {
  
      /** */
      private static final long serialVersionUID = 2389476641866177676L;
  
      //  public static final PhysicalNamingStrategyImpl INSTANCE = new PhysicalNamingStrategyImpl();
  
      private Logger logger = LogManager.getLogger(PhysicalNamingStrategyImpl.class);
  
      @Value(&quot;${app.primary.schema.name}&quot;)
      private String primarySchemaName;
  
      @Value(&quot;${app.primary.table.name}&quot;)
      private String primaryTableName;
  
      @Value(&quot;${app.secondary.schema.name}&quot;)
      private String secondarySchemaName;
  
      @Value(&quot;${app.secondary.table.name}&quot;)
      private String secondaryTableName;
  
  
      @Override
      public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment context) {
  
          logger.debug(&quot;toPhysicalSchemaName: {} with primarySchemaName: {}, secondarySchemaName: {}&quot;, name, primarySchemaName, secondarySchemaName);
  
          if (name == null) {
              return null;
          }
  
          // FIXME @Value always null.... 
          primarySchemaName = &quot;ACTUAL_SCHEMA_NAME&quot;;
          secondarySchemaName = &quot;ACTUAL_SCHEMA_NAME2&quot;;
  
          switch (name.getText()) {
              case &quot;primary_schema&quot;:
                  return new Identifier(primarySchemaName, name.isQuoted());
              case &quot;secondary_schema&quot;:
                  return new Identifier(secondarySchemaName, name.isQuoted());
              default:
                  return name;
          }
      }
  
  
      @Override
      public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
  
          logger.debug(&quot;toPhysicalTableName: {} with primaryTableName : {}, secondaryTableName : {}&quot;, name, primaryTableName , secondaryTableName );
  
          if (name == null) {
              return null;
          }
  
          // FIXME @Value always null....
          primaryTableName = &quot;ACTUAL_TABLE_NAME&quot;;
          secondaryTableName = &quot;ACTUAL_TABLE_NAME2&quot;;
  
          switch (name.getText()) {
              case &quot;table_name&quot;:
                  return new Identifier(syncLogTableTableName, name.isQuoted());
              case &quot;table_name2&quot;:
                  return new Identifier(assetExportDataTableName, name.isQuoted());
              default:
                  return name;
          }
      }
  
  
      @Override
      public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
          return name;
      }
  
  
      @Override
      public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment context) {
          return name;
      }
  
  
      @Override
      public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment context) {
          return name;
      }
  
  }
  </code></pre>
  <p>I am not instantiating this Class manually (no <code>new PhysicalNamingStrategyImpl()</code>) in the Code of my project so it should be handled completly by Spring itself. But I am not sure how, where and when does Spring create this Class during boot.</p>