From a0462715daadd244b9b19fc661616b531669e056 Mon Sep 17 00:00:00 2001 From: Vitaliy Fedoriv <vitaliy.fedoriv@gmail.com> Date: Sun, 12 Mar 2017 00:23:56 +0200 Subject: [PATCH] switch app init to boot config --- .../petclinic/PetClinicApplication.java | 12 +++ .../petclinic/PetclinicInitializer.java | 77 ------------------- .../samples/petclinic/ServletInitializer.java | 13 ++++ src/main/resources/application.properties | 21 +++++ 4 files changed, 46 insertions(+), 77 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java delete mode 100644 src/main/java/org/springframework/samples/petclinic/PetclinicInitializer.java create mode 100644 src/main/java/org/springframework/samples/petclinic/ServletInitializer.java create mode 100644 src/main/resources/application.properties diff --git a/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java b/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java new file mode 100644 index 00000000..f377ff55 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java @@ -0,0 +1,12 @@ +package org.springframework.samples.petclinic; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class PetClinicApplication { + + public static void main(String[] args) { + SpringApplication.run(PetClinicApplication.class, args); + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/PetclinicInitializer.java b/src/main/java/org/springframework/samples/petclinic/PetclinicInitializer.java deleted file mode 100644 index 3d99bf6f..00000000 --- a/src/main/java/org/springframework/samples/petclinic/PetclinicInitializer.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2002-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic; - -import org.springframework.web.context.WebApplicationContext; -import org.springframework.web.context.support.XmlWebApplicationContext; -import org.springframework.web.filter.CharacterEncodingFilter; -import org.springframework.web.servlet.DispatcherServlet; -import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; - -import javax.servlet.Filter; -import javax.servlet.ServletContext; - - -/** - * In Servlet 3.0+ environments, this class replaces the traditional {@code web.xml}-based approach in order to configure the - * {@link ServletContext} programmatically. - * <p/> - * Create the Spring "<strong>root</strong>" application context.<br/> - * Register a {@link DispatcherServlet} in the servlet context.<br/> - * For both servlets, register a {@link CharacterEncodingFilter}. - * <p/> - * - * @author Antoine Rey - */ -public class PetclinicInitializer extends AbstractDispatcherServletInitializer { - - /** - * Spring profile used to choose the persistence layer implementation. - * <p/> - * When using Spring jpa, use: jpa - * When using Spring JDBC, use: jdbc - * When using Spring Data JPA, use: spring-data-jpa - */ - private static final String SPRING_PROFILE = "jpa"; - - @Override - protected WebApplicationContext createRootApplicationContext() { - XmlWebApplicationContext rootAppContext = new XmlWebApplicationContext(); - rootAppContext.setConfigLocations("classpath:spring/business-config.xml", "classpath:spring/tools-config.xml"); - rootAppContext.getEnvironment().setActiveProfiles(SPRING_PROFILE); - return rootAppContext; - } - - @Override - protected WebApplicationContext createServletApplicationContext() { - XmlWebApplicationContext webAppContext = new XmlWebApplicationContext(); - webAppContext.setConfigLocation("classpath:spring/mvc-config.xml"); - return webAppContext; - } - - @Override - protected String[] getServletMappings() { - return new String[]{"/"}; - } - - @Override - protected Filter[] getServletFilters() { - // Used to provide the ability to enter Chinese characters inside the Owner Form - CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter("UTF-8", true); - return new Filter[]{characterEncodingFilter}; - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/ServletInitializer.java b/src/main/java/org/springframework/samples/petclinic/ServletInitializer.java new file mode 100644 index 00000000..25f8fccc --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/ServletInitializer.java @@ -0,0 +1,13 @@ +package org.springframework.samples.petclinic; + +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.support.SpringBootServletInitializer; + +public class ServletInitializer extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(PetClinicApplication.class); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 00000000..91ebd53b --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,21 @@ +# When using Spring jpa, use: jpa +# When using Spring JDBC, use: jdbc +# When using Spring Data JPA, use: spring-data-jpa + +spring.profiles.active=spring-data-jpa + +database=hsqldb +spring.datasource.schema=classpath*:db/${database}/initDB.sql +spring.datasource.data=classpath*:db/${database}/populateDB.sql + +server.port=9966 +server.context-path=/petclinic/ + + +spring.jpa.hibernate.ddl-auto=none +spring.messages.basename=messages/messages + +logging.level.org.springframework=INFO + + + -- GitLab