深入浅出之Spring第二章注解

来源:互联网 发布:将mdf文件导入数据库 编辑:程序博客网 时间:2024/05/16 00:42

注解是Spring一项重要的功能,它简化了我们使用Spring时配置XML文件的工作量。

注解相对于XML配置有以下几点优势

1:它可以充分的利用Java的反射机制获取类结构的信息,这些信息可以有效的减少配置的工作。

2:注解和JAVA代码位于同一个文件中,而XML配置采用独立的配置文件,

    但是呢,大多数配置信息在程序开发完成后都不会再修改,因此注解所采用的将配置信息和代码放在一起,有助于增强程序的内举行。

    而独立的XML配置文件,程序员在编写程序时需要不停的在程序和配置文件之间进行切换,这样会降低开发效率。

我们要是想使用Spring的注解机制的话,必须引入common-annotations.jar这个jar包。

然后还需要在Spring的配置文件中添加<context:annotation-config />的命名空间

然后引入Spring头文件,然后就可以开始你的Spring的注解之旅了。

<?xml version="1.0" encoding="UTF-8"?>
<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"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
         http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"
      default-lazy-init="true">
 <tx:annotation-driven />
 <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
 <context:component-scan base-package="com.first.controller" />
 <context:component-scan base-package="com.first.logAop" />
 <!-- 启动对@AspectJ注解的支持 -->
 <!--通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Controller--> 
 <aop:aspectj-autoproxy proxy-target-class="true"/>
 <!-- 注解支持 --> 
 <context:annotation-config/>

这里对上面XML配置文件做一下简单的说明。

首先<beans>标签是Spring的根节点,这个是每一个Spring配置文件都有的。

xmlns是xml namespace的缩写,也就是你这个xml文件的命名空间。

xmlns:xsi其中的xsi是xml shcema instance的缩写,也就是说你这个xml文件所要遵循的xml文件的书写规范。

 xmlns:context是对Spring上下文规范的定义。

xmlns:tx是对Spring事务的规范定义。

xmlns:aop是对Spring切面的规范定义。

xsi:schemaLocation这个标签定义了上面所说的各种规范的位置。


下面我们就来开始学习一下关于使用Spring注解时常用的几个标签

@Resource:表示bean之间的依赖关系。

例如下面的例子就是把UserService的bean注入给属性userService

package com.first.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.first.entity.User;
import com.first.service.UserService;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Controller
@RequestMapping("/user")
public class UserController {

 @Resource
 private UserService userService;

}

@PostConstruct:bean初始化完成后执行的操作。

@PreDestroy:bean销毁前执行的操作。

例如下面的例子

package com.first.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.first.entity.User;
import com.first.service.UserService;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Controller
@RequestMapping("/user")
public class UserController {
 
 @PostConstruct
    public void init()
 {
  System.out.println("init");
 }

    @PreDestroy
    public void destory()
    {
     System.out.println("destory");
    }

}

@Repository       持久层
@Service            业务层
@Controller         控制层(Web 层)
@Component      对那些比较中立的类进行注解。

@RequestMapping("/user")  对于请求的注解


0 0