NullPointerException for EntityManager using @PersistenceContext
<p>I'm trying to create an auditing service that has an <code>EntityManager</code>. However, this <code>EntityManager</code> is never initialized when I run the application, so when calling it, it throws a <code>NullPointerException</code>. </p>

<p>My code looks like this:</p>

<pre><code>import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@Service
public class AuditServiceImpl implements AuditService {

    private static final String FULL_NAME = "%s %s";

    @PersistenceContext
    private EntityManager entityManager;

    public EntityManager getEntityManager(){
        return entityManager;
    }
}
</code></pre>

<p>I expected that the <code>@PersistenceContext</code> annotation would fix this for me, but it did not. I saw several other posts about similar problems, but most solutions seem outdated, so I decided to create another post. </p>

<p>Can anybody shed some light on the problem, maybe even a solution?</p>