Spring MVC学习笔记

本文记录学习SpringSpring MVC过程中的笔记。

基于Java的配置方式

1
2
3
4
5
6
// Marks this is a configuration class
@Configuration
// Enables scanning components in current and sub packages
@ComponentScan
public class SpringConfig {
}

分profile的配置

开发环境的配置类

1
2
3
4
5
@Configuration
@Profile("dev")
public class SpringDevConfig {
// Configurations
}

生产环境的配置类

1
2
3
4
@Configuration
@Profile("prod")
public class SpringProdConfig {
}

最外层配置类中引入各环境配置

1
2
3
4
5
6
7
8
// Marks this is a configuration class
@Configuration
// Enables scanning components in current and sub packages
@ComponentScan
// Imports configuration classes
@Import({SpringDevConfig.class, SpringProdConfig.class})
public class SpringConfig {
}

web.xml中配置默认环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<!--
The default profile will be the development profile.
You can add following code to tomcat's context.xml to enable the production environment:

<Environment name="spring.profiles.active" type="java.lang.String" value="prod" />
-->
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

生产环境的Tomcatcontext.xml中配置启用生产环境配置(JNDI方式)

1
2
3
4
5
6
7
8
9
<Context>
<!-- Other configurations -->

<Environment
name="spring.profiles.active"
type="java.lang.String"
value="prod" />

</Context>