Spring4.2+Hibernate5配置

本次配置使用的是注解的方式配置:

1.引入maven依赖

     Spring:

<!--Spring  -->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <argLine>-Dfile.encoding=UTF-8</argLine>
    <spring.version>4.2.4.RELEASE</spring.version>
    <tomcat.version>8.0.23</tomcat.version>
    </properties>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>
<!--Spring  -->

      Hibernate:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.11.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.2.1.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.11.Final</version>
</dependency>
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
Tomcat
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>${tomcat.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jsp-api</artifactId>
    <version>${tomcat.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
    <version>${tomcat.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.9</version>
</dependency> 项目初始化 1.创建WebApp容器,这里使用的是注解容器 
public class WebAppContext { public static final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); public static <T> T getBean(Class<T> requiredType) { return context.getBean(requiredType); } } 2.创建注解容器的配置项 @Configuration @ComponentScan(basePackages = {"cn"}) @EnableWebMvc @ImportResource("classpath:conf/dispatch-mvc.xml") @EnableTransactionManagement(proxyTargetClass = true) public class WebAppConfig extends WebMvcConfigurerAdapter { 3.创建WebApplicationInitializer的实现类,WebInitializer,实现onStartup方法 @Override public void onStartup(ServletContext servletContext) throws ServletException { System.setProperty("APP_CONTEXT_PATH", servletContext.getContextPath()); /*  * 创建并注册{@link DispatcherServlet}  */ WebAppContext.context.register(WebAppConfig.class); DispatcherServlet dispatcherServlet = new DispatcherServlet( WebAppContext.context); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic dispatcher = servletContext.addServlet( AbstractDispatcherServletInitializer.DEFAULT_SERVLET_NAME, dispatcherServlet); // dispatcher.setInitParameter("throwExceptionIfNoHandlerFound",  // "true"); dispatcher.setLoadOnStartup(1); dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING); // /:~  /*  * 解决使用Restful时,静态资源被拦截的问题。在Spring3.2中,使用了addResourceHandler方法, * 对静态资源进行了映射 ,代替此解决方案 。 */  // servletContext.getServletRegistration("default").addMapping(  // DEFAULT_URL_MAPPING);  // /:~  // Manage the lifecycle of the root application context servletContext .addListener(new ContextLoaderListener(WebAppContext.context)); // 加入监听器,在系统关闭的时候,注销JDBC Driver  // servletContext.addListener(new  // ServletContextScopeListener());  // add filter CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); characterEncodingFilter.setForceEncoding(true); servletContext .addFilter("CharacterEncodingFilter", characterEncodingFilter) .addMappingForUrlPatterns(EnumSet.noneOf(DispatcherType.class), false, "/*"); logger.info("Webapp has been initialized at {}", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")); }

 

你可能感兴趣的:(Spring4.2+Hibernate5配置)