<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=schlaufuchs" rel="nofollow">Kai Hackemesser</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8583?redirect=false" rel="nofollow">SPR-8583</a></strong> and commented</p>
<p dir="auto">I have the web application context and the servlet context converted from xml to java config. The web.xml is updated according to the online documentation example. I use <code class="notranslate">@Autowire</code> annotation in the servlet config to get the beans required for the form controllers. I have autowired the web application context config class, too, as I want to retrieve some fields from there that I have <code class="notranslate">@Value</code> annotated, by calling some type converting methods. The autowiring seems to fail as I get Nullpointer exceptions on lines where the webapp config class method is called. I was debugging the server: the web application context has been built using the config class and the fields I want to call methods on are filled properly. The <code class="notranslate">@Autowired</code> config field is null when the <code class="notranslate">@Bean</code> annotated method wants to use it.</p>
<p dir="auto">Here some short example lines from our project:</p>
<p dir="auto">web.xml:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;web-app xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  version=&quot;2.4&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;&gt;
...
  &lt;servlet&gt;
...
    &lt;init-param&gt;
      &lt;param-name&gt;contextClass&lt;/param-name&gt;
      &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;com.ourcompany.ServletConfig&lt;/param-value&gt;
    &lt;/init-param&gt;
...
  &lt;/servlet&gt;
...
  &lt;context-param&gt;
    &lt;param-name&gt;contextClass&lt;/param-name&gt;
    &lt;param-value&gt;
          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      &lt;/param-value&gt;
  &lt;/context-param&gt;

  &lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;
      com.ourcompany.ApplicationConfig
    &lt;/param-value&gt;
  &lt;/context-param&gt;
...
&lt;/web-app&gt;"><pre class="notranslate"><code class="notranslate">&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
...
  &lt;servlet&gt;
...
    &lt;init-param&gt;
      &lt;param-name&gt;contextClass&lt;/param-name&gt;
      &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;com.ourcompany.ServletConfig&lt;/param-value&gt;
    &lt;/init-param&gt;
...
  &lt;/servlet&gt;
...
  &lt;context-param&gt;
    &lt;param-name&gt;contextClass&lt;/param-name&gt;
    &lt;param-value&gt;
          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      &lt;/param-value&gt;
  &lt;/context-param&gt;

  &lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;
      com.ourcompany.ApplicationConfig
    &lt;/param-value&gt;
  &lt;/context-param&gt;
...
&lt;/web-app&gt;
</code></pre></div>
<p dir="auto">ServletConfig.java:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="...
@Configuration
@ImportResource(&quot;classpath:/META-INF/iem/servletPropertyConfigurer.xml&quot;)
public class ServletConfig
{
...
  @Autowired
  private ApplicationConfig applicationConfig;
...
  @Bean
  public DbStatsController dbStatsController()
  {
    DbStatsController controller = new DbStatsController();
    controller.setManageUrl(applicationConfig.getDatabaseManageUrl());
...
    return controller;
  }
..."><pre class="notranslate"><code class="notranslate">...
@Configuration
@ImportResource("classpath:/META-INF/iem/servletPropertyConfigurer.xml")
public class ServletConfig
{
...
  @Autowired
  private ApplicationConfig applicationConfig;
...
  @Bean
  public DbStatsController dbStatsController()
  {
    DbStatsController controller = new DbStatsController();
    controller.setManageUrl(applicationConfig.getDatabaseManageUrl());
...
    return controller;
  }
...
</code></pre></div>
<p dir="auto">ApplicationConfig.java:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@Configuration(&quot;applicationConfig&quot;)
@ImportResource({
    &quot;classpath:/META-INF/...&quot; })
public class ApplicationConfig
{
...
  @Value(&quot;$[database.manage.url]&quot;)
  private String databaseManageUrl;

...
  public String getDatabaseManageUrl()
  {
    return databaseManageUrl;
  }
"><pre class="notranslate"><code class="notranslate">@Configuration("applicationConfig")
@ImportResource({
    "classpath:/META-INF/..." })
public class ApplicationConfig
{
...
  @Value("$[database.manage.url]")
  private String databaseManageUrl;

...
  public String getDatabaseManageUrl()
  {
    return databaseManageUrl;
  }

</code></pre></div>
<p dir="auto">Following the documentation a Config class can be autowired like any other bean, but it doesn't work here. and I don't get a BeanNotFound or similar exception, it just fails with a NullpointerException when trying to access the autowired field.</p>
<hr>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398113839" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13226" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13226/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13226">#13226</a> unresolvable circular reference when bean defined in xml config refers to bean defined in outer java config (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=schlaufuchs" rel="nofollow">Kai Hackemesser</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8583?redirect=false" rel="nofollow">SPR-8583</a></strong> and commented</p>
<p dir="auto">I have the web application context and the servlet context converted from xml to java config. The web.xml is updated according to the online documentation example. I use <code class="notranslate">@Autowire</code> annotation in the servlet config to get the beans required for the form controllers. I have autowired the web application context config class, too, as I want to retrieve some fields from there that I have <code class="notranslate">@Value</code> annotated, by calling some type converting methods. The autowiring seems to fail as I get Nullpointer exceptions on lines where the webapp config class method is called. I was debugging the server: the web application context has been built using the config class and the fields I want to call methods on are filled properly. The <code class="notranslate">@Autowired</code> config field is null when the <code class="notranslate">@Bean</code> annotated method wants to use it.</p>
<p dir="auto">Here some short example lines from our project:</p>
<p dir="auto">web.xml:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;web-app xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  version=&quot;2.4&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;&gt;
...
  &lt;servlet&gt;
...
    &lt;init-param&gt;
      &lt;param-name&gt;contextClass&lt;/param-name&gt;
      &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;com.ourcompany.ServletConfig&lt;/param-value&gt;
    &lt;/init-param&gt;
...
  &lt;/servlet&gt;
...
  &lt;context-param&gt;
    &lt;param-name&gt;contextClass&lt;/param-name&gt;
    &lt;param-value&gt;
          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      &lt;/param-value&gt;
  &lt;/context-param&gt;

  &lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;
      com.ourcompany.ApplicationConfig
    &lt;/param-value&gt;
  &lt;/context-param&gt;
...
&lt;/web-app&gt;"><pre class="notranslate"><code class="notranslate">&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
...
  &lt;servlet&gt;
...
    &lt;init-param&gt;
      &lt;param-name&gt;contextClass&lt;/param-name&gt;
      &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;com.ourcompany.ServletConfig&lt;/param-value&gt;
    &lt;/init-param&gt;
...
  &lt;/servlet&gt;
...
  &lt;context-param&gt;
    &lt;param-name&gt;contextClass&lt;/param-name&gt;
    &lt;param-value&gt;
          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      &lt;/param-value&gt;
  &lt;/context-param&gt;

  &lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;
      com.ourcompany.ApplicationConfig
    &lt;/param-value&gt;
  &lt;/context-param&gt;
...
&lt;/web-app&gt;
</code></pre></div>
<p dir="auto">ServletConfig.java:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="...
@Configuration
@ImportResource(&quot;classpath:/META-INF/iem/servletPropertyConfigurer.xml&quot;)
public class ServletConfig
{
...
  @Autowired
  private ApplicationConfig applicationConfig;
...
  @Bean
  public DbStatsController dbStatsController()
  {
    DbStatsController controller = new DbStatsController();
    controller.setManageUrl(applicationConfig.getDatabaseManageUrl());
...
    return controller;
  }
..."><pre class="notranslate"><code class="notranslate">...
@Configuration
@ImportResource("classpath:/META-INF/iem/servletPropertyConfigurer.xml")
public class ServletConfig
{
...
  @Autowired
  private ApplicationConfig applicationConfig;
...
  @Bean
  public DbStatsController dbStatsController()
  {
    DbStatsController controller = new DbStatsController();
    controller.setManageUrl(applicationConfig.getDatabaseManageUrl());
...
    return controller;
  }
...
</code></pre></div>
<p dir="auto">ApplicationConfig.java:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@Configuration(&quot;applicationConfig&quot;)
@ImportResource({
    &quot;classpath:/META-INF/...&quot; })
public class ApplicationConfig
{
...
  @Value(&quot;$[database.manage.url]&quot;)
  private String databaseManageUrl;

...
  public String getDatabaseManageUrl()
  {
    return databaseManageUrl;
  }
"><pre class="notranslate"><code class="notranslate">@Configuration("applicationConfig")
@ImportResource({
    "classpath:/META-INF/..." })
public class ApplicationConfig
{
...
  @Value("$[database.manage.url]")
  private String databaseManageUrl;

...
  public String getDatabaseManageUrl()
  {
    return databaseManageUrl;
  }

</code></pre></div>
<p dir="auto">Following the documentation a Config class can be autowired like any other bean, but it doesn't work here. and I don't get a BeanNotFound or similar exception, it just fails with a NullpointerException when trying to access the autowired field.</p>
<hr>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398113839" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13226" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13226/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13226">#13226</a> unresolvable circular reference when bean defined in xml config refers to bean defined in outer java config (<em><strong>"duplicates"</strong></em>)</li>
</ul>