Spring 弹簧:Can';t加载应用程序上下文。NoSuchBeanDefinitionException
我希望任何人都能找到解决办法。 我实现了一个简单的测试来检查应用程序上下文是否正确启动,但我得到的只是一个NoSuchBeanDefinition异常 因此,我仔细查看了ctx-*.xml文件。遗憾的是,我什么也没找到 提前thx 测试准备: 例外情况:Spring 弹簧:Can';t加载应用程序上下文。NoSuchBeanDefinitionException,spring,applicationcontext,Spring,Applicationcontext,我希望任何人都能找到解决办法。 我实现了一个简单的测试来检查应用程序上下文是否正确启动,但我得到的只是一个NoSuchBeanDefinition异常 因此,我仔细查看了ctx-*.xml文件。遗憾的是,我什么也没找到 提前thx 测试准备: 例外情况: 附加文件 实体类: 存储库: package de.backend.repositories; 导入java.util.List; 导入org.springframework.data.jpa.repository.JpaRepository
附加文件 实体类: 存储库:
package de.backend.repositories;
导入java.util.List;
导入org.springframework.data.jpa.repository.JpaRepository;
导入org.springframework.stereotype.Repository;
导入de.backend.entities.och条目;
@存储库
公共接口OCHEntryRepo扩展了JpaRepository{
列出FindAllowerByCreated();
列表FindBytletleLike(字符串搜索条件);
列表findByLinkLike(字符串搜索条件);
列出findAllBySource(字符串源);
}
服务接口:
package de.backend.services.interfaces;
导入java.util.List;
导入de.backend.entities.och条目;
公共接口OCHEntryService{
列出FindAllowerByCreated();
列出findAllBySource(字符串源);
列表FindBytletleLike(字符串搜索条件);
列表findByLinkLike(字符串搜索条件);
列出findAll();
作废保存(OCHEntry OCHEntry);
}
服务实施:
package de.backend.services;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入de.backend.entities.och条目;
导入de.backend.repositories.OCHEntryRepo;
导入de.backend.services.interfaces.OCHEntryService;
@服务
公共类OCHEntryServiceImpl实现OCHEntryService{
@自动连线
私人OCHEntryRepo OCHEntryRepo;
公共列表FindAllowerByCreated(){
返回此.ochEntryRepo.findAllOrderByCreated();
}
公共列表FindBytletleLike(字符串搜索条件){
返回this.ochEntryRepo.findbytilelike(searchCriteria);
}
公共列表findByLinkLike(字符串搜索条件){
返回这个.ochentrypo.findByLinkLike(searchCriteria);
}
公共列表findAll(){
返回这个.ochEntryRepo.findAll();
}
公共作废保存(ochcentry ochcentry){
this.ochentrypo.save(ochEntry条目);
}
@凌驾
公共列表findAllBySource(字符串源){
返回此.ochEntryRepo.findAllBySource(source);
}
}
ctx后端:
ctx数据源:
pom.xml-依赖项:
org.hibernate
休眠实体管理器
javax.validation
验证api
org.hibernate
休眠验证器
朱尼特
朱尼特
测验
com.h2数据库
氢
com.zaxxer
希卡里普
mysql
mysql连接器java
org.hibernate
休眠实体管理器
org.springframework.data
spring数据jpa
org.springframework
春季甲虫
org.springframework
弹簧试验
log4j
log4j
de.dirrot.dev.parsers
门户解析器
0.0.1-快照
org.slf4j
slf4j api
org.slf4j
slf4j-log4j12
解决方案:
主要问题是,“按方法名查询”语法的声明不起作用。所以我改变了一些方法。现在它开始工作了
ctx后端:
及
OCHEntryRepo:
@存储库
公共接口OCHEntryRepo扩展了JpaRepository{
列表findAll(排序);
列表FindBytleteContaining(字符串搜索条件);
列表findByLinkContaining(字符串搜索条件);
列出findBySource(字符串源);
}
非常感谢所有相关人员 请添加
<jpa:repositories base-package="de.backend.repositories" />
在
<context:compnent-scan>.
。
请重命名
List<OCHEntry> findAllBySource(String source);
列出findAllBySource(字符串源);
进入
列出findBySource(字符串源);
及
List findAllOrderByCreated();
进入
List findAllOrderByCreatedDesc();
对不起,我弄错了。你能试试带有jpa:前缀的存储库吗?如果这对存储库没有帮助?根据spring文档,它应该是“”名称空间,在您的设置中有jpa:前缀。仍然得到相同的异常。我还查看了文档。你看到存储库扫描了吗?配置log4j进行调试后,它会产生相当多的输出。我添加了日志输出,问题在于存储库中的一个(或多个)方法。你能试着注释掉一个接一个的方法,直到上下文能够成功启动吗?
package de.backend.repositories;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import de.backend.entities.OCHEntry;
@Repository
public interface OCHEntryRepo extends JpaRepository<OCHEntry, Long> {
List<OCHEntry> findAllOrderByCreated();
List<OCHEntry> findByTitleLike(String searchCriteria);
List<OCHEntry> findByLinkLike(String searchCriteria);
List<OCHEntry> findAllBySource(String source);
}
package de.backend.services.interfaces;
import java.util.List;
import de.backend.entities.OCHEntry;
public interface OCHEntryService {
List<OCHEntry> findAllOrderByCreated();
List<OCHEntry> findAllBySource(String source);
List<OCHEntry> findByTitleLike(String searchCriteria);
List<OCHEntry> findByLinkLike(String searchCriteria);
List<OCHEntry> findAll();
void save(OCHEntry ochEntry);
}
package de.backend.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import de.backend.entities.OCHEntry;
import de.backend.repositories.OCHEntryRepo;
import de.backend.services.interfaces.OCHEntryService;
@Service
public class OCHEntryServiceImpl implements OCHEntryService {
@Autowired
private OCHEntryRepo ochEntryRepo;
public List<OCHEntry> findAllOrderByCreated() {
return this.ochEntryRepo.findAllOrderByCreated();
}
public List<OCHEntry> findByTitleLike(String searchCriteria) {
return this.ochEntryRepo.findByTitleLike(searchCriteria);
}
public List<OCHEntry> findByLinkLike(String searchCriteria) {
return this.ochEntryRepo.findByLinkLike(searchCriteria);
}
public List<OCHEntry> findAll() {
return this.ochEntryRepo.findAll();
}
public void save(OCHEntry ochEntry) {
this.ochEntryRepo.save(ochEntry);
}
@Override
public List<OCHEntry> findAllBySource(String source) {
return this.ochEntryRepo.findAllBySource(source);
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:annotation-config />
<context:component-scan base-package="de.backend.*" />
<!-- Import datasource -->
<import resource="classpath:META-INF/spring/ctx-datasource.xml" />
</beans>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<alias name="jpaVendorAdapterMySql" alias="jpaVendorAdapter" />
<bean id="jpaVendorAdapterMySql"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
<property name="generateDdl" value="true" />
<property name="showSql" value="false" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
<property name="driverClassName" value="${mysql.driverClassName}" />
<property name="url" value="${mysql.url}" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan" value="de.backend.entities.*" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="de.backend.repositories.*" />
<bean id="datasourcePropertyConfigurer"
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="datasource.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean name="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
</beans>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- Database (H2) -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- DataSource (HikariCP) -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- JPA Provider (Hibernate) -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>de.dirrot.dev.parsers</groupId>
<artifactId>portal-parsers</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- Import datasource -->
<import resource="classpath:META-INF/spring/ctx-datasource.xml" />
<context:annotation-config />
<jpa:repositories base-package="de.backend.epositories" />
<context:component-scan base-package="de.backend.services" />
</beans>
@Repository
public interface OCHEntryRepo extends JpaRepository<OCHEntry, Long> {
List<OCHEntry> findAll(Sort sort);
List<OCHEntry> findByTitleContaining(String searchCriteria);
List<OCHEntry> findByLinkContaining(String searchCriteria);
List<OCHEntry> findBySource(String source);
}
<jpa:repositories base-package="de.backend.repositories" />
<context:compnent-scan>.
List<OCHEntry> findAllBySource(String source);
List<OCHEntry> findBySource(String source);
List<OCHEntry> findAllOrderByCreated();
List<OCHEntry> findAllOrderByCreatedDesc();