使用@Configuration和@Controller注释一个类。需要帮助重构 [英] Annotated a class with @Configuration and @Controller. Need help in refactoring

查看:134
本文介绍了使用@Configuration和@Controller注释一个类。需要帮助重构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的课程,其中我必须同时使用 @Configuration @Controller ,因为它应该只有在整个应用程序中, Thymeleaf 的一个实例我得到了例外。我的其他类用 @RequestScope 注释,所以我不能使用单例作用域bean。所以我有一个配置和控制器的混合来获得结果,但我觉得这是一个不好的做法。我将不胜感激任何帮助重构代码并删除不良做法。

Below is my class in which i had to use both @Configuration and @Controller as there should be only one instance of Thymeleaf in the entire application else i get exceptions for that. My other classes are annotated with @RequestScope so i cannot use a singleton scoped bean. So i had a mixup of Configuration and Controller to get the result, but i feel it is a bad practice. I would appreciate any help to refactor the code and remove the bad practice.

更新

我正在使用 spring-boot 1.5.14 。我使用以下方法处理模板并将处理后的模板保持为字符串。

I am using spring-boot 1.5.14. I am using the following approach to process a template and keep the processed template as string.

@Controller
@Configuration
@EnableWebMvc
@ApplicationScope
public class MyThymeleafConfig {

    @GetMapping("/view-template")
    @ResponseBody
    public void viewTemplates() {

        Context context = new Context();
        context.setVariable("mydata", "this is it");

        String html = templateEngine().process("templates/view-to-process.html", context);
        System.out.println(html);
    }


    /*

    configuration for thymeleaf and template processing

    */

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(thymeleafTemplateResolver());
        return templateEngine;
    }

    @Bean
    public SpringResourceTemplateResolver thymeleafTemplateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setPrefix("classpath:");
        templateResolver.setSuffix(".html");
        templateResolver.setCacheable(false);
        templateResolver.setTemplateMode(TemplateMode.HTML);
        return templateResolver;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        return viewResolver;
    }
}

为静态资源提供以下配置:

To serve static resources the following config:

@Configuration
@EnableWebMvc
public class StaticResourceConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/**")
                .addResourceLocations("/static/", "classpath:static/");
    }
}

更新

我还提到了为什么我不能接受下面提到的答案,因为我的其他课程有请求范围。

I have also mentioned the reasons why i couldn't accept the below mentioned answers as my other classes have request scopes.

更新

我还有其他类 @RequestScope ,如下所示:

I have other classes with @RequestScopelike below:

@RequestScope
@Controller
public class SecondController {

    @GetMapping("/viewPage")
    public String viewPage(Model model) {
        model.addAttribute("mydata", "sjfbsdf");
        model.addAttribute("somedata", "sjdfksfjhshgdfbskdfj");
        return "templates/view-to-process.html";
    }
}


推荐答案

假设你正在使用Spring Boot,因为你有标签,你不需要任何配置来使用Thymeleaf。

Assuming you're using Spring Boot, since you have it in tags, you do not need any configuration to use Thymeleaf.

只需要这种依赖关系,你可以:

@GetMapping("/view-template")
public String viewTemplates(Model model) {
    model.addAttribute("mydata", "this is it")
    return "view-to-process";
}

它应该有效。

顺便说一下,是的,你应该在同一个班级中拥有 @Configuration @Controller 永远不需要。

By the way, yes, having @Configuration and @Controller in the same class is something you should never need.

这篇关于使用@Configuration和@Controller注释一个类。需要帮助重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆