栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JavaWeb Spring注解Annotation深入学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaWeb Spring注解Annotation深入学习

一、注解

注解Annotation,是一种类似注释的机制,在代码中添加注解可以在之后某时间使用这些信息。跟注释不同的是,注释是给我们看的,java虚拟机不会编译,注解也是不编译的,但是我们可以通过反射机制去读取注解中的信息。注解使用关键字@interface,继承java.lang.annotition.Annotition

1、javaSE中的注解

先举个例子来回顾一下在javaSE中注解是什么东东,关键是两点,注解的定义与如何通过反射得到注解上面的信息。

1.先定义两个注解一个是在类上有注解ClassInfo,一个是在方法上有注解为MethodInfo.

ClassInfo

package com.itheima10.annotation;

import java.lang.annotation.documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE) //该注解可以用于类上
@Retention(RetentionPolicy.RUNTIME) //在java,class文件以及运行时注解都起作用
@documented //能生成在帮助文档中
public @interface ClassInfo {
 
 String name() default "";
 String value() default "";
}

MethodInfo

package com.itheima10.annotation;

import java.lang.annotation.documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD) //该注解可以用于方法上
@Retention(RetentionPolicy.RUNTIME) //在java,class文件以及运行时注解都起作用
@documented //能生成在帮助文档中
public @interface MethodInfo {
 
 String name() default "";
 String value() default "";
}

2.写一个类AnnotationUse来使用上面定义的注解

package com.itheima10.annotation;

@ClassInfo(name="小平果118",value="牛")
public class AnnotationUse {
 @MethodInfo(name="java",value="spring框架很重要")
 public void java(){

 }
}

3.编写测试类AnnotationTest,解析上述两个注解上面的属性

package com.itheima10.annotation;

import java.lang.reflect.Method;

import org.junit.Test;

public class AnnotationTest {
 public static void test(){
  
  Class class1 = Itheima10.class;
  //判断类上面是否有ClassInfo注解
  if(class1.isAnnotationPresent(ClassInfo.class)){
   //得到类上面的注解
   ClassInfo classInfo = (ClassInfo)class1.getAnnotation(ClassInfo.class);
   System.out.println(classInfo.value());
   System.out.println(classInfo.name());
  }

  Method[] methods = class1.getMethods();
  for (Method method : methods) {
   //正在遍历的方法上面是否存在MethodInfo注解
   if(method.isAnnotationPresent(MethodInfo.class)){
    MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
    System.out.println(methodInfo.name());
    System.out.println(methodInfo.value());
   }
  }
 }

 @Test
 public void test(){
  AnnotationTest.test();
 }
}

2、spring中的注解

spring框架为我们提供了注解功能。

使用注解编程,主要是为了替代xml文件,使开发更加快速。但是,xml文件的使用就是解决修改程序修改源代码,现在又不去使用xml文件,那么不就违背了开闭原则了么,得确是。不过么,注解也有注解的好,使用注解就不用配置那么多的xml文件啦,最重要的是开发效率高。。

在没有使用注解时,spring框架的配置文件applicationContext.xml文件中需要配置很多的标签,用来声明类对象。使用注解,则不必在配置文件中添加标签拉,对应的是在对应类的“注释”位置添加说明。具体介绍如下:
 •1.@Resource 对象间关系的组合,默认采用的是按名称方式进行装配,如果根据名称查找不到关联的对象,那么会再采用按类型继续查找。如果没有指定name属性,

• 当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象
• 当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。

• 注意:如果没有指定name属性,并且按照默认的名称找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。

 •2. @Autowired
@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。 解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。

 •3、 @Qualifier
如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。 

1、使用注解,需要在配置文件中增加命名空间和约束文件步骤:

引入context命名空间

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           ...
                      http://www.springframework.org/schema/context/spring-context-2.5.xsd">

2、 在配置文件中加入context:annotation-config标签

实例演示:

编写一个Person类,其中有一个student属性,以及一个say()方法,代码如下

package com.itheima10.spring.di.annotation;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;


public class Person {
 @Resource(name="student")
 private Student student;

 public void say(){
  this.student.say();
 }
}

Student类代码如下

package com.itheima10.spring.di.annotation;

public class Student {
 public void say(){
  System.out.println("student");
 }
}

配置applicationContext.xml文件



 
 
 
 
  
  


编写测试类AnnotationTest

package com.itheima10.spring.di.annotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AnnotationTest {
 @Test
 public void testAnnotation(){
  ApplicationContext context = 
     new ClassPathXmlApplicationContext("applicationContext.xml");
  Person person = (Person)context.getBean("person");
  person.say();
 }
}

如果使用注解,就不需要在配置文件中装载person和student了,这样就可以简化配置文件的编写。

3、 扫描

前面的例子我们都是使用XML的bean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找及维护起来也不太方便。spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件是一样的。要使用自动扫描机制,我们需要打开以下配置信息:

1、引入context命名空间

在xml配置文件中添加context:component-scan标签

其中base-package为需要扫描的包(含子包)。

实例:
将上述实例用扫描的方式书写如下

@Component
public class Person {
 @Resource(name="student")
 private Student student;

 public void say(){
  this.student.say();
 }
}

@Component
public class Student {
 public void say(){
  System.out.println("student");
 }
}

applicationContext.xml只需配置一句话

 
  

编写测试类AnnotationTest


public class AnnotationTest {
 @Test
 public void testAnnotation(){
  ApplicationContext context = 
     new ClassPathXmlApplicationContext("applicationContext.xml");
  Person person = (Person)context.getBean("person");
  person.say();
 }
}

实例再现

我们将Item51中最后的文档管理系统用注解的方式改一下,document接口不变,有read和write方法,实现类分别如下Exceldocument ,PDFdocument ,Worddocument 。

@Component("exceldocument")
public class Exceldocument implements document{

 public void read() {
  System.out.println("excel read");
 }
 public void write() {
  System.out.println("excel write");
 }

}

@Component("pdfdocument")
public class PDFdocument implements document{

 public void read() {
  System.out.println("pdf read");
 }
 public void write() {
  System.out.println("pdf write");
 }

}

@Component("worddocument")
public class Worddocument implements document{

 public void read() {
  System.out.println("word read");
 }
 public void write() {
  System.out.println("word write");
 }

}

documentManager

@Component("documentManager")
public class documentManager {
 @Resource(name="exceldocument")
 private document document;

 public void read(){
  this.document.read();
 }

 public void write(){
  this.document.write();
 }
}

配置文件

   
   

编写测试类documentTest

public class documentTest {
 @Test
 public void testdocument(){
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  documentManager documentManager = (documentManager)context.getBean("documentManager");
  documentManager.read();
  documentManager.write();
 }
}

2、其他注解功能介绍
@Service用于标注业务层组件、服务层注解
@Controller用于标注控制层组件(如struts中的action)、控制层注解
@Repository用于标注数据访问组件,即DAO组件。持久层注解

而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

实例重现–MVC案例

我们再次回顾Item51中的MVC案例,分别将PersonDaoImpl ,PersonAction ,PersonServiceImpl 的Dao,Service,Action层加上注解有

@Repository("personDao") 
public class PersonDaoImpl implements PersonDao {
 @Override
 public void savePerson() {
  System.out.println(" save person");
 }
}

@Service("personService")
public class PersonServiceImpl implements PersonService{
 @Resource(name="personDao")
 private PersonDao personDao;

 public void setPersonDao(PersonDao personDao) {
  this.personDao = personDao;
 }
 @Override
 public void savePerson() {
  this.personDao.savePerson();

 }

}

@Controller("personAction")
public class PersonAction {
 @Resource(name="personService")
 private PersonService personService;

 public void setPersonService(PersonService personService) {
  this.personService = personService;
 }

 public void savePerson(){
  this.personService.savePerson();
 }
}

编写测试MVCTest

public class MVCTest {
 @Test
 public void testMVC(){
  ApplicationContext context = 
     new ClassPathXmlApplicationContext("applicationContext.xml");
  PersonAction personAction = (PersonAction)context.getBean("personAction");
  personAction.savePerson();
 }
}

4. spring中的继承

Spring支持继承,可以分为类继承和属性继承

1. 类继承

Spring属性:
(1)abstract: 如果设置为true,表示定义的bean是抽象的,告诉spring不要实例化这个bean;
问题:必须是抽象类么?可以不是抽象类么?
(2)parent: 指明bean的id,对bean的作用,相当于extends对于java类的作用;

场景:有三个Bean:

 
  
 
 
  
 
 
  
 

修改:定义spring 父bean

 
  
 

定义子Bean

   继承父Bean的属性
    
   覆盖父Bean的属性
  
 

子bean可以继承父Bean的属性,也可以覆盖父Bean的属性

2. 属性继承

几个不同Bean之间存在相同的属性,可以抽离出来
场景:


 
 


 


修改:(1) 抽取公共属性

 
  
 

(2)bean修改

 
  
 
 

这里bean同时有parent和class属性,其中parent指向的baseSex,就是为了让不同Bean之间共享相同的属性值;在TransactionProxyFactoryBean声明业务时,Bean属性继承能够明显的减少冗余的xml配置。

基于注解的继承无需要parent属性。

最后上一张小小的总结图吧

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/149202.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号