我正在尝试制作项目并且遇到连接java类lang的问题。 我的春天配置。
<bean id="userService" class="com.event.services.UserServiceImpl">
<property name="userRepository" ref="userRepository"></property>
</bean>
<bean name="userRepository" class="com.event.repositories.UserRepository">
<property name="entityClass">
<bean class="java.lang.Class" factory-method="forName">
<constructor-arg value="com.event.domain.User"/>
</bean>
</property>
</bean>
类: package com.event.repositories;
import com.event.domain.User;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository extends JPARepository<String, User> implements UserBonusRepository {
@Override
public User findByKey(String id) {
return em.find(entityClass, id);
}
@Override
public Iterable<User> findByKeys(Iterable<String> ids) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void persist(User entity) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public User merge(User entity) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void remove(User entity) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void saveAll(Iterable<User> entities) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isUserValid(String userName, String password) {
User user = em.find(entityClass, userName);
if (user == null || !user.getPassword().equals(password)) {
return false;
}
return true;
}
}
在JpaRepository中我有这个
@PersistenceContext
public EntityManager em;
@Autowired
public Class<T> entityClass;
问题: 没有类型&#39; java.lang.Class&#39;的限定bean可用:预计至少有1个豆有资格作为autowire候选者。 我试图解决这个问题并找到它 Spring syntax for setting a Class object?但这没有帮助。有人知道如何解决这个问题吗? 先感谢您。 :)
答案 0 :(得分:0)
你试过这个:
删除
@Autowired
来自public Class<T> entityClass;
并像这样更新你的xml
<bean name="userRepository" class="com.event.repositories.UserRepository">
</bean>
<强>更新强>
另一种解决方案是:
通过使方法getEntityClass为abstract来使JpaRepository
抽象化,并在需要类类型的地方使用它并删除entityClass字段。 Child类将通过实现此方法来提供EntityClass。
创建JpaRepository
(抽象,或不取决于您的设计)并删除entityClass字段并添加一个方法以在运行时发现参数化类型并在需要类类型的地方使用它,例如:
@SuppressWarnings("unchecked") private Class<T> getEntityClass() { return (Class<T>) ( (ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1]; }
参数索引1,因为T是定义的第二个参数化类型
UserRepository
super(User.class);
请注意,我已从xml
中删除了entityClass连线