//Replacer 需要實現MethodReplacer接口,並重寫reimplement方法
//會替換MyBean的getTestStr方法爲Replacer類的reimplement方法
<bean id="MyBean" class="com.qmylzx.bean.MyBean">
<replaced-method name="getTestStr" replacer="Replacer"></replaced-method>
</bean>
<bean id="Replacer" class="com.qmylzx.bean.Replacer"></bean>
//這裏MyAbstractClass爲抽象類 getBean爲抽象方法
//lookup-method 使getBean方法返回的是MyBean對象
<bean id="MyAbstractClass" class="com.qmylzx.bean.MyAbstractClass">
<lookup-method bean="MyBean" name="getBean"></lookup-method>
</bean>
SpringMVC.xml配置標籤介紹
<!-- 開始組件掃描 -->
<context:component-scan base-package="com.qmylzx.ssm"></context:component-scan>
web.xml中 urlmapping 只能配置爲 *.do *.xx 帶有後綴的才能被controller攔截
如果將DispatcherServlet請求映射配置爲"/",則Spring MVC將捕獲Web容器所有的請求,包括靜態資源的請
求,Spring MVC會將它們當成一個普通請求處理,因此找不到對應處理器將導致錯誤。
<!-- 處理靜態資源 -->
<mvc:default-servlet-handler/>
當配置這個選項後, web.xml中可以把 dispatcherServlet的mapping配置爲 /
即可以訪問靜態資源 但是會導致其它的正常請求的controller找不到,所有請求都會直接給
default-servlet處理,必須配置
<!-- 啓用註解驅動 -->
<mvc:annotation-driven></mvc:annotation-driven>
纔可以找到正常請求的controller。
靜態資源還可以用
<mvc:resources /> //它可以將靜態資源放到任意位置,而不像上一個標籤只能放到web-info下
配置location屬性
"/,classpath:/META-INF/publicResources/" mapping="/resources/**"
表示以上配置將Web根路徑"/"及類路徑下 /META-INF/publicResources/ 的目錄映射爲/resources路徑
網頁中直接訪問是訪問不到的
1、描述有錯誤:
直接在網頁中寫:<img src="/resources/images/bg1.gif" />這樣是不行的,後臺會認爲它是"http://xxx.xxx.xx.xx:xxxx/resources/images/bg1.gif"。
2、正確用法:
2.1 先引入jstl標籤庫
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
2.2 再引入靜態資源
<img src="<c:url value='/resources/images/bg1.gif'/>"/>
配置了context:component-scan ,<context:annotation-config/>就不用配置
-----------------------------------------------------------------------
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.qmylzx.ssm.intercept.LTIntercept"/> <!--禁止直接訪問後臺(沒有登陸的情況下)-->
</mvc:interceptor>
</mvc:interceptors>
<!--全局異常處理器-->
<!-- 全局異常處理器,只要實現HandlerExceptionResolver接口就是全局異常處理器-->
<bean id="handlerExceptionResolver" class="com.qmylzx.ssm.exception.MyExceptionResolver"></bean>
-------------------------------------------------------------------------
@Controller 標記類
@RequestParam :將請求的參數綁定到方法中的參數上,有required參數,默認情況下,required=true,也就是改參數必須要傳。如果改參數可以傳可不傳,可以配置required=fals
@RequestMapping("/happy")
public String sayHappy(
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "age", required = true) String age) {
//age參數必須傳 ,name可傳可不傳
...
}
@RequestMapping 標記類、方法
@RequestMapping (value= "testParams" , params={ "param1=value1" , "param2" , "!param3" })
public String testParams() {
System. out .println( "test Params..........." );
return "testParams" ;
}
@RequestMapping 的params 屬性指定了三個參數,這些參數都是針對請求參數而言的,它們分別表示參數
param1 的值必須等於value1 ,參數param2 必須存在,值無所謂,參數param3 必須不存在,只有當請
求/testParams.do 並且滿足指定的三個參數條件的時候才能訪問到該方法。所以當請求/testParams.do?
param1=value1¶m2=value2 的時候能夠正確訪問到該testParams方法
method 參數限制了以GET 或DELETE 方法請求/testMethod 的時候才能訪問到該Controller的
testMethod 方法
headers={ "host=localhost" , "Accept" }
當請求/testHeaders.do 的時候只有當請求頭包含Accept 信息,且請求的host 爲localhost 的時候
才能正確的訪問到testHeaders 方法
@PathVariable : 該註解用於方法修飾方法參數,會將修飾的方法參數變爲可供使用的uri變量(可用於動態
綁定)。
@RequestMapping(value="/admin/{id}",method=RequestMethod.GET)
public String findPet(@PathVariable("id") String userid) {
// 這裏 id = request.getParameter("id"); ,已經賦值
}
@ResponseBody 在輸出JSON格式的數據時,會經常用到 標記方法
@ModelAttribute :@ModelAttribute可以作用在方法或方法參數上,當它作用在方法上時,標明該方法的目
的是添加一個或多個模型屬性(model attributes)。
該Controller的所有方法在調用前,先執行此@ModelAttribute方法,並且返回值會綁定到ModelAttribute的
參數上,然後方法參數中標記直接賦值給參數,
@ModelAttribute("id")
public String t(){
return "22";
}
public String findPet(@ModelAttribute ("id") String userid) {
// 直接使用 ,這裏userid = 22;
}
@SessionAttributes即將值放到session作用域中,寫在class上面
@SessionAttributes (value={ "intValue" , "stringValue" }, types={User. class })
表示會把對象寫到session中
@Resource 默認按名稱注入 也可按類型 javax的註解,spring兼容
@Autowired 按類型注入,配合Qualifier 可以按名稱注入 ,spring註解
可用於類的屬性、構造器、方法
@Component、@Repository、@Service、@Controller @Component可以代替後三個,後三個功能細化
@Bean
Bean註解主要用於方法上,有點類似於工廠方法,當使用了@Bean註解,我們可以連續使用多種定義bean時用到
的註解,譬如用@Qualifier註解定義工廠方法的名稱,用@Scope註解定義該bean的作用域範圍,譬如是
singleton還是prototype等
@Configuration
Java 配置支持的核心 註解類表示類可以被 Spring 的 IoC 容器所使用,
作爲 bean 定義的資源
@Component 可以替代Configuration
區別
@Configuration
public static class Config {
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new SimpleBeanConsumer(simpleBean());
}
} //SimpleBeanConsumer將會得到一個單例SimpleBean的鏈接
@Component
public static class Config {
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new SimpleBeanConsumer(simpleBean());
}
}//Spring會創建一個SimpleBean的單例bean,但是SimpleBeanConsumer將獲得另一個SimpleBean實例
//可以這樣解決區別
@Component
public static class Config {
@Autowired
SimpleBean simpleBean;
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new SimpleBeanConsumer(simpleBean);
}
}
handler method 參數綁定常用的註解,我們根據他們處理的Request的不同內容部分分爲四類:(主要講解常用類型)
A、處理requet uri 部分(這裏指uri template中variable,不含queryString部分)的註解: @PathVariable;
B、處理request header部分的註解: @RequestHeader, @CookieValue;
C、處理request body部分的註解:@RequestParam, @RequestBody;
D、處理attribute類型是註解: @SessionAttributes, @ModelAttribute;
@RequestHeader、@CookieValue
@RequestHeader 註解,可以把Request請求header部分的值綁定到方法的參數上。
示例代碼:
這是一個Request 的header部分:
Host localhost:8080
Accept text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
}
@RequestBody
該註解常用來處理Content-Type: 不是application/x-www-form-urlencoded編碼的內容,例如application/json, application/xml等;
service 層註解
<!-- 開啓註解方式聲明事務 -->
<tx:annotation-driven transaction-manager="transactionManager" />
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class) 使用事務