<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=vvvngo" rel="nofollow">Don Ngo</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-5138?redirect=false" rel="nofollow">SPR-5138</a></strong> and commented</p>
<p dir="auto">Consider the following example:</p>
<p dir="auto">********** The dependency class<br>
public class Dependency {<br>
<code class="notranslate">@Override</code><br>
public String toString() {<br>
return "Dependency bean";<br>
}<br>
}</p>
<p dir="auto">********** The parent domain class<br>
import javax.annotation.Resource;</p>
<p dir="auto">import org.springframework.beans.factory.annotation.Configurable;</p>
<p dir="auto"><code class="notranslate">@Configurable</code><br>
public class Parent {<br>
<code class="notranslate">@Resource</code><br>
protected Dependency dependency;<br>
}</p>
<p dir="auto">********** The child domain class<br>
public class Child extends Parent {<br>
}</p>
<p dir="auto">********** The context file (context.xml)</p>
<p dir="auto">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</p>
<p dir="auto">&lt;beans xmlns="<a href="http://www.springframework.org/schema/beans" rel="nofollow">http://www.springframework.org/schema/beans</a>"<br>
xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" rel="nofollow">http://www.w3.org/2001/XMLSchema-instance</a>"<br>
xmlns:context="<a href="http://www.springframework.org/schema/context" rel="nofollow">http://www.springframework.org/schema/context</a>"<br>
xsi:schemaLocation="<br>
<a href="http://www.springframework.org/schema/beans" rel="nofollow">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" rel="nofollow">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</a><br>
<a href="http://www.springframework.org/schema/context" rel="nofollow">http://www.springframework.org/schema/context</a> <a href="http://www.springframework.org/schema/context/spring-context-2.5.xsd%22%3E" rel="nofollow">http://www.springframework.org/schema/context/spring-context-2.5.xsd"&gt;</a></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;!-- Switch on the load-time weaving --&gt;
&lt;context:load-time-weaver /&gt;

&lt;!-- Apply dependency injection	to non-managed classes annotated with the @Configurable --&gt;
&lt;context:spring-configured /&gt;

&lt;!-- Activates various annotations to be detected in bean classes --&gt;
&lt;context:annotation-config /&gt;

&lt;bean id=&quot;dependency&quot; class=&quot;Dependency&quot; /&gt;"><pre class="notranslate"><code class="notranslate">&lt;!-- Switch on the load-time weaving --&gt;
&lt;context:load-time-weaver /&gt;

&lt;!-- Apply dependency injection	to non-managed classes annotated with the @Configurable --&gt;
&lt;context:spring-configured /&gt;

&lt;!-- Activates various annotations to be detected in bean classes --&gt;
&lt;context:annotation-config /&gt;

&lt;bean id="dependency" class="Dependency" /&gt;
</code></pre></div>
<p dir="auto">&lt;/beans&gt;</p>
<p dir="auto">Now the problem... The dependency injection works fine with the domain objects are created like this:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  Parent bean = new Parent();

             or this:

  Child bean = new Child();"><pre class="notranslate"><code class="notranslate">  Parent bean = new Parent();

             or this:

  Child bean = new Child();
</code></pre></div>
<p dir="auto">However, it will fail with the following statement (ie. the dependency field will be null (not injected) :</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Parent bean = new Child();"><pre class="notranslate"><code class="notranslate">Parent bean = new Child();
</code></pre></div>
<p dir="auto">Also, I've found that if the following statements are combined, it will fail too:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Parent bean = new Parent();
Parent bean2 = new Child();"><pre class="notranslate"><code class="notranslate">Parent bean = new Parent();
Parent bean2 = new Child();
</code></pre></div>
<p dir="auto">Below are some test cases to demonstrate this problem:</p>
<p dir="auto">******** Demonstrate successful injection<br>
import static org.junit.Assert.assertNotNull;</p>
<p dir="auto">import org.junit.Test;<br>
import org.junit.runner.RunWith;<br>
import org.springframework.test.context.ContextConfiguration;<br>
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;</p>
<p dir="auto"><code class="notranslate">@RunWith</code>(SpringJUnit4ClassRunner.class)<br>
<code class="notranslate">@ContextConfiguration</code>(locations = { "classpath:context.xml" })<br>
public final class PassTest {<br>
<code class="notranslate">@Test</code><br>
public final void case1() {<br>
Parent bean = new Parent();<br>
assertNotNull(bean.dependency);<br>
}</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@Test
public final void case2() {
	Child bean = new Child();
	assertNotNull(bean.dependency);
}"><pre class="notranslate"><code class="notranslate">@Test
public final void case2() {
	Child bean = new Child();
	assertNotNull(bean.dependency);
}
</code></pre></div>
<p dir="auto">}</p>
<p dir="auto">******** Demonstrate injection failure<br>
import static org.junit.Assert.assertNotNull;</p>
<p dir="auto">import org.junit.Test;<br>
import org.junit.runner.RunWith;<br>
import org.springframework.test.context.ContextConfiguration;<br>
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;</p>
<p dir="auto"><code class="notranslate">@RunWith</code>(SpringJUnit4ClassRunner.class)<br>
<code class="notranslate">@ContextConfiguration</code>(locations = { "classpath:context.xml" })<br>
public final class FailTest {<br>
<code class="notranslate">@Test</code><br>
public final void case3() {<br>
Parent bean = new Child();<br>
assertNotNull(bean.dependency);  // Will fail!!!  Why??!!!<br>
}<br>
}</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.5.5</p>
<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="398091403" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9895" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9895/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9895">#9895</a> <code class="notranslate">@Configureable</code> doesn't work right (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398089462" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9638" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9638/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9638">#9638</a> LTW spring-agent Weaving not performed within Test, if class to be woven is referenced by Generic Variable declaration</li>
</ul>
<p dir="auto">5 votes, 8 watchers</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=dhewitt" rel="nofollow">David Hewitt</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-2697?redirect=false" rel="nofollow">SPR-2697</a></strong> and commented</p>
<p dir="auto">Just upgraded from 2.0-RC2 to 2.0-Final. The refactoring of LocalSessionFactoryBean to introduce an Abstract baseclass seems to have broken the call to updateDatabaseSchema on initialisation. This means that setting schemaUpdate to true always causes a failure on startup.</p>
<p dir="auto">Previously the SessionFactory was created entirely within the afterPropertiesSet method, at the end of which updateDatabaseSchema was called.</p>
<p dir="auto">However, now the behaviour is as follows:</p>
<p dir="auto">AbstractSessionFactoryBean:<br>
public void afterPropertiesSet() throws Exception {<br>
SessionFactory rawSf = buildSessionFactory();<br>
this.sessionFactory = wrapSessionFactoryIfNecessary(rawSf);<br>
}</p>
<p dir="auto">LocalSessionFactoryBean:<br>
protected SessionFactory buildSessionFactory() throws Exception {<br>
SessionFactory sf = null;</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="            ...

	// Execute schema update if requested.
	if (this.schemaUpdate) {
		updateDatabaseSchema();
	}

	return sf;
}"><pre class="notranslate"><code class="notranslate">            ...

	// Execute schema update if requested.
	if (this.schemaUpdate) {
		updateDatabaseSchema();
	}

	return sf;
}
</code></pre></div>
<p dir="auto">LocalSessionFactoryBean:<br>
public void updateDatabaseSchema() throws DataAccessException {<br>
logger.info("Updating database schema for Hibernate SessionFactory");<br>
HibernateTemplate hibernateTemplate = new HibernateTemplate(getSessionFactory());<br>
...<br>
}</p>
<p dir="auto">AbstractSessionFactoryBean:<br>
protected final SessionFactory getSessionFactory() {<br>
if (this.sessionFactory == null) {<br>
throw new IllegalStateException("SessionFactory not initialized yet");<br>
}<br>
return this.sessionFactory;<br>
}</p>
<p dir="auto">At which point an "java.lang.IllegalStateException: SessionFactory not initialized yet" is thrown every time. This is because the call to getSessionFactory() happens as part of updatedDatabaseSchema(), which is called from buildSessionFactory() - which has not yet returned, and therefore the sessionFactory has not been assigned yet. Possibly the sessionFactory "sf" in the buildSessionFactory method should be passed as an argument to updateDatabaseSchema.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.0 final</p>