-
Notifications
You must be signed in to change notification settings - Fork 0
/
PetclinicInitializer.java
81 lines (71 loc) · 3.04 KB
/
PetclinicInitializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* 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
* <p/>
* <p>
* You also may use the -Dspring.profiles.active=jdbc VM options to change
* default jpa Spring profile.
*/
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().setDefaultProfiles(SPRING_PROFILE);
return rootAppContext;
}
@Override
protected WebApplicationContext createServletApplicationContext() {
XmlWebApplicationContext webAppContext = new XmlWebApplicationContext();
webAppContext.setConfigLocation("classpath:spring/mvc-core-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};
}
}