说到注解,自己已经忘了很干净流量,所以又回头复习了一下,在这里举个小列子看一下。
创建一个作用在类上的注解:只能在类上调用
@Target(ElementType.TYPE)/*只能标注在类上*/
@Retention(RetentionPolicy.RUNTIME) /*作用域范围*/
public @interface CLassInfo {
String name() default ""; /*注解有name属性,默认为空 */
}
创建一个作用在方法上注解:只能在方法上调用
@Target(ElementType.METHOD)/*作用在方法上*/
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodInfo {
String name() default "";
}
测试类:
@CLassInfo(name = "这就是类")
public class AnnoationClass {
@MethodInfo(name = "这就是方法")
public void java() {
}
}
注解解析类:
public class AnnoationParse {
public static void parse() {
Class clazz = AnnoationClass.class;
//该类上是否存在ClassInfo注解
if(clazz.isAnnotationPresent(CLassInfo.class)) {
//获取类上面的ClassInfo注解
CLassInfo classInfo = (CLassInfo) clazz.getAnnotation(CLassInfo.class);
System.out.println(classInfo.name());
}
Method[] methods = clazz.getMethods();
for (Method method : methods) {
//判断该方法上是否存在MethodInfo注解
if(method.isAnnotationPresent(MethodInfo.class)) {
MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
System.out.println(methodInfo.name());
}
}
}
public static void main(String[] args) {
AnnoationParse.parse();
}
}
以上简单回顾了一下注解,注解是很重要的,建议好好学一下。
下面我们来看一下在spring中运用注解
首先看一下如何使用注解实现属性的赋值
建立Person类:
public class Person {
private Student student;
public Student getStudent() {
return student;
}
}
此前,我们用的xml方式进行给student属性赋值,通过bean和property标签进行赋值。这里我们用一个@Resource(name=”student”)这样一个注释进行赋值;那么这个注解是什么意思呢?
spring 解析@resource注解的name属性,如果name属性为空,说明该注解没有写name属性,spring容器会的得到该注解所在属性的名称(student)和spring容器中的id做比对,如果匹配成功则赋值,如果匹配不成功,则按照类型进行匹配。如果name属性不为空,则按照name属性的值和Spring的id值做匹配,匹配成功赋值,匹配不成功,报错。
如果使用注解方式进行赋值,我们需要在applicationContext,xml中添加一些
配置。
修改xml的头信息
<?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"
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">
这里直接复制到xml文件即可;
此外,加上
<context:annotation-config></context:annotation-config>
这是一个依赖注入的注解解析器
总结一下xml的书写:
<?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"
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">
<!-- 注解实现ioc
<bean id = "person" class = "com.my.spring.di.annoation.Person"></bean>
<bean id = "student" class = "com.my.spring.di.annoation.Student"></bean>-->
<!-- 启动依赖注入的 注解解析器
<context:annotation-config></context:annotation-config> -->
</beans>
总结一下注解实现Ioc的执行步骤:(牢记)
/**
* 注解原理:
*1. 启动spring容器时候,创建啦两个对象(Person和Student对象)
*2. 当spring 容器解析到时候
* spring容器会在spring容器管理的bean的范围内查找这些对象的属性上是否加了@额Resource注解
*3. spring 解析@resource注解的name属性,
* 如果name属性为空,说明该注解没有写name属性
* spring容器会的得到该注解所在属性的名称(student)和spring容器中的id做比对 ,、
* 如果匹配成功则赋值,
* 如果匹配不成功,则按照类型进行匹配
* 如果name属性不为空:
* 则按照name属性的值和Spring的id值做匹配,
* 匹配成功赋值
* 匹配不成功,报错
*
*说明:
* 注解只能用用于引用类型
* 注解写法简单,效率低
* XML写法难,效率高
*/
注解的scan
Person类依旧不变,添加注解@Component(“person”)
Student类添加注解@Component(“student”);
配置文件:
<!-- component:把一个类放入到spring容器中,该类就是一个component
在base-package指定对的包及子包下扫描所有类
-->
<context:component-scan base-package="com.my.spring.scan"></context:component-scan>
总结一下@Component的原理
*1.启动spring容器,spring容器解析配置文件
*2.当解析到<context:component-scan base-package="com.my.spring.scan"></context:component-scan>时候
* 就会在com.my.spring.scan这个包及其子包中扫描所有的类看那些类有@Component注解
*3.如果有该注解,则有如下规则:
* (1)比如:@Component
* public class PersonDaoImpl{
* }
* 就会被翻译成
* <bean id = 'personDaoImpl' class = ''> 即把类的第一个字母小写其他字母不变
* (2).@Component('personDao')
* 就会被翻译成<bean id = 'personDao' class = ''>
* 4.按照@Resource注解的规则赋值