简体   繁体   English

Spring boot - 没有@component 注解的bean配置

[英]Spring boot - beans configuration without @component annotation

As part of my university project I was asked to implement a simple spring-boot app(backend only) which can communicate with Postman through HTTP requests.作为我大学项目的一部分,我被要求实现一个简单的 spring-boot 应用程序(仅限后端),它可以通过 HTTP 请求与 Postman 通信。

The project built in controller-service-repository architecture and conatins only 1 Entity(Post object with string content) and 2 end-points(create new post, get all posts).该项目内置控制器-服务-存储库架构,仅包含 1 个实体(带有字符串内容的 Post 对象)和 2 个端点(创建新帖子,获取所有帖子)。

I know there is a few ways to configure beans in spring-boot:我知道有几种方法可以在 spring-boot 中配置 bean:

  1. with an external XML file.使用外部 XML 文件。
  2. With @Configuration annotation & @Bean annotation使用@Configuration 注解和@Bean 注解
  3. With @Component annotation(@RestController,@Service, @JpaRepository)带有@Component 注解(@RestController,@Service, @JpaRepository)

The 3rd way working great but i was asked to implement the 2nd way and I'm really struggling to get this working.第 3 种方式工作得很好,但我被要求实施第二种方式,我真的很难让它发挥作用。

Im getting:我越来越:

ServletException: Circular view path [post]: would dispatch back to the current handler URL [/post] again. ServletException: Circular view path [post]: 将再次分派回当前处理程序 URL [/post]。 Check your ViewResolver setup!检查您的 ViewResolver 设置!

Tried to explore about this exception and i did manage to "solve" it by adding this maven dependency:试图探索这个异常,我确实通过添加这个 maven 依赖来“解决”它:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.5.2</version>
</dependency>

which led to:这导致:

"org.thymeleaf.exceptions.TemplateInputException: Error resolving template [post], template might not exist or might not be accessible by any of the configured Template Resolvers"

what am i doing wrong ?我究竟做错了什么 ?

Configuration class:配置类:

@Configuration
@EnableJpaRepositories(basePackages = {
        "com.example.microblog.post.domain.repository"
})
public class ApplicationBeans {

    @Bean
    public PostController postController(PostService postService){
        return new PostController(postService);
    }

    @Bean
    public PostService postService(){
        return new PostService();

    }

}

Controller class:控制器类:

@AllArgsConstructor
@RequestMapping(path = "post")
public class PostController {

    @Autowired
    private PostService service;

    @CrossOrigin(origins = "http://localhost:4200")
    @PostMapping("")
    public PostEntity create(@RequestBody PostDto dto) {
        return service.create(dto);
    }

    @GetMapping("/all")
    @CrossOrigin(origins = "http://localhost:4200")
    public List<PostEntity> getAll() {
        return service.getAll();
    }

}

Service Class:服务等级:

@Transactional
public class PostService {

    @Autowired
    private PostRepository PostRepository;


    public PostEntity create(PostDto dto){
        PostEntity newPost = new PostEntity(dto.getContent());
        return PostRepository.save(newPost);
    }

    public List<PostEntity> getAll(){
        return PostRepository.findAll();
    }

Repository class:存储库类:

public interface PostRepository extends JpaRepository<PostEntity,Long> {}

For second approach, when you create a Bean, try not to have @Component/@Controller ... on the class for which you create the bean对于第二种方法,当您创建 Bean 时,尽量不要在为其创建 Bean 的类上使用 @Component/@Controller ...

@Configuration
public class AppConfig {
    @Bean
    public TransferService transferService() {
        return new TransferServiceImpl();
    }
}

You can continue to autowire them like in third approach, try not to keep beans of same name您可以像第三种方法一样继续自动装配它们,尽量不要保留同名的 bean

Your error points in the direction, that your Controller has difficulties to resolve the answer of your Service to a valid JSON response.您的错误指向一个方向,即您的控制器难以将您的服务的答案解析为有效的 JSON 响应。

Note that @RestController is just a convenient way to add @Controller and @ResponseBody annotation.请注意,@RestController 只是添加 @Controller 和 @ResponseBody 注释的便捷方式。 When you just add a @Bean annotation you are not adding either @Controller or @ResponseBody.当您只添加@Bean 注释时,您不会添加@Controller 或@ResponseBody。 If you want to use the Controller class without using these Annotations you need to implement the functionality that these classes provide.如果您想在不使用这些注解的情况下使用 Controller 类,您需要实现这些类提供的功能。

However I really see no way, why option 2 would be used for a Controller class.但是,我真的看不出,为什么将选项 2 用于 Controller 类。 If you want to use it for a @Service class (which is doing the same as @Component) you can use the approach that Ravi suggested.如果您想将它用于@Service 类(与@Component 的作用相同),您可以使用 Ravi 建议的方法。

暂无
暂无

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

相关问题 Spring 启动组件扫描无法识别不同 bean 的限定符注释 - Spring boot component scan does not recognize qualifier annotation for different beans spring 引导如何使用 @Autowired 没有 @Component 注释和 xml 配置注入 ApplicationContext 和 JdbcTemplate 的实例? - How does spring boot inject the instance of ApplicationContext and JdbcTemplate using @Autowired without @Component annotation and xml configuration? Spring Boot Application如何在没有@Configuration类的情况下创建bean - How does a Spring Boot Application create beans without @Configuration class Spring Framework:是否可以在没有@Configuration的情况下创建具有相同@Component的两个bean? - Spring Framework: is possible to create two beans of the same @Component without @Configuration? 使用Spring @Configuration批注注入bean列表 - Inject a list of beans using Spring @Configuration annotation 使用注释配置bean的Spring安全配置 - Spring security configuration using annotation configured beans 组件注释在 spring 启动时不起作用 - Component annotation not working in spring boot @Autowired 没有 spring boot 的注解 - @Autowired annotation without spring boot 使用基于注释的配置创建延迟初始化的Spring bean - Creating lazily initialized Spring beans using annotation based configuration 在Spring MVC中,beetwen Beans配置文件和注释使用的区别? - Differences beetwen Beans configuration file and annotation use in Spring MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM