gpt4 book ai didi

java - xml bean配置到java代码配置

转载 作者:行者123 更新时间:2023-12-01 22:43:44 25 4
gpt4 key购买 nike

如何更改此 xml 配置:

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
...
<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
</set>
</property>
...
</bean>

到目前为止,我有一个java代码配置,我只在这部分遇到问题:

<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
</set>
</property>

这是我迄今为止在 java 代码中所拥有的:

   @Bean
public SpringSecurityDialect springSecurityDialect() {
return new SpringSecurityDialect();
}

@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver( templateResolver() );
engine.setMessageSource( messageSource() );

//DIALECTS
Set<SpringSecurityDialect> ssdSet = new HashSet<SpringSecurityDialect>();
ssdSet.add( springSecurityDialect() );
engine.setAdditionalDialects( ssdSet ); <-- this line give me this error
return engine;
}

这一行给我这个错误:The method setAdditionalDialects(Set<IDialect>) in the type TemplateEngine is not applicable for the arguments (Set<SpringSecurityDialect>)

最佳答案

今天我为此花了一段时间。答案在于查看 Spring Boot 源代码:

https://github.com/spring-projects/spring-boot/blob/v1.2.5.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java

@Configuration
@ConditionalOnClass({ SpringSecurityDialect.class })
protected static class ThymeleafSecurityDialectConfiguration {

@Bean
@ConditionalOnMissingBean
public SpringSecurityDialect securityDialect() {
return new SpringSecurityDialect();
}

}

因此 Spring Boot(好吧,“org.springframework.boot:spring-boot-starter-thymeleaf”)将尝试注入(inject) org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect。也许它不起作用,因为我有 springsecurity4 jar。

无论如何,有一个简单的解决方法:只需执行 Spring Boot 尝试执行的操作即可。在 @Configuration 类中,只需添加 bean:

@Bean
public SpringSecurityDialect securityDialect() {
return new SpringSecurityDialect();
}

关于java - xml bean配置到java代码配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25689910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com