- 浏览: 545244 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (618)
- java (109)
- Java web (43)
- javascript (52)
- js (15)
- 闭包 (2)
- maven (8)
- 杂 (28)
- python (47)
- linux (51)
- git (18)
- (1)
- mysql (31)
- 管理 (1)
- redis (6)
- 操作系统 (12)
- 网络 (13)
- mongo (1)
- nginx (17)
- web (8)
- ffmpeg (1)
- python安装包 (0)
- php (49)
- imagemagic (1)
- eclipse (21)
- django (4)
- 学习 (1)
- 书籍 (1)
- uml (3)
- emacs (19)
- svn (2)
- netty (9)
- joomla (1)
- css (1)
- 推送 (2)
- android (6)
- memcached (2)
- docker、 (0)
- docker (7)
- go (1)
- resin (1)
- groovy (1)
- spring (1)
最新评论
-
chokee:
...
Spring3 MVC 深入研究 -
googleyufei:
很有用, 我现在打算学学Python. 这些资料的很及时.
python的几个实用网站(转的) -
hujingwei1001:
太好了找的就是它
easy explore -
xiangtui:
例子举得不错。。。学习了
java callback -
幻影桃花源:
太好了,謝謝
Spring3 MVC 深入研究
原文地址:http://developer.51cto.com/art/201104/255395.htm一、各种注解方式
1.@Autowired注解(不推荐使用,建议使用@Resource)
@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。要使@Autowired能够工作,还需要在配置文件中加入以下
Xml代码
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
2. @Qualifier注解
@Autowired是根据类型进行自动装配的。例如,如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在UserDao类型的bean,也会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。如下:
1). 可能存在多个UserDao实例
Java代码
@Autowired
@Qualifier("userServiceImpl")
public IUserService userService;
或者
Java代码
@Autowired
public void setUserDao(@Qualifier("userDao") UserDao userDao) {
this.userDao = userDao;
}
这样,Spring会找到id为userServiceImpl和userDao的bean进行装配。
2). 可能不存在UserDao实例
Java代码
@Autowired(required = false)
public IUserService userService;
3. @Resource注解
JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解。@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将 @Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。要使@Autowired能够工作,还需要在配置文件中加入以下:
Xml代码
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
@Resource装配顺序:
a.如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
b.如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
c.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
d.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;
4. @PostConstruct(JSR-250)注解
在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。它的一个典型的应用场景是,当你需要往Bean里注入一个其父类中定义的属性,而你又无法复写父类的属性或属性的setter方法时,如:
Java代码
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
private SessionFactory mySessionFacotry;
@Resource
public void setMySessionFacotry(SessionFactory sessionFacotry)
{
this.mySessionFacotry = sessionFacotry;
}
@PostConstruct
public void injectSessionFactory()
{
super.setSessionFactory(mySessionFacotry);
}
}
这里通过@PostConstruct,为UserDaoImpl的父类里定义的一个sessionFactory私有属性,注入了我们自己定义的 sessionFactory(父类的setSessionFactory方法为final,不可复写),之后我们就可以通过调用 super.getSessionFactory()来访问该属性了。
5. @PreDestroy(JSR-250)注解
在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行。其用法同@PostConstruct。和@PostConstruct 区别在于:@PostConstruct注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。
6. @Component注解 (不推荐使用)
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了。Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替代@Component。
7.@Scope注解
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:
Java代码
@Scope("session")
@Component()
public class UserSessionBean implements Serializable{
... ...
}
二、配置启用注解(注意以下配置需要使用spring2.5的头文件,在spring3.0中不适用)
1.使用简化配置
Spring2.1添加了一个新的context的Schema命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。但是直接在Spring配置文件中定义这些Bean显得比较笨拙。Spring为我们提供了一种方便的注册这些BeanPostProcessor的方式,这就是,以下是spring的配置。
Xml代码
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
beans>
将隐式地向Spring容器注册了
AutowiredAnnotationBeanPostProcessor 、
CommonAnnotationBeanPostProcessor 、
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
这4个BeanPostProcessor。
2.使用让Bean定义注解工作起来
Xml代码
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.kedacom.ksoa" />
beans>
这里,所有通过元素定义Bean的配置内容已经被移除,仅需要添加一行配置就解决所有问题了——Spring XML配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:
过滤器类型 | 表达式范例 | 说明
注解 | org.example.SomeAnnotation | 将所有使用SomeAnnotation注解的类过滤出来
类名指定 | org.example.SomeClass | 过滤指定的类
正则表达式 | com\.kedacom\.spring\.annotation\.web\..* | 通过正则表达式过滤一些类
AspectJ表达式 | org.example..*Service+ | 通过AspectJ表达式过滤一些类
以正则表达式为例,我列举一个应用实例:
Xml代码
<context:component-scan base-package="com.casheen.spring.annotation">
<context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
context:component-scan>
值得注意的是配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用后,就可以将移除了。
3.
是不支持spring的@Transcation和EJB的Spring's @Transactional or EJB3's @TransactionAttribute annotation。用此配置可以达到目的。
4. 使用@Scope来定义Bean的作用范围
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:
Java代码
@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
...
}
1.@Autowired注解(不推荐使用,建议使用@Resource)
@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。要使@Autowired能够工作,还需要在配置文件中加入以下
Xml代码
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
2. @Qualifier注解
@Autowired是根据类型进行自动装配的。例如,如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在UserDao类型的bean,也会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。如下:
1). 可能存在多个UserDao实例
Java代码
@Autowired
@Qualifier("userServiceImpl")
public IUserService userService;
或者
Java代码
@Autowired
public void setUserDao(@Qualifier("userDao") UserDao userDao) {
this.userDao = userDao;
}
这样,Spring会找到id为userServiceImpl和userDao的bean进行装配。
2). 可能不存在UserDao实例
Java代码
@Autowired(required = false)
public IUserService userService;
3. @Resource注解
JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解。@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将 @Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。要使@Autowired能够工作,还需要在配置文件中加入以下:
Xml代码
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
@Resource装配顺序:
a.如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
b.如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
c.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
d.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;
4. @PostConstruct(JSR-250)注解
在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。它的一个典型的应用场景是,当你需要往Bean里注入一个其父类中定义的属性,而你又无法复写父类的属性或属性的setter方法时,如:
Java代码
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
private SessionFactory mySessionFacotry;
@Resource
public void setMySessionFacotry(SessionFactory sessionFacotry)
{
this.mySessionFacotry = sessionFacotry;
}
@PostConstruct
public void injectSessionFactory()
{
super.setSessionFactory(mySessionFacotry);
}
}
这里通过@PostConstruct,为UserDaoImpl的父类里定义的一个sessionFactory私有属性,注入了我们自己定义的 sessionFactory(父类的setSessionFactory方法为final,不可复写),之后我们就可以通过调用 super.getSessionFactory()来访问该属性了。
5. @PreDestroy(JSR-250)注解
在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行。其用法同@PostConstruct。和@PostConstruct 区别在于:@PostConstruct注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。
6. @Component注解 (不推荐使用)
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了。Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替代@Component。
7.@Scope注解
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:
Java代码
@Scope("session")
@Component()
public class UserSessionBean implements Serializable{
... ...
}
二、配置启用注解(注意以下配置需要使用spring2.5的头文件,在spring3.0中不适用)
1.使用简化配置
Spring2.1添加了一个新的context的Schema命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。但是直接在Spring配置文件中定义这些Bean显得比较笨拙。Spring为我们提供了一种方便的注册这些BeanPostProcessor的方式,这就是,以下是spring的配置。
Xml代码
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
beans>
将隐式地向Spring容器注册了
AutowiredAnnotationBeanPostProcessor 、
CommonAnnotationBeanPostProcessor 、
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
这4个BeanPostProcessor。
2.使用让Bean定义注解工作起来
Xml代码
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.kedacom.ksoa" />
beans>
这里,所有通过元素定义Bean的配置内容已经被移除,仅需要添加一行配置就解决所有问题了——Spring XML配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:
过滤器类型 | 表达式范例 | 说明
注解 | org.example.SomeAnnotation | 将所有使用SomeAnnotation注解的类过滤出来
类名指定 | org.example.SomeClass | 过滤指定的类
正则表达式 | com\.kedacom\.spring\.annotation\.web\..* | 通过正则表达式过滤一些类
AspectJ表达式 | org.example..*Service+ | 通过AspectJ表达式过滤一些类
以正则表达式为例,我列举一个应用实例:
Xml代码
<context:component-scan base-package="com.casheen.spring.annotation">
<context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
context:component-scan>
值得注意的是配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用后,就可以将移除了。
3.
是不支持spring的@Transcation和EJB的Spring's @Transactional or EJB3's @TransactionAttribute annotation。用此配置可以达到目的。
4. 使用@Scope来定义Bean的作用范围
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:
Java代码
@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
...
}
发表评论
-
java的InputStream和OutputStream的理解【转】
2015-11-09 18:10 624原文地址:http://www.cnblogs.com/spr ... -
【JSP】让HTML和JSP页面不缓存的方法
2015-10-14 10:16 448原文地址:http://blog.csdn.net/juebl ... -
jsp去掉浏览器缓存
2015-10-14 09:21 591原文地址:http://bbs.csdn.net/topics ... -
Spring定时任务的几种实现
2015-09-17 18:02 359原文地址:http://gong1208.iteye.com/ ... -
Java 8 简明教程
2015-08-31 17:43 325原文地址:http://www.iteye.com/magaz ... -
Java 8 简明教程
2015-08-28 15:30 540原文地址:http://www.iteye.com/magaz ... -
Apache所有项目介绍
2015-08-20 11:47 917原文地址:http://haisha.iteye.com/bl ... -
jdk5.0新特性介绍
2015-08-04 18:08 445原文地址:http://blog.sina.com.cn/s/ ... -
Apache Log4j配置说明
2015-05-18 15:59 388原文地址:http://zhangjunh ... -
ubuntu 14.04 下通过apt-get 安装jdk
2015-04-09 16:42 663原文地址:http://segmentfault.com/a/ ... -
【原创】Eclipse Class Decompiler——Java反编译插件
2015-04-01 15:00 575原文地址:http://www.blogj ... -
jvisualvm远程监控Tomcat
2015-03-06 10:19 682原文地址:http://ihuangweiwei.iteye. ... -
Java 并发核心编程
2015-01-08 18:07 591原文地址:http://www.cnblogs.com/see ... -
log4j日志文件乱码问题的解决方法
2015-01-06 18:11 787原文地址:http://blog.csdn.net/inkfi ... -
SHA1与MD5
2014-12-22 15:31 574原文地址:http://blog.csdn.net/fogle ... -
征服 Redis + Jedis
2014-12-19 13:51 371原文地址:http://snowolf.iteye.com/b ... -
Java连接redis的使用示例
2014-12-19 12:08 1035原文地址:http://blog.csdn.net/wgw33 ... -
java对redis的基本操作
2014-12-19 12:07 555原文地址:http://www.cnblogs.com/edi ... -
Eclipse Class Decompiler——Java反编译插件
2014-10-16 11:02 434原文地址:http://bbs.csdn. ... -
Eclipse上Java反编译插件Java Decompiler的安装与使用
2014-10-16 11:00 832原文地址:http://my.oschina.net/Pipi ...
相关推荐
Spring3.0注解注入详解[总结].pdf
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235287
基于spring3.0依赖注入详解,体验新特性。
spring3.0MVC注解(附实例). spring3.0MVC注解(附实例). spring3.0MVC注解(附实例).
spring3.0 全部jar包 spring3.0 全部jar包 spring3.0 全部jar包
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
《Spring 3.0就这么简单》语言简洁,实例丰富,可帮助读者迅速掌握使用Spring3.0进行开发所需的各种技能。《Spring3.0就这么简单》适合于具有一定Java编程基础的读者,以及在Java平台下进行各类软件开发的开发人员和...
spring3.0 项目源码 spring3.0 注解 的学习,spring mvc 完整项目, 请你好好学习一下!
spring3.0MVC注解:spring3.0MVC注解spring3.0MVC注解spring3.0MVC注解
spring3.0 开发包,开发J2ee,值得拥有
《Spring3.0就这么简单》主要介绍了Spring3.0的核心内容,不仅讲解了Spring3.0的基础知识,还深入讨论了SpringIoC容器、SpringAOP、使用SpringJDBC访问数据库、集成Hibernate、Spring的事务管理、SpringMVC、单元...
spring3.0MVC注解(附实例)
采用spring3.0 mvc 注解的方式,在页面显示helloworld
Spring 3.0重要特性总结如下: ◆Spring表达式(SpEL):用于bean定义的核心表达式分析器 ◆对基于注释的组件的更多支持:允许通过元注释创建注释的“快捷方式” ◆标准化的依赖性注入注释:对Java中依赖性注入的...
spring3.0的API,spring3.0的API
spring3.0 jar包
spring3.0 apispring3.0 apispring3.0 apispring3.0 apispring3.0 apispring3.0 apispring3.0 apispring3.0 apispring3.0 apispring3.0 apispring3.0 apispring3.0 apispring3.0 api
Struts2.1+Spring3.0+JPA1.0 .pdfStruts2.1+Spring3.0+JPA1.0 .pdfStruts2.1+Spring3.0+JPA1.0 .pdfStruts2.1+Spring3.0+JPA1.0 .pdfStruts2.1+Spring3.0+JPA1.0 .pdfStruts2.1+Spring3.0+JPA1.0 .pdf
spring 3.0 jar包,下载无需资源分,希望大家能够用得到,另外强烈鄙视那些上传资源不完整,不经过测试的人。