我是Java新手。
我正在制作这个tutorial(Spring MVC + Hibernate + Tomcat)
到目前为止,一切都很顺利。当我尝试制作ApplicationController
时,我无法自动发送ApplicationRepository
。
部署应用程序时收到此错误消息和404
错误代码:
Could not autowire. No beans of 'ApplicationRepository' type found. less...
(Ctrl+F1)
Checks autowiring problems in a bean class.
好的谈话很便宜。
这里是 repo (github!):https://github.com/maciejkowalski/sample-spring-app
Tomcat stacktrace :https://gist.github.com/maciejkowalski/c7512d82feb75fcebd5f
以下是代码:
根/ SRC /主/爪哇/ 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> {
}
根/ SRC /主/ web应用/ WEB-INF / MVC-调度-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>
根/ SRC /主/ web应用/ 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>
根/ SRC /主/资源/ 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>
根/ 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>
编辑#1
我已编辑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>
但现在我从Intellij IDEA获得Element listener-class is not allowed her
。
我的应用甚至没有部署。
编辑#2
问题仍然存在。
我将persistance.xml
重命名为persistence.xml
。
重新启动了Intellij的想法并仍然收到此错误。
也许这是一个错误?
我可能会对这些春天的东西投降,这超出了我的范围......
答案 0 :(得分:13)
我遵循相同的教程并遇到了相同的代码检查警告(即使应用程序工作正常,IDE也在抱怨)。为了解决这个问题,我将@Repository
添加到了我的JpaRepository:
对于你的例子:
package wzpweb;
import org.springframework.data.jpa.repository.JpaRepository;
@Repository
public interface ApplicationRepository extends JpaRepository<Application, Long> {
}
答案 1 :(得分:2)
将文件persistance.xml
重命名为persistence.xml
。默认情况下,persistence.xml
是META-INF/
目录中所需文件的名称,用于定义entityManagerFactory所需的持久性单元。
Here是一篇博文,解释了Spring如何使用实现JpaRepository
的自定义接口来生成实现类。我原本以为你需要一个实现类,但不的情况。
命名中的小错误是你的问题,由堆栈跟踪中的第一个异常给出。
javax.persistence.PersistenceException: No Persistence provider for EntityManager named defaultPersistenceUnit
答案 2 :(得分:1)
将上下文加载器侦听器添加到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>
答案 3 :(得分:1)
删除Spring facet(File-&gt; Project Structure) 将项目配置杀死在相关模块配置内容的方面,将自动识别IDEA。删除后,相关错误消失。不影响编译。
答案 4 :(得分:0)
类似的问题出现在您创建 ObjectService 并在 RestController 中实例化了同样的问题,而您又没有用 @注释 ObjectServiceImpl 时服务。