我是
Java新手.
@H_404_2@我正在制作这个tutorial(Spring MVC Hibernate Tomcat)
@H_404_2@到目前为止,一切都很好.当我尝试创建ApplicationController时,我无法自动装配ApplicationRepository.
@H_404_2@部署应用程序时,我收到此错误消息和404错误代码:
Could not autowire. No beans of 'ApplicationRepository' type found. less... (Ctrl+F1) Checks autowiring problems in a bean class.@H_404_2@好的谈话很便宜. @H_404_2@这里是回购(github!):https://github.com/maciejkowalski/sample-spring-app @H_404_2@Tomcat stacktrace:https://gist.github.com/maciejkowalski/c7512d82feb75fcebd5f @H_404_2@这是代码: @H_404_2@根/ 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> { }@H_404_2@根/ 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>@H_404_2@根/ 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>@H_404_2@根/ 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>@H_404_2@根/ 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>@H_404_2@编辑#1 @H_404_2@我编辑了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>@H_404_2@但是现在我得到了Element listener-class,不允许她来自Intellij IDEA. @H_404_2@我的应用程序甚至没有部署. @H_404_2@编辑#2 @H_404_2@问题仍然存在. @H_404_2@我将persistance.xml重命名为persistence.xml. @H_404_2@重新启动Intellij的想法,仍然收到此错误. @H_404_2@也许这是一个错误? @H_404_2@我可能会投降这个春天的东西,它超出了我……