java


Spring #autowired does not work


I have a problem with spring DI via annotations, here is my app:
#Service
public class Test {
#Autowired
private GpsPointEntityDao gpsPointEntityDao;
public void test() {
if (gpsPointEntityDao == null)
System.out.println("It's null!\n" + gpsPointEntityDao);
}
}
generic interface:
public interface GenericDao<T extends DomainObject> {
public T find(long id);
public List<T> getAll();
public void save(T object) throws DataAccessException;
public void delete(T object) throws DataAccessException;
}
concrete interface:
public interface GpsPointEntityDao extends GenericDao<GpsPointEntity> {}
abstract implementation:
abstract class AbstractGenericDaoJpa<T extends DomainObject> implements GenericDao<T> {
private final Class<T> entityType;
protected EntityManager entityManager;
public AbstractGenericDaoJpa() {
this.entityType = (Class<T>) GenericTypeResolver.resolveTypeArgument(getClass(), GenericDao.class);
}
#PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
#Transactional
#Override
public T find(long id) {
return entityManager.find(entityType, id);
}
#Transactional
#Override
public List<T> getAll() {
return entityManager.createQuery("SELECT e FROM " + entityType.getName() + " e").getResultList();
}
#Transactional
#Override
public void save(T object) throws DataAccessException {
entityManager.persist(object);
}
#Transactional
#Override
public void delete(T object) throws DataAccessException {
entityManager.remove(object);
}
}
concrete class:
#Repository
public class GpsPointEntityDaoJpa extends AbstractGenericDaoJpa<GpsPointEntity> implements GpsPointEntityDao {}
And my appcontext:
<context:component-scan base-package="com.test"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="basicDataSource"/>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<tx:annotation-driven mode="proxy" transaction-manager="transactionManager"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
The result of app is:
It's null!
I have been spending all day for searching the problem but unsuccessfully. Where someone sees a problem?
I found this message in logs:
INFO org.springframework.context.support.ClassPathXmlApplicationContext - Bean 'entityManagerFactory' of type [class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
I don't see a problem with that. With roughly the same code as you posted, I ran this:
public static void main(String[] args) {
Test bean = new ClassPathXmlApplicationContext("/applicationContext.xml").getBean(Test.class);
bean.test();
}
The Test bean was injected correctly. I can make my test project available if you want to take a look. Are you sure you're getting an injected version of Test? How are you obtaining it?
Edit: Your instance isn't being injected because you're instantiating it yourself instead of letting Spring do it. Unless you use AspectJ to inject objects, Spring can/will only inject objects that it is managing. When you call new Test(), you're not getting the instance from Spring, and Spring doesn't know anything about that instance you've created.
you can use the #Resource annotation instead of #Autowire.
Try whether this works.

Related Links

Getting Java.io.closeable Error in Android
Concatenate uri with # in java using UriComponentsBuilder
Daemon Threads, thread count, and total started thread count
java8 Interface allowing public default method
How to plot color map in java?
PHP Server | Java Client - SSLSocket Certificate
How to manipulate volley response in android?
File not found on Android Device
Paypal Express Checkout Security Header is not valid
How to use drawOval() method correctly?
How to extract the value of TIME(7) field of MS SQL SERVER 2008 R2 in java?
Final keyword in Java [duplicate]
ADT list of integers to test program
Image Failed to Load with Picasso
Custom Code Panels preview in InstallAnywhere?
How to get exact index of selected item from filtered adapter?

Categories

HOME
log4j
keycloak
zeromq
c#-4.0
react-virtualized
lodash
react-router
read-eval-print-loop
q
youtube-api-v3
mvvmcross
serverspec
in-app-purchase
podio
qore
portia
multiple-records
android-youtube-api
maude-system
facebook-php-sdk
ssl-client-authentication
volttron
midi
wheelnav.js
resize
zend-framework3
php-7.1
orchardcms
jsprit
serverless-framework
tar
csrf-protection
sox
core-text
delicious-api
bpel
tinymce-4
karaf
stringtemplate
phpfreechat
openoffice.org
xmlreader
io-redirection
devextreme
main
serve
cookiecutter-django
eigenvalue
apache-fop
multipeer-connectivity
jquery-validate
feeds
boost-preprocessor
flow-control
gcsfuse
smart-table
background-service
google-feed-api
vmware-tools
holder.js
qcustomplot
python-stackless
sdf
nessus
php-internals
ptrace
tform
freedesktop.org
jfugue
collapse
qdialog
fouc
varargs
tableau-online
twirl
e10s
system.reflection
typekit
jscript.net
xcode-6.2
markers
openexr
mesa
android-radiobutton
jubula
kgdb
balanced-payments
page-layout
gridfs
jmapviewer
google-cloud-save
rabl
ember-app-kit
flashvars
smtp-auth
specification-pattern
selected
quickdialog
cisco-jtapi
multipage
cascalog
armcc
tridion-worldserver
ticoredatasync
windows-phone-7.1.1
fireworks
invite
file-comparison
appendto
yui-datatable
krl
sudzc
gamma
icanhaz.js
ncqrs
multi-tier
compiler-specific
misv

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App