运行.sh文件时出现此错误:
Error creating bean with name 'frdParameterRule2DAO' defined in class path resource [com/.../beans.xml]:
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.....dao.FrdParameterRule2DAO]: Specified class is an interface
这是文件.sh执行的代码(我省略了不必要的代码):
public final class FraudCacheBatchJob {
private static final String CLASS_NAME = FraudCacheBatchJob.class.getCanonicalName();
private static final String CONTEXT_XML_NAME = "/beans.xml";
private static String getPackageLocation() {
return FraudCacheBatchJob.class.getPackage().getName().replace(".", "/");
}
public static void main(String[] args) {
try {
ApplicationContext context = new ClassPathXmlApplicationContext(getPackageLocation() + CONTEXT_XML_NAME);
CachedRule cachedRule = context.getBean("cachedRule", CachedRule.class);
cachedRule.getCacheRule2();
} catch (Exception ex) {
}
}
}
CachedRule
:
public interface CachedRule {
void getCacheRule2();
...
}
CachedRuleImpl
:
@Service("CachedRule")
public class CachedRuleImpl implements CachedRule {
@Autowired
FrdParameterRule2DAO frdParameterRule2DAO;
@Cacheable(value = "ruleCache", key = "'cache-getFrdRule2'")
public void getCacheRule2() {
frdParameterRule2DAO.findAll();
}
...
}
FrdParameterRule2DAO
:
public interface FrdParameterRule2DAO extends JpaRepository<parameterRule, Long> {
...
}
beans.xml
:
<bean id="frdParameterRule2DAO" class="com.....dao.FrdParameterRule2DAO"></bean>
<bean id="cachedRule" class="com.....spring.fraud.rule.CachedRuleImpl">
<property name="frdParameterRule2DAO" ref="frdParameterRule2DAO" />
</bean>
当我尝试将DAO更改为public class
时,我收到错误:
The type JpaRepository<FrdParameterRule2,Long> cannot be the superclass of FrdParameterRule2DAO; a superclass must be a class
我该怎么办?感谢。
修改
我试过这个(@StanislavL):
FrdParameterRule2DAO
:
@Repository
public interface FrdParameterRule2DAO extends JpaRepository<parameterRule, Long> {
...
}
beans.xml
:
<!-- <bean id="frdParameterRule2DAO" class="com.....dao.FrdParameterRule2DAO"></bean> -->
<bean id="cachedRule" class="com.....spring.fraud.rule.CachedRuleImpl">
<property name="frdParameterRule2DAO" ref="frdParameterRule2DAO" />
</bean>
然后得到:
Error creating bean with name 'cachedRule' defined in class path resource [com/.../beans.xml]: Cannot resolve reference to bean 'frdParameterRule2DAO' while setting bean property 'frdParameterRule2DAO'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'frdParameterRule2DAO' is defined
答案 0 :(得分:0)
从xml中删除bean id="frdParameterRule2DAO"
并添加
@Repository
FrdParameterRule2DAO frdParameterRule2DAO;
改为使用CachedRuleImpl。
还有一件事
您有interface FrdParameterRule2DAO extends JpaRepository<parameterRule, Long>
但希望列出其他实体List<FrdParameterRule2> result = frdParameterRule2DAO.findAll()
可能是实体类差异。