@Autowiredが動作するのに問題があります。申し訳ありませんが、私は任意の条件をねじれ、私は比較的新しい春です。春のオートワイヤリングとクラスの継承
春のバージョンは3.0.5.RELEASEで、私は豆の定義でcontext:component-scanを使用しています。
これは@Autowired注釈で動作します:
@Component
public class UserDao {
@PersistenceContext
protected EntityManager em;
@Transactional
public User findById(Long id) {
return em.find(User.class, id);
}
}
これは@Autowiredアノテーションでは動作しません:このセットアップ、で
@Component
public class UserDao implements Dao<User> {
@PersistenceContext
protected EntityManager em;
@Transactional
public User findById(Long id) {
return em.find(User.class, id);
}
}
私が追加したすべては 'ダオを実装'と、次のようになります。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [web.rs.persistence.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
参照のためにいくつかの他のクラス:
Dao.java(インターフェース):
public interface Dao<T extends BaseEntity> {
T findById(Long id);
}
UserResource.java:
@Component
public class UserResource {
@Autowired
UserDao userDao;
public User getUser(Long id) {
return userDao.findById(id);
}
}
applicationContext.xmlを:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="web.rs" />
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config.properties" />
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="persistenceUnitName" value="${persistence.unit}" />
</bean>
<bean id="trx-manager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<tx:annotation-driven transaction-manager="trx-manager" />
</beans>
は誰でも当てることができますいくつかのこの問題についての光?私はクラス継承を維持したいと思います。
ありがとうございます!
コンテキストファイルにBeanを定義しましたか? UserDaoのBeanが複数ある場合は、 '@ autowire'とともに' @ qualifier'を試してください。 –
クラスパスにUserDaoが1つしかありません。 私の豆のファイル。 –
あなたは 'NoSuchBeanDefinitionException'を取得しているので、コンテキストファイルにBeanを定義しているようです。コンテキストファイルを投稿してください。 –