Spring——注解
来源:互联网 发布:mac截gif 编辑:程序博客网 时间:2024/06/13 02:35
注解:
* 用来标识的
* 注解是用来标注类里的东西
* 注解不能单独存在,只能依附于类中的结构
* jdk中经常用的几种注解:
@Target
ElementType
TYPE 类和接口
FIELD
METHOD
…
用于说明这个注解所标注的注解用在哪些地方
@Documented
是否出现在帮助文档
@Retention 作用范围
SOURCE 源代码
CLASS 源代码和class文件
RUNTIME 源代码、class文件、运行时
* 自定义注解
* 一个自定义注解必须有
@Target 说明这个注解标注在哪个部位
@Retention 说明注解的作用范围
* @interface 说明是一个注解类
* 注解类中可以有属性
String value();
* 注解解析器
用来解析标注在类上的注解
package cn.itcast.annotation0401;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;/** * 注解类 * @author Administrator * * 提供标注在类的哪些部位 * * 这个注解的作用范围是哪些 * * 在document里(帮助文档里)是否体现注解类 *//** * @Target能标注在注解类型上 * ElementType * TYPE * 类和接口 * FIELD * 属性 * METHOD * 方法 * PARAMETER * 方法中的参数 * CONSTRUCTOR * 构造器 * ANNOTATION_TYPE * 注解类型 * PACKAGE * 包 * 指明了所在的注解类能标注在类的哪些部位 * @Target({ElementType.TYPE,ElementType.METHOD}) * 既可以标注在类上也可以标注在方法上 */@Target(ElementType.TYPE)/** * 是否让@Description这个注解出现在帮助文档中 */@Documented/** * @Retention * 说明@Description注解的作用域 * RetentionPolicy.SOURCE * @Description这个注解在源文件中起作用 * RetentionPolicy.CLASS * @Description这个注解即在源文件中起作用又在class文件中起作用 RetentionPolicy.RUNTIME @Description在源文件、class文件、运行的过程中都起作用 */@Retention(RetentionPolicy.RUNTIME)public @interface Description {String value();//属性}
package cn.itcast.annotation0401;@Description(value="这是一个学生类")public class Student {@Name(aa="这是一个方法")public void say(){System.out.println("aaa");}@Name(aa="说明这个学生来源")public void des(){System.out.println("来自传智播客,good student");}}
package cn.itcast.annotation0401;import java.lang.annotation.Annotation;import java.lang.reflect.Method;import org.junit.Test;/** * 注解解析器 * @author Administrator * */public class StudentParse {@Testpublic void parse() throws Exception{Class classStudent = Class.forName("cn.itcast.annotation0401.Student");if(classStudent.isAnnotationPresent(Description.class)){//如果该注解存在Description description = (Description)classStudent.getAnnotation(Description.class);System.out.println(description.value());}Method[] methods = classStudent.getMethods();System.out.println(methods.length);for(Method method:methods){System.out.println(method.getName());if(method.isAnnotationPresent(Name.class)){//如果该注解存在Name name = (Name)method.getAnnotation(Name.class);System.out.println(name.aa());}}}}
package cn.itcast.spring.annotaion.scan; import javax.annotation.Resource; import org.springframework.stereotype.Component; /** * @Component 等价于 <bean id="person" class=""/> * @component("b") 等价于 <bean id="b" class=""/> * @author Administrator * */ @Component public class Person { @Resource(name="student") private Student student; public void say(){ this.student.say(); } }
package cn.itcast.spring.annotaion.scan; import org.springframework.stereotype.Component; @Component public class Student { public void say(){ System.out.println("student"); } }
- <?xmlversion="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"
- 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">
- <!--
- 1、导入基于注解的xsd
- xmlns:context="http://www.springframework.org/schema/context"
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- 2、导入注解解析器
- <context:component-scanbase-package="cn.itcast.spring.annotaion.scan"></context:component-scan>
- 3、导入person和student
- -->
- <!-- 类扫描的注解解析器
- 原理:
- 类扫描的注解解析器包含了依赖注入的注解解析器
- 当启动spring容器的时候 spring容器会加载配置文件 并且解析配置文件 就会解析到
- 类扫描的注解解析器 就会在base-package包及子包中扫描所有的类 检测类上是否有
- @Compontent注解
- 如果有
- 看@compontent是否有value属性
- 如果没有value属性 则会把 注解所在的类的类名的第一个字母变成小写 其余不变 当作bena的id
- 如果有value属性 这该属性就是bane的id
- 如果没有
- 类扫描注解解析完以后 所有的在base-package包及子包下的@Compontent注解的类就会纳入spring管理
- 在纳入spring管理的类中 扫描各个属性 看属性是否有 @Resource 在根据这个属性的规则进行操作
- 扫描的次数
- 1.根据base-package包及子包进行扫描
- 2.扫描纳入spring管理的所有bean的属性
- 3.属性的name的值和bena中的id进行匹配
- -->
- <context:component-scanbase-package="cn.itcast.spring.annotaion.scan"></context:component-scan>
- </beans>
- package cn.itcast.spring.annotaion.scan;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestAnnotaion {
- @Test
- public void test(){
- ApplicationContext context=new ClassPathXmlApplicationContext("cn/itcast/spring/annotaion/scan/applicationContext.xml");
- Person p=(Person) context.getBean("person");
- p.say();
- }
- }
- Spring——注解
- Spring——注解
- Spring——注解
- spring ——注解
- Spring——注解详解
- Spring 笔记—— Spring 注解
- Annotation——Spring注解开发
- Spring注解配置事务管理——问题
- spring学习——常用注解
- spring中AOP——注解实现
- Spring 注解配置——@Autowired
- spring预热(1)——注解
- Annotation——Spring注解开发
- Spring入门3——注解篇
- Spring MVC 注解——@Autowired
- Spring —— @Transacitonal 注解详解
- Spring事务管理——AOP注解事务管理
- spring—使用注解配置Bean
- secureCRT
- 初识ZigBee
- UEFI HII 模块介绍
- Tomcat配置及介绍
- gtest
- Spring——注解
- 域名的划分
- 新浪微博技术架构分析
- oracle 11g PL/SQL Programming学习八
- oracle用户被锁
- MyGui调用Win32窗口打开保存文件
- JNI基本原理
- codeforces 275A - Lights Out
- 移动设备web自适应布局