我是靠谱客的博主 可耐未来,这篇文章主要介绍JAVA Eclipse使用Maven构建web项目详解(SSM框架),现在分享给大家,希望可以做个参考。

tips: 启动项目后,welcome-file的链接即为测试用例

https://github.com/sheep0704/JAVA-SSM-Spring

  1. 部署maven web项目
    • Eclipse使用Maven构建web项目详解
    • pom.xml添加webapp依赖:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency>
  • 配置jdk版本,在build->plugins节点中添加:
复制代码
1
2
3
4
5
6
7
8
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin>
  1. 整合spring
  2. 整合log4j
  3. 配置dataSource

    • Spring整合阿里巴巴开源数据源Druid
复制代码
1
2
3
4
5
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.7</version> </dependency>

“`
* [编程方式取得Spring上下文的Properties]

复制代码
1
2
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&amp;characterEncoding=utf-8
  1. 整合mybatis
    • mybatis-spring官方文档 - SqlSessionFactoryBean

      在此,应完成的有:
      1. 事务管理
      2. 扫描mapper包

  2. 整合mybatis-generator
    • MyBatis Generator generatorConfig.xml配置详解
      mybatis-spring的版本换成1.3.0,否则会报错:
      java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L
    • 用Maven插件生成Mybatis代码
  3. 用junit测试一下
    • Junit测试用例见test下的cn.jxnu.mapper.UserMapperTest
    • Spring Test+JUnit完美组合
  4. 整合springMVC

    • spring MVC配置详解

    一、[编程方式取得Spring上下文的Properties]
    在Spring初始化时,可以使用Properties配置器把properties文件装载到Spring的上下文中。

复制代码
1
2
3
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=“http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd” <context:property-placeholder location="classpath:dataSource.properties" />

这样在Spring的配置文件中可以用表达式来获得load进来的properties内容,例如:

复制代码
1
2
3
<property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" />

有时候我们在程序中也需要用到这些配置,那么如何取值,显然不能使用${}方式的。
这时要决定用什么方式来获取properties了,最方便的当然是直接读取文件,此处省略。
如果程序一定要用通过Spring加载的properties,那么我们首先要得到Context了。

1、FileSystemXmlApplicationContext——从指定的目录中加载:

复制代码
1
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");

2、ClassPathXmlApplicationContext——从classpath路径加载:

复制代码
1
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

3、参照http://blog.csdn.net/dqatsh/article/details/3469278, 通过web.xml来获取Context。

4、在servlet中获取。

复制代码
1
2
ServletContext servletContext = servlet.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

然后可以通过相应的bean去访问需要的properties(spring配置文件中${}方式设置到bean里)的值,这里不记录。

用PropertyPlaceholderConfigurer在加载上下文的时候暴露properties

复制代码
1
2
3
4
5
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>hello.properties</value> </property> </bean>

表明PropertyPlaceholderConfigurer是承担properties读取任务的类。

下面的类继承PropertyPlaceholderConfigurer,通过重写processProperties方法把properties暴露出去了。

复制代码
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
import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer { private static Map<String, Object> ctxPropertiesMap; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)throws BeansException { super.processProperties(beanFactory, props); //load properties to ctxPropertiesMap ctxPropertiesMap = new HashMap<String, Object>(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } //static method for accessing context properties public static Object getContextProperty(String name) { return ctxPropertiesMap.get(name); } }

这样此类即完成了PropertyPlaceholderConfigurer的任务,同时又提供了上下文properties访问的功能。
于是在Spring配置文件中把PropertyPlaceholderConfigurer改成CustomizedPropertyConfigurer

复制代码
1
2
3
4
5
<!-- use customized properties configurer to expose properties to program --> <bean id="configBean" class="com.payment.taobaoNavigator.util.CustomizedPropertyConfigurer"> <property name="location" value="classpath:dataSource.properties" /> </bean>

最后在程序中我们便可以使用CustomizedPropertyConfigurer.getContextProperty()来取得上下文中的properties的值了。

最后

以上就是可耐未来最近收集整理的关于JAVA Eclipse使用Maven构建web项目详解(SSM框架)的全部内容,更多相关JAVA内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部