Java Intellij Idea - 无法自动装配。未找到“ApplicationRepository”类型的 bean。少... (Ctrl+F1) 检查 bean 类中的自动装配问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16241946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intellij Idea - Could not autowire. No beans of 'ApplicationRepository' type found. less... (Ctrl+F1) Checks autowiring problems in a bean class
提问by nothing-special-here
I am Java newbie.
我是Java新手。
I am making this tutorial(Spring MVC + Hibernate + Tomcat)
我正在制作本教程(Spring MVC + Hibernate + Tomcat)
Everything goes fine till now. When I tried to make a ApplicationController
I could not autowire ApplicationRepository
.
到目前为止一切都很好。当我尝试制作 a 时,ApplicationController
我无法自动装配ApplicationRepository
。
I get this error message and 404
error code when I deploy application:
404
部署应用程序时,我收到此错误消息和错误代码:
Could not autowire. No beans of 'ApplicationRepository' type found. less...
(Ctrl+F1)
Checks autowiring problems in a bean class.
Ok talk is cheap.
好的谈话是便宜的。
Here goes the repo(github!): https://github.com/maciejkowalski/sample-spring-app
这是回购(github!):https: //github.com/maciejkowalski/sample-spring-app
Tomcat stacktrace: https://gist.github.com/maciejkowalski/c7512d82feb75fcebd5f
Tomcat堆栈跟踪:https: //gist.github.com/maciejkowalski/c7512d82feb75fcebd5f
Here is the code:
这是代码:
root/src/main/java/wzpweb/
根/src/main/java/wzpweb/
package wzpweb;
import javax.persistence.*;
@Entity(name = "applications")
public class Application {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Basic
private String name;
@Basic
private String ip;
@Basic
private Boolean alive;
@Basic
private Integer port;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Boolean getAlive() {
return alive;
}
public void setAlive(Boolean alive) {
this.alive = alive;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
package wzpweb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class ApplicationController {
@Autowired
private ApplicationRepository applicationRepository;
// THIS IS NOT WORKING !! ACHTUNG!
@RequestMapping(value = "/", method = RequestMethod.GET)
public String listApplications(ModelMap model) {
model.addAttribute("application", new Application());
model.addAttribute("applications", applicationRepository.findAll());
return "users";
}
}
package wzpweb;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ApplicationRepository extends JpaRepository<Application, Long> {
}
root/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml
root/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml
<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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="wzpweb"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<jpa:repositories base-package="wzpweb"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="defaultPersistenceUnit"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
root/src/main/webapp/WEB-INF/web.xml
root/src/main/webapp/WEB-INF/web.xml
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
root/src/main/resources/META-INF/persistance.xml
root/src/main/resources/META-INF/persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost/sample"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="123" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
root/pom.xml
根/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springapp</groupId>
<artifactId>WZP_aplikacja_web</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>WZP_aplikacja_web</name>
<properties>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
</dependencies>
<build>
<finalName>WZP_aplikacja_web</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
EDIT #1
编辑#1
I have edited web.xml
and added code:
我已经编辑web.xml
并添加了代码:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
But now I get Element listener-class is not allowed her
from Intellij IDEA.
但现在我Element listener-class is not allowed her
从 Intellij IDEA得到 了。
And my app even doesnt deploy.
我的应用程序甚至没有部署。
EDIT #2
编辑#2
Well problem still occurs.
那么问题仍然存在。
I renamed persistance.xml
to persistence.xml
.
我改名persistance.xml
为persistence.xml
.
Restarted Intellij idea and still getting this error.
重新启动 Intellij 想法,但仍然出现此错误。
Maybe this is a bug?
也许这是一个错误?
I will probably surrender with this Spring stuff, it's beyond me...
我可能会用这个春天的东西投降,它超出了我的范围......
回答by duffymo
Add a context loader listener to your web.xml to read the Spring app context XML on startup.
将上下文加载器侦听器添加到您的 web.xml 以在启动时读取 Spring 应用程序上下文 XML。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/applicationContext.xml,/WEB-INF/context/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
回答by Sotirios Delimanolis
Rename your file persistance.xml
to persistence.xml
. The persistence.xml
is, by default, the name of the file needed in the META-INF/
directory to define a persistence unit needed by your entityManagerFactory.
将您的文件重命名persistance.xml
为persistence.xml
. 的persistence.xml
是,默认情况下,在所需要的文件的名称META-INF/
目录来定义你的entityManagerFactory需要一个持久性单元。
Here's a blog post explaining how Spring uses your custom interface implementing JpaRepository
to generate an implementation class. I originally thought that you needed an implementation class for it, but that is notthe case.
这是一篇博客文章,解释了 Spring 如何使用您的自定义接口实现JpaRepository
来生成实现类。我最初认为您需要一个实现类,但事实并非如此。
Small bug in the naming is your problem, given away by your first exception in the stack trace.
命名中的小错误是您的问题,由堆栈跟踪中的第一个异常给出。
javax.persistence.PersistenceException: No Persistence provider for EntityManager named defaultPersistenceUnit
回答by Nicolas C
I followed the same tutorial and ran into the same code inspection warning (even if the application was working fine, the IDE was complaining). To make fix it, I added @Repository
to my JpaRepository:
我遵循了相同的教程并遇到了相同的代码检查警告(即使应用程序运行良好,IDE 也在抱怨)。为了修复它,我添加@Repository
到我的 JpaRepository:
For your example:
对于您的示例:
package wzpweb;
import org.springframework.data.jpa.repository.JpaRepository;
@Repository
public interface ApplicationRepository extends JpaRepository<Application, Long> {
}
回答by kkkkill
removing the Spring facet (File->Project Structure) Kill the project configuration in the facet of the relevant module configuration content, the IDEA is automatically identified. After the removal, the relevant error disappears. Does not affect compilation.
去掉Spring facet(File->Project Structure)杀掉相关模块配置内容的facet中的项目配置,IDEA自动识别。删除后,相关错误消失。不影响编译。
回答by Vaibs
Similar issue come when you have created ObjectServiceand instantiated the same in the RestControllerand you havent annotated the ObjectServiceImplwith @Service.
当您创建ObjectService并在RestController 中实例化相同的对象并且您还没有使用@Service注释ObjectServiceImpl时,会出现类似的问题。