创建名称为“flightController”的 bean 时出错:通过字段“flightrepos”表示的依赖关系不满足;

Posted

技术标签:

【中文标题】创建名称为“flightController”的 bean 时出错:通过字段“flightrepos”表示的依赖关系不满足;【英文标题】:Error creating bean with name 'flightController': Unsatisfied dependency expressed through field 'flightrepos'; 【发布时间】:2019-02-22 03:44:14 【问题描述】:

我在第一个代码中创建了 2 个单独的代码,我的代码运行良好,而在其他代码中我面临上述问题。我将分享这两个代码,请帮助我,因为我是今年春天的新手并遵循一些教程。

代码 1


控制器

@Controller
public class UserController 

@Autowired
private UserRepository userRepos;

@RequestMapping("/showReg")
public String showRegistrationPage() 
    return "registerUser";


@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@RequestParam("email")String email, @RequestParam("password")String password, ModelMap modelMap) 
    User user = userRepos.findByEmail(email);
    if(user.getPassword().equals(password)) 
        return "findflights";
    
    else 
        modelMap.addAttribute("msg", "Invalid try again");
    

    return "login";

用户 bean 类

@Entity
public class User  

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
private String email;
private String password;

//getters and setters

存储库类

public interface UserRepository extends JpaRepository<User, Long> 

@Query(value = "select * from user u where u.email = :email", nativeQuery = true)
public User findByEmail(@Param("email") String email);


代码 2


控制器

@Controller
public class FlightController 

@Autowired
private FlightRepository flightrepos;

@RequestMapping("/findFlights")
public String findFlights(@RequestParam("from")String from, @RequestParam("to")String to,@RequestParam("departureDate") @DateTimeFormat(pattern ="MM-dd-yyyy")Date departureDate, ModelMap modelMap ) 
     List<Flight> flights = flightrepos.findFlights(from, to, departureDate);
    modelMap.addAttribute("flights", flights);
    return "displayFlights";


飞行豆类

@Entity
public class Flight 

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String flightNumber;
private String operatingAirlines;
private String departureCity;
private String arrivalCity;
private Date dateOfDeparture;
private Timestamp estimatedDepartureTime;
//getters and setters

存储库

public interface FlightRepository extends JpaRepository<Flight, Long> 

@Query( value ="select * from Flight f where f.departurecity =:departureCity and f.arrivalCity =:arrivalCity and f.dateOfDeparture =:dateOfDeparture ", nativeQuery = true)
List<Flight> findFlights(@Param("departurecity")String from, @Param("arrivalCity")String to, @Param("dateOfDeparture")Date departureDate);

我检查了所有内容,但现在我遇到了错误


or creating bean with name 'flightController': Unsatisfied dependency expressed through field 'flightrepos'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flightRepository': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract java.util.List com.project.test.repos.FlightRepository.findFlights(java.lang.String,java.lang.String,java.util.Date) but parameter 'Optional[departurecity]' not found in annotated query 'select * from Flight f where f.departurecity =:departureCity and f.arrivalCity =:arrivalCity and f.dateOfDeparture =:dateOfDeparture '!

【问题讨论】:

这里的驼峰拼写错误:将离开城市更改为离开城市 【参考方案1】:

您的 @ParamfindFlights() 方法中有一个错字。

改变,

@Param("departurecity")String from

到,

@Param("departureCity")String from

【讨论】:

【参考方案2】:

...参数 'Optional[departurecity]' 在带注释的查询中找不到...

f.departureCity,不是f.departurecity@Param("departureCity"),不是@Param("departurecity")

【讨论】:

【参考方案3】:

我猜同样的tuto也有同样的问题:p juste change @Param("departurecity") to @Param("departureCity")

【讨论】:

以上是关于创建名称为“flightController”的 bean 时出错:通过字段“flightrepos”表示的依赖关系不满足;的主要内容,如果未能解决你的问题,请参考以下文章

在类路径资源中定义名称为“entityManagerFactory”的 bean 创建错误

WebLogic 10.3.7 文件上传返回“创建名称为 'portletMultipartResolver' 的 bean 时出错”

创建名称为“*repository”的 bean 时出错

Spring MVC:创建 ServletContext 资源中定义的名称为“HandlerMapping”的 bean 时出错

启动程序时,在类路径资源中定义名称为 'entityManagerFactory' 的 bean 创建时出错

BeanCreationException:创建名称为“flywayInitializer”的 bean 时出错