您的位置:首页 > 编程语言 > Java开发

用注解方式实现springMVC

2015-06-24 10:14 609 查看
以前总是在弄Struts+Spring+jdbc、Ibatis实现MVC,最近公司项目换Spring mvc实现方式,从网络上找了一个项目来练手,现在总结一下,mvc无非就是分清M V C 各自功能,而web程序无非就是要解决web容器数据和java程序数据传输问题(就程序开发而言)。如果你理解MVC, 对SSH有基本的了解,学习Spring MVC 就可以套用SSH的基本实现。 在struts中使用 ActionServlet作为请求分发器,依据请求路径信息将请求分派到各个action中,调用基本数据CURD方法。在SSH中,ActionServlet作为Control
, JSP/HTML作为View承担着视图功能,Action作为mode和struts的“适配器”,将请求数据转发给后端的DAO处理。在依据处理结果,跳转到对应的View,其中ActionMapping是对View的抽象。 有了这个基本认识,我们类比类推,spring mvc,在spring mvc中比较重要的两个包就是spring-web.jar spring-context.jar、基本的ioc等jar包。 org.springframework.web.servlet.DispatcherServlet作为Control
,它做的事和ActionServlet差不多。JSP/HTML作为View,在spring最大的亮点莫过于和mode、dao交互的类可以是普通的JAVABEAN,在struts中的action到了spring 中就完全变成了POJO。和Struts2 的Aciton一样,减少了数据侵入性。下面我们就用注解实现Spring mvc。

我们用注解方式实现spring mvc,下面我们要对基本的产使用的注解有个基本的理解,注解的存在使得我们可以减少spirng的xml配置,做的事情和xml配置差不多。所以我们需要的注解就包括三大类:

* bean 属性注入注解,例如 @Autowired 、@Resource

* bean方法执行注解,例如 @PostConstruct 、@PreDesotory

* bean 定义注解,例如@Component 、 @Control 、@Service 、 @Repository等。

@Autowired、@Resource作为常使用的属性注入注解,区别在于:

1、@Autowired 是依据type来自动封装注入的;而@Resource 先按照name,在按照type依赖注入

2、扫描处理注解的处理类不同,要让@Autowired 生效,必须在spring xml配置文件中加入

Java代码


<span style="white-space: normal; background-color: #ffffff;"><bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /></span>

而要让@Resource 生效,必须在spring xml配置文件中加入

Xml代码


<context:annotation-config />

通过<context:annotation-config /> 将隐式的向容器注入AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。

使用Autowired当IOC中拥有两个相同目标类型时候,可以使用@Qualifier指定具体使用那个类,下面代码指定id为userDao的类注入进来:

Java代码


@Autowired

@Qualifier("userDao")

private UserDao userdao;

当IOC容器找不到 Autowired指定的类型时,可以通过如下方式配置,这样就需要在代码中做null判断:

Java代码


@Autowired(required= false)

public void setUserDao(UserDao userDao){

this.userDao = userDao;

}

@PostConstruct 、 @PreDestory 用于定义方法在对象是实例化后执行的方法、和在对象销废之前调用的方法。

例如子类需要实例化父类的变量,就可以再对应的方法中添加@PostConstruct、某些类在内存中销废之前需要释放自己的资源可以使用@PreDestory注解

@Component 、 @Control 、 @Service 、 @Repository 用于向IOC容器中注入bean:

Java代码


@Component

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

...

}

如上代码会在ioc容器中声明id名称为userDaoImpl的bean,如果指定了名称,如@Component("userDao"),那就会在ico容器中声明id为userDao的bean,没有添加参数默认使用的就是非限定类名的骆驼命名法方式定义bean。其他@Control、@Service、@Repository和@Component的作用一样,但他们更加答意,表示控制层、业务层、持久层。

要让上述bean定义注解生效需要在spring配置文件中添加如下xml配置:

Xml代码


<context:component-scan base-package="com.kedacom.ksoa" />

这个处理类会将包下所有添加了注解的类实例化并且注入到IOC。加入类中添加了@Resource、@Autowired等属性注入注解,该类也会一并处理,不需要在添加<context: annocation-config />配置了。base-page表示需要扫描的包,base-package指定的类包及其递归子包中所有的类都会被处理。

<context:component-scan />还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:

过滤器类型 表达式范例 说明
注解 org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来
类名指定 org.example.SomeClass 过滤指定的类
正则表达式 com\.kedacom\.spring\.annotation\.web\..* 通过正则表达式过滤一些类
AspectJ表达式 org.example..*Service+ 通过AspectJ表达式过滤一些类

值得注意的是<context:component-scan />配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用<context:component-scan
/>后,就可以将<context:annotation-config />移除了。

使用@Scope来定义Bean的作用范围

在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:

Java代码


<span style="font-size: x-small;">@Scope("session")

@Component()

public class UserSessionBean implements Serializable {

...

}

</span>

3. 参考
http://kingtai168.iteye.com/blog/244002 http://www.iteye.com/topic/244153 http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanning
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: