spring boot 例子报错,高手请进

litsnake 2017-04-17 08:45:21
小弟是新手,想想试试spring boot +JPA +mysql,网上找了几个例子,写了个例子,但是跑不通,报错了,各位大虾帮看看,哪里出问题了,看了许久木有找出问题来。先谢过了!

报错信息:

:: Spring Boot :: (v1.5.2.RELEASE)

2017-04-17 20:35:07.575 INFO 5464 --- [ main] com.example.Application : Starting Application on th-PC with PID 5464 (D:\th\springboot\demo\target\classes started by th in D:\th\springboot\demo)
2017-04-17 20:35:07.884 INFO 5464 --- [ main] com.example.Application : No active profile set, falling back to default profiles: default
2017-04-17 20:35:08.640 INFO 5464 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7cb502c: startup date [Mon Apr 17 20:35:08 CST 2017]; root of context hierarchy
2017-04-17 20:35:11.046 INFO 5464 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$17bab5b6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-04-17 20:35:11.944 INFO 5464 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-04-17 20:35:12.008 INFO 5464 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-04-17 20:35:12.016 INFO 5464 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-04-17 20:35:12.349 INFO 5464 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-04-17 20:35:12.350 INFO 5464 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3717 ms
2017-04-17 20:35:12.848 INFO 5464 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-04-17 20:35:12.860 INFO 5464 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-04-17 20:35:12.861 INFO 5464 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-04-17 20:35:12.862 INFO 5464 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-04-17 20:35:12.862 INFO 5464 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-04-17 20:35:13.024 WARN 5464 --- [ 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 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userJpaDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.dao.UserJpaDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-04-17 20:35:13.034 INFO 5464 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat
2017-04-17 20:35:13.100 INFO 5464 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-04-17 20:35:13.497 ERROR 5464 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userJpaDao in com.example.base.service.UserServiceImpl required a bean of type 'com.example.dao.UserJpaDao' that could not be found.


Action:

Consider defining a bean of type 'com.example.dao.UserJpaDao' in your configuration.



package com.example.dao;

import org.springframework.stereotype.Component;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.example.User;

@Component
public interface UserJpaDao extends JpaRepository<User,Long>{

/**
* Find by name.
*
* @param name the name
* @return the user
*/
User findByName(String name);

/**
* Find user.
* User为@Entity 的名字
* @param name the name
* @return the user
*/
@Query("from UserInfo u where u.name=:name")
User findUser(@Param("name") String name);
}




package com.example.base.service;

import org.springframework.beans.factory.annotation.Autowired;
import com.example.User;
import com.example.dao.UserJpaDao;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserJpaDao userJpaDao;


@Override
public User getUserByName(String username) {
// TODO Auto-generated method stub
return userJpaDao.findByName(username);

}

}



package com.example.base.service;

import com.example.User;

public interface UserService {
/**
* Gets the user by name.
*
* @param username the user name
* @return the user by name
*/
public User getUserByName(String username);
}



package com.example;

import org.springframework.boot.orm.jpa.*;
import org.springframework.boot.orm.jpa.hibernate.*;

import javax.persistence.*;

@Entity
@Table(name="userinfo")
public class User {

@Id
@GeneratedValue
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}


}



package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args)
{
SpringApplication.run(Application.class);
}
}

...全文
465 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
码之道 2018-03-15
  • 打赏
  • 举报
回复
很有帮助的代码实例,测试Spring Boot REST API要写很多的UT代码,很繁琐,还好找到一个很适合测试REST API工具: Wisdom RESTClient,支持自动化测试,生成REST API文档。 https://github.com/Wisdom-Projects/rest-client 感谢作者的分享,赞一个!
litsnake 2017-04-18
  • 打赏
  • 举报
回复
还灭有解决呢,请继续!
litsnake 2017-04-17
  • 打赏
  • 举报
回复
引用 3 楼 pany1209 的回复:
[quote=引用 2 楼 litsnake 的回复:] [quote=引用 1 楼 pany1209 的回复:] Field userJpaDao in com.example.base.service.UserServiceImpl required a bean of type 'com.example.dao.UserJpaDao' that could。。。。。不能用@Component给接口注释,写一个UserJpaDao的实现类。。。用@Component注解。。再注入 @Autowired private UserJpaDao userJpaDao;
还需要接口userJpaDao的 实现类?但是为啥我看到一段这样的描述“在Spring-data-jpa中,只需要编写类似上面这样的接口就可实现数据访问。不再像我们以往编写了接口时候还需要自己编写接口实现类,直接减少了我们的文件清单。” 不是说不需要实现JpaRepository,就可以完成CURD吗?[/quote] 不好意思。。搞错了。。在启动类加上注解@SpringBootApplication试试[/quote]

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
	public static void main(String[] args)
	{
		SpringApplication.run(Application.class);
	}
}

加了,@SpringBootApplication,还是一样,同样的报错,而且我查过了@SpringBootApplication等效于@Configuration @ComponentScan @EnableAutoConfiguration 所以应该不是这个问题
李德胜1995 2017-04-17
  • 打赏
  • 举报
回复
引用 2 楼 litsnake 的回复:
[quote=引用 1 楼 pany1209 的回复:] Field userJpaDao in com.example.base.service.UserServiceImpl required a bean of type 'com.example.dao.UserJpaDao' that could。。。。。不能用@Component给接口注释,写一个UserJpaDao的实现类。。。用@Component注解。。再注入 @Autowired private UserJpaDao userJpaDao;
还需要接口userJpaDao的 实现类?但是为啥我看到一段这样的描述“在Spring-data-jpa中,只需要编写类似上面这样的接口就可实现数据访问。不再像我们以往编写了接口时候还需要自己编写接口实现类,直接减少了我们的文件清单。” 不是说不需要实现JpaRepository,就可以完成CURD吗?[/quote] 不好意思。。搞错了。。在启动类加上注解@SpringBootApplication试试
litsnake 2017-04-17
  • 打赏
  • 举报
回复
引用 1 楼 pany1209 的回复:
Field userJpaDao in com.example.base.service.UserServiceImpl required a bean of type 'com.example.dao.UserJpaDao' that could。。。。。不能用@Component给接口注释,写一个UserJpaDao的实现类。。。用@Component注解。。再注入 @Autowired private UserJpaDao userJpaDao;
还需要接口userJpaDao的 实现类?但是为啥我看到一段这样的描述“在Spring-data-jpa中,只需要编写类似上面这样的接口就可实现数据访问。不再像我们以往编写了接口时候还需要自己编写接口实现类,直接减少了我们的文件清单。” 不是说不需要实现JpaRepository,就可以完成CURD吗?
李德胜1995 2017-04-17
  • 打赏
  • 举报
回复
Field userJpaDao in com.example.base.service.UserServiceImpl required a bean of type 'com.example.dao.UserJpaDao' that could。。。。。不能用@Component给接口注释,写一个UserJpaDao的实现类。。。用@Component注解。。再注入 @Autowired private UserJpaDao userJpaDao;

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧