标签:
File --> New --> Other,然后选择Maven目录下的Maven Project,如下图:
然后在弹出的面板中选择配置后,下一步即可,如图:
Create a simple project (skip archetype selection) :此选项表示跳过后续的配置选项框。
Use default Workspace location:表示将项目放置到默认的工作区域下。
配置好后,Next下一步,再进行如下图的配置:
点击Finish后,便完成了Maven工程的创建。
此时在你开发工具的Workspace工作区的树桩菜单中出现了SpringRedisStudy这个项目工程.如图:
这时,工程会报错,是因为一开始创建Maven工程时,选择的是 Create a simple project 属性,
所以在webapp项目下可能会没有任何文件,如图:
这个时候就需要我们自己去创建它们。
首先在 webapp 下增加一个 WEB-INF 文件夹;
然后在 WEB-INF 文件夹中增加一个 web.xml(部署描述符文件);
接下来我们就要在这个文件中进行一些配置工作;
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>SpringRedisStudy</display-name> <!-- 设置由Sprng载入的Log4j配置文件位置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:config/property/log4j.properties</param-value> </context-param> <!-- Spring刷新Log4j配置文件变动的间隔,单位为毫秒 --> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>100000</param-value> </context-param> <!-- 加载其他配置文件至Spring应用上下文中 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 指定多个Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/xml/dispatcher-servlet.xml; classpath:config/xml/spring-context.xml </param-value> </context-param> <!-- Spring字符集过滤器 --> <filter> <filter-name>SpringEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>SpringEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring view分发器 --> <!-- servlet-name 决定了初始化的名字:name-servlet.xml --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/conf/dispatcher-servlet.xml </param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
接下来我们进行详细的项目搭建,首先配置maven 工程文件 pom.xml,
因为我们要搭建Spring MVC这个工程,所以就要将相关的jar包配置到Maven工程中进行引用管理;
pom.xml文件配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringRedisStudy</groupId> <artifactId>SpringRedisStudy</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- spring版本号 --> <org.springframework.version>3.0.7.RELEASE</org.springframework.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <slf4j.version>1.7.5</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!-- config junit jar --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <!-- config redis data and client jar --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.4.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency> <!-- config need jar --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_3.0_spec</artifactId> <version>1.0</version> </dependency> <!-- cofig spring jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> <scope>test</scope> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> </dependencies> <build> <resources> <resource> <directory>/src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <version>2.5</version> </configuration> </plugin> </plugins> </build> </project>
Maven配置好后,我们就需要在项目中进行Spring环境搭建了;
在/SpringRedisStudy工程的src/main/resources目录新建config.xml包,并创建spring-context.xml文件;
这个文件的作用是配置Spring容器的,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 激活@Controller模式 --> <mvc:annotation-driven /> <context:annotation-config /> <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 --> <context:component-scan base-package="com.test.*" /> <!-- 引入同文件夹下的redis属性配置文件 --> <import resource="redis-context.xml"/> <!-- 测试用的userDao依赖注入 --> <bean id="userDao" class="com.test.dao.impl.UserDao" /> </beans>
在/SpringRedisStudy工程的src/main/resources目录下的config.xml包中创建redis-context.xml文件;
这个文件的作用是配置redis的,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 引入redis.properties配置文件 --> <context:property-placeholder location="classpath:/config/property/redis.properties" /> <!-- redis 相关配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" p:maxTotal="${redis.maxTotal}" p:maxIdle="${redis.maxIdle}" p:testOnBorrow="${redis.testOnBorrow}" /> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="${redis.host}" p:port="${redis.port}" p:password="${redis.password}" p:database="${redis.database}" p:pool-config-ref="poolConfig" /> <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory-ref="connectionFactory" p:keySerializer-ref="redisTemplateKeySerializer" p:valueSerializer-ref="redisTemplateValueSerializer" p:hashKeySerializer-ref="redisTemplateHashKeySerializer" p:hashValueSerializer-ref="redisTemplateHashValueSerializer"/> <bean id="redisTemplateKeySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="redisTemplateValueSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> <bean id="redisTemplateHashKeySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="redisTemplateHashValueSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </beans>
在/SpringRedisStudy工程的src/main/resources目录下新建config.property包中创建redis-context.xml文件;
这个文件的作用是配置redis的,内容如下:
# Redis settings
# server IP
redis.host=127.0.0.1
# server port
redis.port=6379
redis.password=myredis
redis.maxTotal=600
#redis.maxActive=600 在新版中此属性不可用
# use dbIndex
redis.database=0
# 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例
redis.maxIdle=300
# 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException;
#redis.maxWait=5000 在新版中此属性不可用
# 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
redis.testOnBorrow=true
在/SpringRedisStudy工程的src/main/resources目录下新建log4j.properties文件,内容如下:
#All level less than INFO will be logged
log4j.rootLogger=info,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.ImmediateFlush=true
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %m%n
标签:
原文地址:http://www.cnblogs.com/asoks/p/5324605.html