简体   繁体   English

Spring 启动 web java 配置不起作用

[英]Spring boot web java configuration not working

I'm just starting off with Spring, I have created a basic Spring boot web app with - Thymeleaf templates, JPA and Mysql. I'm just starting off with Spring, I have created a basic Spring boot web app with - Thymeleaf templates, JPA and Mysql.

When I use the autowiring and annotate all the classes everything works.当我使用自动装配并注释所有类时,一切正常。 Here is an example of the working code.这是工作代码的示例。

Simple product class with the @Component annotation带有@Component 注释的简单产品 class
带有@Component 注解的简单产品类

Simple DAO class with the @service annotation带有 @service 注释的简单 DAO class
带有 @service 注释的简单 DAO 类

Basic controller with the @Autowired annotation to get DAO instance基本 controller 带有 @Autowired 注释以获取 DAO 实例
带有@Autowired注解的基本控制器来获取DAO实例

So when I call the get method, I'm able to see a list of products: 2 products displayed因此,当我调用 get 方法时,我可以看到产品列表:显示 2 个产品
2个产品展示

Now when I transition to the java configuration, this stops working.现在,当我转换到 java 配置时,它停止工作。 I commented out the annotations and added a new java config file.我注释掉了注释并添加了一个新的 java 配置文件。

the @Autowiring annotation also removed from the Controller @Autowiring 注释也从 Controller 中删除

Added a java class with the @Configuration, see config file:添加了带有 @Configuration 的 java class,请参阅配置文件:
配置文件

Just to make sure the context loaded the beans, I took the configurable context that was returned by the run method and checked if the bean was loaded.为了确保上下文加载了 bean,我采用了 run 方法返回的可配置上下文并检查了 bean 是否已加载。

Get the DAO bean and call the method to get the Product line items获取DAO bean并调用获取Product line items的方法
获取DAO bean并调用获取Product line items的方法

This also works as I could see the object was not null.这也有效,因为我可以看到 object 不是 null。 Also I could get a count of the products in the output immediately after the application was started.我还可以在应用程序启动后立即获得 output 中的产品数量。

Everything is ok till here and the application has started.直到这里一切都很好,应用程序已经启动。 But now when I access the URL - I get an error但是现在当我访问 URL - 我得到一个错误

Summary This example is only being done to understand Spring and is not following proper DDD practices.总结这个例子只是为了理解 Spring 而不是遵循正确的 DDD 实践。

Here is a summary of the steps, the issues are in step 4(b)这是步骤的摘要,问题在步骤 4(b)

  1. DAO creates 2 Product instances, puts into a list and returns it. DAO 创建 2 个 Product 实例,放入一个列表并返回它。
  2. The DAO is injected into the controller. DAO 被注入到 controller 中。 The controller calls the getproducts() on the DAO and returns the list of products. controller 调用 DAO 上的 getproducts() 并返回产品列表。
  3. Everything works with the component and service Annotations combined with @Autowiring一切都与组件和服务注释结合使用@Autowireing
  4. Problem comes when I use a Java configuration.当我使用 Java 配置时,问题就来了。 (a) After application startup I get the context and verify if the beans are loaded. (a) 应用程序启动后,我获取上下文并验证 bean 是否已加载。 (I'm doing this in the main application.) (b) Only when I hit the url and when the controller tries to call the method on the DAO I get the problem. (我在主应用程序中执行此操作。) (b) 只有当我点击 url 并且 controller 尝试调用 DAO 上的方法时,我才会遇到问题。

Declare a constructor for HomeController as followsHomeController声明一个构造函数,如下所示

@Controller
public class HomeController{

   private final ProductDao dao;

    public HomeController(ProductDao dao){
       this.dao = dao;
    }
 
  //... rest of the code
}

Reference: Spring Boot Reference Documentation - Spring Beans and Dependency Injection参考:Spring 引导参考文档 - Spring Beans 和依赖注入

If a bean has one constructor, you can omit the @Autowired如果一个 bean 有一个构造函数,你可以省略 @Autowired

Note: Share actual code instead of images for reference.注意:共享实际代码而不是图像以供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM