最近剛入職,發現公司都是使用eclipse,以前一直在學校一直使用netbeans集成開發環境,對eclipse不是太熟悉,本身也不太喜歡使用myeclipse收費的軟件(雖然能夠盜版激活),反應慢也是myeclipse被人詬病的緣由,決定花一天時間來本身動手搭建eclipse+maven+spring。
準備工做:
1.下載eclipse(Eclipse Java EE IDE for Web Developers,Version: Juno Service Release 2)。
2.下載maven3.3.3。 java
3.安裝maven插件()。 mysql
注意事項:maven3.3.3支持jdk1.7,maven3.2.5僅僅支持到1.6。 web
第一步:新建maven項目,選擇Maven Project,如圖1。
第二步:配置轉換項目爲spring項目。
首先右擊項目,點擊Properties,選擇左邊的java build path,咱們先把出問題的刪除,若是不刪除,後面在新建source file時沒法新建,如圖,選擇remove就好了。
新建src/main/java、src/test/java/、src/test/resources,如圖,提示:若是你eclipse是選擇java模式,能夠直接新建source file,j2ee模式能夠切換到java模式。
修改jdk版本,若是此版本不修改,會形成maven與jdk不兼容,致使編譯出問題,右擊JRE System Library[J2SE-1.5] 修改問1.7,如圖。
你能夠調整source folder的順序,根據本身的喜愛,我不知道有什麼影響,看到有的教程上有調整的,若是你有強迫症,能夠修改一下順序,如圖。
下面,咱們解決最重要的一個步驟,spring項目的雛形就在這裏顯現, 把項目轉化爲web項目,先找到位置,如圖。
點擊Convert to faceted form... (註釋:有的教程修改Further configuration available...,其實通常默認就能夠,不用能夠修改,個人eclipse就選的默認的,沒修改),在右邊選擇你項目運行的應用服務器,我選的是tomcat 7.0,你選的maven 和tomcat與你的jdk有很的關係,如今雖然8出來了,單是選擇1.7較爲穩定,只是建議,根據我的喜愛。
如今就轉化爲了web項目,咱們在配置其餘的東西,完成整個項目 。
如今的目錄結構爲這種狀況,如圖:
第三步:轉化爲spring項目,添加applicationContext.xml、simple-servlet.xml,配置spring文件,配置spring引用包即肯定引用文件pom.xml。
applicationContext.xml 、demo-servlet.xml、web.xml爲spring配置文件,demo-servlet.xml爲配置轉發器,主要做用與view層,applicationContext.xml主要配置數據庫與事務,在這裏不過多講解,單是這兩個文件必定要搞清楚怎麼配置,每一個配置模塊什麼意思,不求甚解會害慘本身,出個問題,找不到緣由,我就曾經在這個地方吃過虧。對於pom.xml文件,我建議你們整理出一個本身須要的版本,以前你們不使用maven的時候,都是整理本身一個完整的lib包,maven的做用就是替你們管理引用文件,pom.xml文件就是配置須要引入的包,你項目依賴哪些包,就在這些pom.xml文件裏引用,maven會幫你下載。下面依次列出每一個文件的內容。
demo-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>spring
<
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"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:tx
="http://www.springframework.org/schema/tx"
xmlns:mvc
="http://www.springframework.org/schema/mvc"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
>
<
bean
class
="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"
/>
<
context:component-scan
base-package
="com.demo.controller"
/>
<
mvc:annotation-driven
/>
<
bean
id
="viewResolver"
class
="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix
="/WEB-INF/jsp/"
p:suffix
=".jsp"
/>
</
beans
>
applicationContext.xml
<?
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"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:tx
="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
>
<
context:component-scan
base-package
="com.demo"
/>
<
bean
id
="dataSource"
class
="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName
="com.mysql.jdbc.Driver"
p:url
="jdbc:mysql://XXX.XXX.130.XXX:3306/anhe?useUnicode=true&characterEncoding=UTF-8"
p:username
="root"
p:password
="XXXXXX"
/>
<!--
配置Jdbc模板
-->
<
bean
id
="jdbcTemplate"
class
="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref
="dataSource"
/>
<!--
配置事務管理器
-->
<
bean
id
="transactionManager"
class
="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref
="dataSource"
/>
<!--
經過AOP配置提供事務加強,讓service包下全部Bean的全部方法擁有事務
-->
<
aop:config
proxy-target-class
="true"
>
<
aop:pointcut
id
="serviceMethod"
expression
=" execution(* com.baobaotao.service..*(..))"
/>
<
aop:advisor
pointcut-ref
="serviceMethod"
advice-ref
="txAdvice"
/>
</
aop:config
>
<
tx:advice
id
="txAdvice"
transaction-manager
="transactionManager"
>
<
tx:attributes
>
<
tx:method
name
="*"
/>
</
tx:attributes
>
</
tx:advice
>
</
beans
>
web.xmlsql
<?xml version="1.0" encoding="UTF-8"?>數據庫
<
web-app
version
="3.0"
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_3_0.xsd"
>
<
filter
>
<
filter-name
>CharacterEncoding
</
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
>CharacterEncoding
</
filter-name
>
<
url-pattern
>/*
</
url-pattern
>
</
filter-mapping
>
<
context-param
>
<
param-name
>contextConfigLocation
</
param-name
>
<
param-value
>/WEB-INF/applicationContext.xml
</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<
servlet
>
<
servlet-name
>demo
</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet
</
servlet-class
>
<
load-on-startup
>2
</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>demo
</
servlet-name
>
<
url-pattern
>*.do
</
url-pattern
>
</
servlet-mapping
>
<
session-config
>
<
session-timeout
>
30
</
session-timeout
>
</
session-config
>
<
welcome-file-list
>
<
welcome-file
>/WEB-INF/jsp/index.jsp
</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
pom.xml文件express
<
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/maven-v4_0_0.xsd"
>
<
modelVersion
>4.0.0
</
modelVersion
>
<
groupId
>smart
</
groupId
>
<
artifactId
>demo
</
artifactId
>
<
packaging
>war
</
packaging
>
<
version
>0.0.1-SNAPSHOT
</
version
>
<
name
>demo Maven Webapp
</
name
>
<
url
>http://maven.apache.org
</
url
>
<
repositories
>
<
repository
>
<
id
>com.springsource.repository.bundles.release
</
id
>
<
name
>EBR Spring Release Repository
</
name
>
<
url
>http:// repository.springsource.com/maven/bundles/release
</
url
>
</
repository
>
<
repository
>
<
id
>com.springsource.repository.bundles.external
</
id
>
<
name
>EBR External Release Repository
</
name
>
<
url
>http:// repository.springsource.com/maven/bundles/external
</
url
>
</
repository
>
</
repositories
>
<
properties
>
<
org.springframework.version
>3.0.5.RELEASE
</
org.springframework.version
>
</
properties
>
<!--
Core utilities used by other modules. Define this if you use Spring
Utility APIs (org.springframework.core.*/org.springframework.util.*)
-->
<
dependencies
>
<
dependency
>
<
groupId
>org.aspectj
</
groupId
>
<
artifactId
>aspectjweaver
</
artifactId
>
<
version
>1.6.12
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-core
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Expression Language (depends on spring-core) Define this if you use
Spring Expression APIs (org.springframework.expression.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-expression
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Bean Factory and JavaBeans utilities (depends on spring-core) Define
this if you use Spring Bean APIs (org.springframework.beans.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-beans
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Aspect Oriented Programming (AOP) Framework (depends on spring-core,
spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-aop
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Application Context (depends on spring-core, spring-expression, spring-aop,
spring-beans) This is the central artifact for Spring's Dependency Injection
Container and is generally always defined
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-context
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Various Application Context utilities, including EhCache, JavaMail,
Quartz, and Freemarker integration Define this if you need any of these integrations
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-context-support
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Transaction Management Abstraction (depends on spring-core, spring-beans,
spring-aop, spring-context) Define this if you use Spring Transactions or
DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-tx
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
JDBC Data Access Library (depends on spring-core, spring-beans, spring-context,
spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-jdbc
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA,
and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx)
Define this if you need ORM (org.springframework.orm.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-orm
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Object-to-XML Mapping (OXM) abstraction and integration with JAXB,
JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans,
spring-context) Define this if you need OXM (org.springframework.oxm.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-oxm
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Web application development utilities applicable to both Servlet and
Portlet Environments (depends on spring-core, spring-beans, spring-context)
Define this if you use Spring MVC, or wish to use Struts, JSF, or another
web framework with Spring (org.springframework.web.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-web
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Spring MVC for Servlet Environments (depends on spring-core, spring-beans,
spring-context, spring-web) Define this if you use Spring MVC with a Servlet
Container such as Apache Tomcat (org.springframework.web.servlet.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-webmvc
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Spring MVC for Portlet Environments (depends on spring-core, spring-beans,
spring-context, spring-web) Define this if you use Spring MVC with a Portlet
Container (org.springframework.web.portlet.*)
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-webmvc-portlet
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
</
dependency
>
<!--
Support for testing Spring applications with tools such as JUnit and
TestNG This artifact is generally always defined with a 'test' scope for
the integration testing framework and unit testing stubs
-->
<
dependency
>
<
groupId
>org.springframework
</
groupId
>
<
artifactId
>spring-test
</
artifactId
>
<
version
>${org.springframework.version}
</
version
>
<
scope
>test
</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>mysql
</
groupId
>
<
artifactId
>mysql-connector-java
</
artifactId
>
<
version
>5.1.36
</
version
>
</
dependency
>
</
dependencies
>
<
build
>
<
finalName
>demo
</
finalName
>
</
build
></
project
>
配置一下項目依賴關係,如圖,remove右邊紅色框裏面的內容,部署不須要把test文件部署上去,是否是???毋庸置疑。apache
再Add以下文件,如圖:spring-mvc
不知道大家發現嗎?如今項目仍是紅叉+s,那是由於咱們還有一個地方沒有修改,如圖: tomcat
當咱們配置好以後,項目又有可能自動改回來,看看以前是否是咱們選擇過jdk版本,可是如今又改了一次,那是由於還有個地方搗鬼,我當時就吃虧到到這個地方,浪費了好幾十分鐘思考,結果仍是不如谷歌,哦,又想起一點,谷歌不是被封了,用這個"穀粉http://www.jwss.com/" ,也是谷歌的引擎。修改下圖配置,java編譯器:
此刻,咱們神奇的發現,是否是在maven依賴包裏出現了以下美好畫面:
至此,項目配置文件搞定,下面咱們添加示例代碼演示maven神奇之處。
第五步:測試項目。咱們只寫一個攔截器,來測試一下。
新建一個jsp文件夾,如圖:
在controller報中,咱們新建一個java類HomeController,添加以下代碼:
package
com.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.demo.dao.HomeServcies;
@Controller
public
class HomeController {
@Autowired
HomeServcies homeServcies;
@RequestMapping(value = "index")
public String Index() {
return "/jsp/index";
}
}
測試運行:
出現神奇的,如圖:
到此,結束了。
剛來北京,沒有錢,只能擼代碼玩了。