然後選擇建立Maven 的webapp執行個體,當然也可以通過命令列方式建立Maven webapp的項目再轉化並匯入到MyEclipse中。
在pom.xml中需要對於Spring依賴項進行配置:
<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>wx.spring</groupId><artifactId>wx.spring.helloworld</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>wx.spring.helloworld Maven Webapp</name><url>http://maven.apache.org</url><build><finalName>wx.spring.helloworld</finalName></build><properties><spring.version>4.0.6.RELEASE</spring.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- Spring dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</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></dependencies></project>
2.架構配置(Spring MVC)
在本範例程式碼中,對於Spring MVC架構採取xml配置的方式,而對於IOC操作,採取Java Based Annotated方式。
l web.xml
web.xml是整個項目的總體設定檔,在該檔案中需要制定項目Servlet的匹配方式以及整個項目的內容相關的設定檔。在該web.xml檔案中配置了一個Servlet,該Servlet是Spring MVC所需要的。而如果需要在Struts等其他架構中使用Spring,就需要聲明一個listener。換言之,dispatcher-servlet與applicationContext是應用於不同情境的相同的設定檔。
<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>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-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>
</web-app>
2 dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:component-scan base-package="wx.spring.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
3 applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:component-scan base-package="wx.spring.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
3.代碼
1 HelloWorldController
package wx.spring.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public String hello(
@RequestParam(value = "name", required = false, defaultValue = "World") String name,
Model model) {
model.addAttribute("name", name);
return "helloworld";
}
}
2 /WEB-INF/views/helloworld.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring4 MVC -HelloWorld</title>
</head>
<body>
<h1>Hello : ${name}</h1>
</body>
</html>
3 MessagePrinter.java:服務介面類,具體的功能實現依賴於MessageService介面。
該類中的MessageService成員,會在運行時由Spring自動注入,而不需要自身初始化。
package wx.spring.helloworld.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessagePrinter {
private MessageService service;
@Autowired
public void setMessageService(MessageService service) {
this.service = service;
}
public void printMessage() {
System.out.println(this.service.getMessage());
}
}
4 MessageService:介面類
package wx.spring.helloworld.bean;
public interface MessageService {
String getMessage();
}
5 MyConfiguration:配置類
package wx.spring.helloworld.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import wx.spring.helloworld.bean.MessagePrinter;
import wx.spring.helloworld.bean.MessageService;
@Configuration
@ComponentScan(value="wx.spring.helloworld.test")
public class MyConfiguration {
@Bean
MessageService getMessageService() {
return new MessageService() {
public String getMessage() {
return "Hello World!IOC&DI";
}
};
}
@Bean
MessagePrinter getMessagePrinter(){
return new MessagePrinter();
}
}
6 Application
package wx.spring.helloworld.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import wx.spring.helloworld.bean.MessagePrinter;
import wx.spring.helloworld.bean.MessageService;
import wx.spring.helloworld.config.MyConfiguration;
public class Application {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(MyConfiguration.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();
}
}
spring主要內容是:IOC AOP 交易管理 遠程調用 最主要的是前面三個。 你弄個小項目熟悉下就基本會用了。 spring設計的初衷就是為了方便開發設計,他得另一個有點就是對系統是非侵入式起作用。 spring上手是比較容易的,但要精通那就需要好好花點時間了。 建議看看spring in action這本書
怎用spring mvc 編寫日誌 有日誌模板表
Spring是一個開源架構,為瞭解決公司專屬應用程式程式開發的複雜性而建立的。該架構的主要優勢之一就是其分層架構,分層架構允許您選擇使用哪一個組件,提供了一個整合架構的J2EE應用程式開發。
今年春季系列包括三個部分 - 第1部分中,我將介紹Spring架構。我將開始與架構底層模型的角度來看,來描述該架構的功能,然後將討論兩個最有趣的模組:Spring面向方面的編程(AOP)和控制反轉(IOC)容器。下一步將使用幾個樣本示範IOC容器在典型的應用程式的使用方式。這些例子也將作為一個基礎的系列討論後在擴大,在本文的後半部分將介紹Spring架構通過Spring AOP AOP構造。
請參閱下載,下載Spring架構和Apache Ant和運行應用程式範例,他們需要在這個系列。
Spring架構
Spring架構是一個分層架構,由7個定義良好的模組組成的。是建立在該模組的核心容器的頂部,Spring核心容器中定義的建立,配置和管理bean的方式,如在圖1中所示。
圖1。 Spring架構的7個模組
Spring架構的表徵圖
每個模組(或組件)使Spring架構可以單獨,或與其他一個或多個模組來實現組合。每個模組的功能如下:
??核心容器:核心容器提供Spring架構的準系統。核心容器的主要組件是BeanFactory,它是原廠模式的實現。分離BeanFactory的控制反轉(IOC)模式的應用程式的配置和依賴性規範與實際應用程式代碼。
??Spring上下文:Spring上下文是一個設定檔,向Spring架構提供上下文資訊。 Spring上下文包括企業服務,例如JNDI,EJB,電子郵件,國際化,校正和調度功能。
??Spring AOP的組態管理特性,Spring AOP模組直接將面向方面的編程功能整合到了Spring架構中。所以,你可以很容易地使Spring架構的任何對象支援AOP。 Spring AOP模組提供了交易管理服務是基於Spring應用中的對象。使用Spring AOP,不依賴於EJB組件的情況下,可以聲明式交易管理整合到應用程式中。
??的Spring DAO:JDBC DAO抽象層提供了有意義的異常階層,可用來管理異常處理和不同資料庫供應商拋出的錯誤訊息的的結構。的異常階層簡化了錯誤處理,並且極大地降低了異常,你需要寫的代碼數量(例如開啟和關閉串連)。的Spring DAO的面向JDBC的異常遵從通用的DAO異常階層。
??*春ORM:Spring架構插入一些ORM架構提供了ORM的對象關係工具,其中包括JDO,Hibernate和iBatis SQL Map的。所有這些都遵從Spring的通用事務和DAO異常階層。
??的Spring Web模組:Web上下文模組建立在應用程式上下文模組之上,為基於Web的應用程式提供了上下文。所以,Spring架構支援與Jakarta Struts的整合。 Web模組還簡化了處理多部分請求以及將請求參數綁定到域對象。
??* Spring MVC架構:MVC架構是一個全功能的構建Web應用程式的MVC實現。通過策略介面,MVC架構,MVC容納了大量視圖技術,包括JSP,速度,瓷磚,iText的,和POI成為高度可配置的。
Spring架構的功能可以用在任何J2EE伺服器中,大多數功能也適用於非管理環境。 Spring的核心內容:支援不綁定到特定J2EE服務的可重用業務和Data Access Objects。毫無疑問,這個對象在不同J2EE環境(Web或EJB),......餘下全文>>