Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以将bean注入到视图中?_Java_Spring_Spring Mvc - Fatal编程技术网

Java 是否可以将bean注入到视图中?

Java 是否可以将bean注入到视图中?,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在编写一个自定义视图,并希望为Freemarker模板引擎注入如下配置,但configurer始终为null @Component public class WkHtmlToPdfView extends AbstractView { private Logger logger = Logger.getLogger(this.getClass().toString()); @Inject private FreeMarkerConfigurer configured; Bean配置正确

我正在编写一个自定义视图,并希望为Freemarker模板引擎注入如下配置,但configurer始终为null

@Component
public class WkHtmlToPdfView extends AbstractView {

private Logger logger = Logger.getLogger(this.getClass().toString());

@Inject
private FreeMarkerConfigurer configured;
Bean配置正确,因为在控制器中它被正确注入:

@Controller
@RequestMapping("/dashboard")
public class DashboardController {

@Inject
private FreeMarkerConfigurer configured;
配置没有我想的那么特别:

@Configuration
@EnableWebMvc
@EnableJpaRepositories(basePackages = {"com.xxxxxx.yy.repository" })
@EnableTransactionManagement
@ComponentScan(basePackages = "com.xxxxxx.yy")
@PropertySource("classpath:application.properties")
public class WebAppConfig extends WebMvcConfigurerAdapter {

@Bean
public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
    FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
    factory.setTemplateLoaderPath("classpath:WEB-INF/templates");
    FreeMarkerConfigurer result = new FreeMarkerConfigurer();
    freemarker.template.Configuration config = factory.createConfiguration();
    config.setIncompatibleImprovements(new Version(2, 3, 20));
    config.setDefaultEncoding("UTF-8");
    config.setLocale(Locale.GERMANY);
    config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    config.setObjectWrapper(new BeansWrapper());
    result.setConfiguration(config);
    return result;
}
不可能将bean注入视图吗?我错过了什么? 谢谢你的帮助

编辑1

我的视图位于子包视图中。我有一个AppConfig,我没提到。 实际上是这样的

@Configuration
@ComponentScan(basePackages = { "com.xxxxxx.yy" }, excludeFilters = { @ComponentScan.Filter(value = Controller.class, type = FilterType.ANNOTATION) })
public class AppConfig {

}
但如果我设定

@ComponentScan(basePackages = { "com.xxxxxx.yy", "com.xxxxxx.yy.view" }
它起作用了!
你能解释一下有什么不同吗?我想子包也会被扫描。在WebAppConfig中扩展ComponentScan没有效果-为什么

您使用什么作为视图解析器?@ComponentScan(basePackages=“com.xxxxxx.yy”)扫描的包中的视图