//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) 使用事务