diff --git a/parent-spring-6/README.md b/parent-spring-6/README.md new file mode 100644 index 0000000000..791a6ca197 --- /dev/null +++ b/parent-spring-6/README.md @@ -0,0 +1,3 @@ +## Parent Spring 5 + +This is a parent module for all projects using Spring 5 diff --git a/parent-spring-6/pom.xml b/parent-spring-6/pom.xml new file mode 100644 index 0000000000..e1b92ca8cd --- /dev/null +++ b/parent-spring-6/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + parent-spring-6 + 0.0.1-SNAPSHOT + parent-spring-6 + pom + Parent for all spring 6 core modules + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + + org.springframework + spring-framework-bom + ${spring.version} + pom + import + + + + + + + org.springframework + spring-core + + + + + 6.0.8 + + + diff --git a/pom.xml b/pom.xml index cc0aca635a..16fc0007ea 100644 --- a/pom.xml +++ b/pom.xml @@ -331,6 +331,7 @@ parent-boot-2 parent-spring-4 parent-spring-5 + parent-spring-6 parent-java checker-framework @@ -408,6 +409,7 @@ parent-boot-2 parent-spring-4 parent-spring-5 + parent-spring-6 parent-java spring-4 @@ -468,6 +470,7 @@ parent-boot-2 parent-spring-4 parent-spring-5 + parent-spring-6 parent-java apache-spark @@ -509,6 +512,7 @@ parent-boot-2 parent-spring-4 parent-spring-5 + parent-spring-6 parent-java checker-framework @@ -577,6 +581,7 @@ parent-boot-2 parent-spring-4 parent-spring-5 + parent-spring-6 parent-java spring-4 @@ -629,6 +634,7 @@ parent-boot-2 parent-spring-4 parent-spring-5 + parent-spring-6 parent-java apache-spark @@ -896,6 +902,7 @@ spring-5-webflux spring-5-webflux-2 spring-activiti + spring-actuator spring-core-2 spring-core-3 spring-credhub @@ -1201,6 +1208,7 @@ parent-boot-2 parent-spring-4 parent-spring-5 + parent-spring-6 parent-java diff --git a/spring-actuator/pom.xml b/spring-actuator/pom.xml new file mode 100644 index 0000000000..48dae45940 --- /dev/null +++ b/spring-actuator/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + spring-actuator + spring-actuator + war + Demo project for Spring Boot Actuator without Spring Boot + + + com.baeldung + parent-spring-6 + 0.0.1-SNAPSHOT + ../parent-spring-6 + + + + + jakarta.servlet + jakarta.servlet-api + 5.0.0 + provided + + + org.springframework + spring-webmvc + + + org.springframework + spring-test + test + + + org.springframework.boot + spring-boot-actuator-autoconfigure + ${spring-boot.version} + + + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + + + + + + + org.apache.maven.plugins + maven-war-plugin + 3.3.2 + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + + + + org.eclipse.jetty + jetty-maven-plugin + + + + + + 3.0.6 + 11.0.15 + + + diff --git a/spring-actuator/src/main/java/com/baeldung/spring/actuator/ActuatorConfiguration.java b/spring-actuator/src/main/java/com/baeldung/spring/actuator/ActuatorConfiguration.java new file mode 100644 index 0000000000..857db1a0d5 --- /dev/null +++ b/spring-actuator/src/main/java/com/baeldung/spring/actuator/ActuatorConfiguration.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.actuator; + +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.boot.actuate.info.InfoContributor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ActuatorConfiguration { + + @Bean + public HealthIndicator sampleHealthIndicator() { + return Health.up() + .withDetail("info", "Sample Health") + ::build; + } + + @Bean + public InfoContributor provideSampleInfos() { + return builder -> + builder + .withDetail("app-title", "Actuator Sample Application"); + + } +} diff --git a/spring-actuator/src/main/java/com/baeldung/spring/actuator/AppConfig.java b/spring-actuator/src/main/java/com/baeldung/spring/actuator/AppConfig.java new file mode 100644 index 0000000000..e080cc10e2 --- /dev/null +++ b/spring-actuator/src/main/java/com/baeldung/spring/actuator/AppConfig.java @@ -0,0 +1,11 @@ +package com.baeldung.spring.actuator; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.PropertySource; + +@EnableAutoConfiguration +@PropertySource("classpath:application.properties") +@ComponentScan(basePackageClasses = AppConfig.class) +public class AppConfig { +} diff --git a/spring-actuator/src/main/java/com/baeldung/spring/actuator/Start.java b/spring-actuator/src/main/java/com/baeldung/spring/actuator/Start.java new file mode 100644 index 0000000000..66899d1479 --- /dev/null +++ b/spring-actuator/src/main/java/com/baeldung/spring/actuator/Start.java @@ -0,0 +1,64 @@ +package com.baeldung.spring.actuator; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +public class Start { + + private static final Logger logger = LoggerFactory.getLogger(Start.class); + private static final int DEFAULT_PORT = 8080; + private static final String CONTEXT_PATH = "/"; + private static final String CONFIG_LOCATION = AppConfig.class.getName(); + private static final String MAPPING_URL = "/*"; + + /* + * This application runs a Jetty webcontainer that runs the + * Spring Dispatcher Servlet. + */ + public static void main(String[] args) throws Exception { + new Start().startJetty(getPortFromArgs(args)); + } + + private static int getPortFromArgs(String[] args) { + if (args.length > 0) { + try { + return Integer.parseInt(args[0]); + } catch (NumberFormatException ignore) { + } + } + logger.info("No server port configured, falling back to {}", DEFAULT_PORT); + return DEFAULT_PORT; + } + + private void startJetty(int port) throws Exception { + logger.info("Starting server at port {}", port); + Server server = new Server(port); + server.setHandler(getServletContextHandler(getContext())); + server.start(); + logger.info("Server started at port {}", port); + server.join(); + } + + private static ServletContextHandler getServletContextHandler(WebApplicationContext context) { + ServletContextHandler contextHandler = new ServletContextHandler(); + contextHandler.setErrorHandler(null); + contextHandler.setContextPath(CONTEXT_PATH); + contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL); + contextHandler.addEventListener(new ContextLoaderListener(context)); + return contextHandler; + } + + private static WebApplicationContext getContext() { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.setConfigLocation(CONFIG_LOCATION); + return context; + } + +} diff --git a/spring-actuator/src/main/java/com/baeldung/spring/actuator/WebMvcConfiguration.java b/spring-actuator/src/main/java/com/baeldung/spring/actuator/WebMvcConfiguration.java new file mode 100644 index 0000000000..8da2166114 --- /dev/null +++ b/spring-actuator/src/main/java/com/baeldung/spring/actuator/WebMvcConfiguration.java @@ -0,0 +1,23 @@ +package com.baeldung.spring.actuator; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebMvcConfiguration { + + @Bean + public WebMvcConfigurer webMvcConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry + .addViewController("/") + .setViewName("redirect:/actuator"); + } + }; + } + +} diff --git a/spring-actuator/src/main/resources/application.properties b/spring-actuator/src/main/resources/application.properties new file mode 100644 index 0000000000..db3cdb647b --- /dev/null +++ b/spring-actuator/src/main/resources/application.properties @@ -0,0 +1,2 @@ +management.endpoints.web.exposure.include=* +management.endpoint.health.show-details=always