我是Spring的新手,我想使用spring boot构建一个简单的注册表单,并使用jpa将用户信息存储在mysql数据库中。
这是我的错误堆栈。
----------
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)
2017-06-05 12:34:53.392 INFO 9128 --- [ main] com.mohit.LoanapimainApplication : Starting LoanapimainApplication on MOHIT with PID 9128 (C:\Users\admin\Documents\workspace-sts-3.8.4.RELEASE\loanapimain\target\classes started by admin in C:\Users\admin\Documents\workspace-sts-3.8.4.RELEASE\loanapimain)
2017-06-05 12:34:53.407 INFO 9128 --- [ main] com.mohit.LoanapimainApplication : No active profile set, falling back to default profiles: default
2017-06-05 12:34:53.815 INFO 9128 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@396c1e: startup date [Mon Jun 05 12:34:53 IST 2017]; root of context hierarchy
2017-06-05 12:34:55.754 INFO 9128 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-05 12:34:55.772 INFO 9128 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-06-05 12:34:55.772 INFO 9128 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14
2017-06-05 12:34:56.006 INFO 9128 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-06-05 12:34:56.006 INFO 9128 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2207 ms
2017-06-05 12:34:56.163 INFO 9128 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-05 12:34:56.163 INFO 9128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-05 12:34:56.163 INFO 9128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-05 12:34:56.163 INFO 9128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-05 12:34:56.163 INFO 9128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-05 12:34:56.756 INFO 9128 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-06-05 12:34:56.771 INFO 9128 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-06-05 12:34:56.881 INFO 9128 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
2017-06-05 12:34:56.881 INFO 9128 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-06-05 12:34:56.881 INFO 9128 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-06-05 12:34:56.927 INFO 9128 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-06-05 12:34:57.052 INFO 9128 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-06-05 12:34:57.615 INFO 9128 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-06-05 12:34:57.631 WARN 9128 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'user'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mohit.beans.User' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-06-05 12:34:57.631 INFO 9128 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-06-05 12:34:57.631 INFO 9128 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat
2017-06-05 12:34:57.646 INFO 9128 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-05 12:34:57.740 ERROR 9128 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field user in com.mohit.Controllers.UserController required a bean of type 'com.mohit.beans.User' that could not be found.
动作:
考虑定义一个类型为' com.mohit.beans.User'的bean。在你的配置中。
位指示`
package com.mohit.Controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.mohit.beans.User;
import com.mohit.Services.createuser;
@Controller
public class UserController {
@Autowired(required=true)
private User user;
@Autowired(required=true)
private createuser create;
@RequestMapping("/")
public String sayhi(){
return "index";
}
@RequestMapping("/index")
public String Createuser(@RequestParam(value="uname") String name,@RequestParam(value="pass") String pass, Model model){
user.setUname(name);
user.setPass(pass);
create.adduser(user);
return "result";
}
public String AuthUser(@RequestParam(value="name") String name,@RequestParam(value="pass") String pass, Model model){
return "mainpage";
}
}
----------
--------------
----------
package com.mohit.beans;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String uname;
private String pass;
public User(String uname, String pass) {
super();
this.uname = uname;
this.pass = pass;
}
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return "User [id=" + id + ", uname=" + uname + ", pass=" + pass + "]";
}
}
----------
----------
package com.mohit.Services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mohit.Repo.userrepo;
import com.mohit.beans.User;
@Service
public class createuser {
@Autowired
private userrepo Userrepo;
public void adduser(User user)
{
Userrepo.save(user);
}
}
----------
--------------
----------
package com.mohit.Repo;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.mohit.beans.User;
public interface userrepo extends PagingAndSortingRepository<User,String> {
}
----------
----------
?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mohit</groupId>
<artifactId>loanapimain</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>loanapimain</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
----------
spring.datasource.url=jdbc:mysql://localhost/project
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
----------
package com.mohit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LoanapimainApplication {
public static void main(String[] args) {
SpringApplication.run(LoanapimainApplication.class, args);
}
}
答案 0 :(得分:5)
User
不是bean而是实体 - 简单的POJO类。它不能自动装配。
将逻辑更改为
@RequestMapping("/index")
public String Createuser(@RequestParam(value="uname") String name,@RequestParam(value="pass") String pass, Model model){
User user = new User();
user.setUname(name);
user.setPass(pass);
create.adduser(user);
return "result";
}
并从控制器中删除用户字段