在前面,看到自定义注解,并且也简单的使用了一下,
然后就再次用个简单的例子,来看看s,pring里面是如何使用注解的。如下:
先看J,ava代码:简单,就是2个bean和一个主方法。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import javax.annotation.Resource;
class Student {
void say() {
System.out.println("hello");
}
}
class Person {
@Resource(name="student")
private Student student;
//Access can be package-private
//所以方法的 public就不要啦
void say(){
this.student.say();
}
}
/**
* Created by lxk on 2016/9/29
*/
class AtInterfaceTest {
public static void main(String[] args) {
//ApplicationContext ctx = new ClassPathXmlApplicationContext("file:E:/xxx/intellij_work/TrunkNew/sss.xml");
ApplicationContext ctx = new FileSystemXmlApplicationContext("sss.xml");
Person p = (Person) ctx.getBean("person");
p.say();
}
}
注意上面的Person里面的student属性是没有getter和setter的。但是在测试main方法里面确直接可以使用say方法,这个方法里面的student对象何来?
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--
(只有下面的是需要自己添加的,其他的都是在新建spring配置xml文件的时候,就自带的啦)
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:annotation-config></context:annotation-config>
3、导入person和student
-->
<context:annotation-config/>
<bean id="person" class="com.xxx.x.model.s.Person"/>
<bean id="student" class="com.xxx.x.model.s.Student"/>
</beans>
关于配置文件里面的东西,可以翻看以前的spring分类里面的东西。
简单介绍:
<bean>里面的class对应model的全路径。
id命名最好是类名的首字母缩写。
这个配置超简单了点,就是示例而已。用的东西不多。
执行结果的图:
上面关于ApplicationContext 的初始化的问题,以及该如何使用,下次再说吧。
如下:
关于初始化ApplicationContext时报错怎么解决
对上面的文章做一下补充:加深对注解的工作原理的理解(这次是重点)
可以做如下修改:(直接上代码吧,来的快些)
//1.一般正常形式(测试结果:正常)
class Person {
@Resource(name = "student")
private Student student;
void say(){ this.student.say(); }
}
//2.删除注解后面的括号内容(测试结果:正常)
class Person {
@Resource
private Student student;
void say(){ this.student.say(); }
}
//3.在上面的基础上修改属性名称(测试结果:正常)
class Person {
@Resource
private Student ss;
void say(){ this.ss.say(); }
}
//4.在上面的基础上修改括号内容(测试结果:失败)
class Person {
@Resource(name="ss")
private Student ss;
void say(){ this.ss.say(); }
}
//5.在上面的基础上修改括号内容和配置文件bean的id为ss(测试结果:正常)
class Person {
@Resource(name="ss")
private Student ss;
void say(){ this.ss.say(); }
}
<bean id="ss" class="com.xxx.x.model.s.Student"/>
//6.在上面的基础上修改注解(测试结果:正常)
class Person {
@Autowired
private Student ss;
void say(){ this.ss.say(); }
}
//7.在上面的基础上修改注解(测试结果:正常)
class Person {
//下面2个注解的作用相当于 @Resource(name="ss") 一个
//区别在于@Resource是javax的,下面2个是spring自己的
@Autowired
@Qualifier(value = "ss")
private Student ss;
void say(){ this.ss.say(); }
}
//然后看@Resource的源码部分如下:
public @interface Resource {
String name() default "";//解释了在使用注解不写name = "xxx"的时候,默认是""这个值,
。。。。
}
具体总结如下:
工作原理:
当spring容器启动的时候,
ApplicationContext ctx = new FileSystemXmlApplicationContext("sss.xml");
spring容器会创建纳入spring容器管理的bean.分别为person和student;
spring容器会解析配置文件,会解析到<context:annotation-config/> 会在纳入spring的bean范围内查找属性上是否存在
注解@Resource(name="student")
* 如果存在:
* 继续解析@Resource有没有name属性
* 如果没有name属性
就会在所属的属性上,把属性的名称解析出来。会让属性的名称和spring中的bean中的id进行匹配
如果匹配成功,则把spring容器中相应的对象赋值给该属性
如果匹配失败,则按照类型(Class)进行匹配
* 如果有name属性
就会解析name属性的值,把这个值和spring容器中的bean的id进行匹配
如果匹配成功,则把spring容器中的相应的对象赋值给该属性
如果匹配失败,则直接报错
* 如果不存在:
不做任何事情
xml注入属性和注解注入属性的写法的对比:
xml : 书写比较麻烦,但是效率比较高(直接在配置文件里面全有啦)
注解:书写比较简单,但是效率比较低(一遍遍的扫描)
注解的写法只适合引用
再有总结,如下:
下次写:
关于常用的---类扫描的注解解析器---
更加自动化点,需要配置的东西更少。