Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use well-known bean name for view resolver #51

Merged
merged 3 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package io.github.wimdeblauwe.hsbt.mvc;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import static io.github.wimdeblauwe.hsbt.mvc.HtmxResponseHeader.HX_REFRESH;
import static io.github.wimdeblauwe.hsbt.mvc.HtmxResponseHeader.HX_TRIGGER;
import static io.github.wimdeblauwe.hsbt.mvc.HtmxResponseHeader.HX_TRIGGER_AFTER_SETTLE;
import static io.github.wimdeblauwe.hsbt.mvc.HtmxResponseHeader.HX_TRIGGER_AFTER_SWAP;

import java.lang.reflect.Method;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Method;

import static io.github.wimdeblauwe.hsbt.mvc.HtmxResponseHeader.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class HtmxHandlerInterceptor implements HandlerInterceptor {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package io.github.wimdeblauwe.hsbt.mvc;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;

import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;

@AutoConfiguration
@ConditionalOnWebApplication
public class HtmxMvcConfiguration implements WebMvcRegistrations, WebMvcConfigurer {

private final ViewResolver resolver;
private final ObjectFactory<ViewResolver> resolver;
private final ObjectFactory<LocaleResolver> locales;
private final ObjectMapper objectMapper;

HtmxMvcConfiguration(ThymeleafViewResolver resolver, ObjectFactory<LocaleResolver> locales, ObjectMapper objectMapper) {
Assert.notNull(resolver, "ViewResovler must not be null!");
HtmxMvcConfiguration(@Qualifier("viewResolver") ObjectFactory<ViewResolver> resolver, ObjectFactory<LocaleResolver> locales, ObjectMapper objectMapper) {
Assert.notNull(resolver, "ViewResolver must not be null!");
Assert.notNull(locales, "LocaleResolver must not be null!");

this.resolver = resolver;
Expand All @@ -41,7 +43,7 @@ public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HtmxHandlerInterceptor());
registry.addInterceptor(new HtmxViewHandlerInterceptor(resolver, locales, objectMapper));
registry.addInterceptor(new HtmxViewHandlerInterceptor(resolver.getObject(), locales, objectMapper));
}

@Override
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/io/github/wimdeblauwe/hsbt/DummyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;

import io.github.wimdeblauwe.hsbt.mvc.PartialsController.TodoRepository;

/**
* Just created this here to make Spring Boot test slices work
*/
@SpringBootApplication
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class}) // Security is on by default
public class DummyApplication {

public static void main(String[] args) {
SpringApplication.run(DummyApplication.class);
}

@Bean
TodoRepository repository() {
wimdeblauwe marked this conversation as resolved.
Show resolved Hide resolved
return () -> 2;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.github.wimdeblauwe.hsbt.mvc;

import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@HxRequest
Expand Down