0

how can i change this xml configuration :

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

to a java code configuration so far i have this i only have problems in this part :

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

this is what i have so far in java code :

   @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;
    }

this line give me this error: The method setAdditionalDialects(Set<IDialect>) in the type TemplateEngine is not applicable for the arguments (Set<SpringSecurityDialect>)

stackUser2000
  • 1,615
  • 11
  • 32
  • 55
  • possible duplicate of [Understanding spring @Configuration class usage](http://stackoverflow.com/questions/24014919/understanding-spring-configuration-class-usage) – Avi Sep 05 '14 at 17:04

3 Answers3

2

I banged my head against this for a while today. The answer lay in looking at the Spring Boot source:

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();
    }

}

So Spring Boot (well, "org.springframework.boot:spring-boot-starter-thymeleaf") will try to inject the org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect. Perhaps it's not working because I have the springsecurity4 jar.

In any case, there's a simple fix: just do what Spring Boot tries to do. In an @Configuration class, just add the bean:

@Bean
public SpringSecurityDialect securityDialect() {
    return new SpringSecurityDialect();
}
tpdi
  • 34,554
  • 11
  • 80
  • 120
1

According to your config, property additionalDialects should assigned with a Set containing one bean. So in javaconfig just create an instance of HashSet, add to it SpringSecurityDialect bean and assign the resulting set to additionalDialects property. SpringSecurityDialect bean should be created by method annotated with @Bean to let Spring application context know about this bean to execute bean initializers and postprocessors.
Following code illustrates it:

@Bean
public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine  =  new SpringTemplateEngine();
        engine.setTemplateResolver( templateResolver() );
        engine.setMessageSource( messageSource() );
        Set<SpringSecurityDialect> ssdSet = new HashSet<SpringSecurityDialect>();
        ssdSet.add( springSecurityDialect() );
        engine.setAdditionalDialects( ssdSet );

        return engine;
    }

@Bean
public SpringSecurityDialect springSecurityDialect() {
    return new SpringSecurityDialect();
}
vp8106
  • 201
  • 2
  • 5
  • Please elaborate, why this helps by editing your answer. – Artjom B. Sep 05 '14 at 21:59
  • so where should i pass this string in that type of configuration `org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect` – stackUser2000 Sep 26 '14 at 21:25
  • @stackUser2000 it's not just a string, it's a class name, so put it in import section of your javaconfig class – vp8106 Sep 26 '14 at 21:38
  • hey friend i try what you said and it give this error (see how i edit my question with your code) `The method setAdditionalDialects(Set) in the type TemplateEngine is not applicable for the arguments (Set)` – stackUser2000 Sep 29 '14 at 18:43
0

You could instantiate the entire engine using code similar to this.

FileSystemXmlApplicationContext appCtx = new FileSystemXmlApplicationContext("myconfigfile.xml");
if (appCtx.containsBean("templateEngine") {
   SpringTemplateEngine engine = (SpringTemplateEngine) appCtx.getBean("templateEngine");
}

The additional dialects and any other properties you've set on the engine in your xml configuration file will be applied.

You might want to look at using ClassPathXmlApplicationContext instead of the FileSystem one in my example.

  • yeah but you will still need the .xml file I'm trying to get the configuration without the .xml file, so is there no way to do that config without a xml file?? – stackUser2000 Sep 05 '14 at 17:53
  • Ah, I misunderstood the question. It looks like vp8106 has an answer above that does what you want. If that's also not a satisfactory answer than please add some clarification and I'll try to help again. – Bryan Moore Sep 06 '14 at 13:48