(1)注解就是为了说明java中的某一个部分的作用(Type);
(2)注解都可以用于那个部分是@Target注解起的作用;
(3)注解可以标注在ElementType枚举类所指定的位置上;
(4)用来解析注解的类成为注解解析器;
(1) bean.xml 导入注解解析器 - context命名空间,只有引用类型可以加上注解,基本类型不可以;
(2)@Resource使用步骤:
1) 在spring的配置文件中导入命名空间
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"
@Resource(name="student_di")
private Student student;
(3)示例
1)Person类
public class Person {
private String name;
private Integer age;
@Resource(name="student_di")
private Student student;
public void showStudent(){
this.student.say();
}
}
public class Student {
public void say(){
System.out.println("我是学生");
}
}
@Test
public void testDI() {
Person person=(Person) context.getBean("person_di");
person.showStudent();
}
(4)总结:
当启动spring容器的时候,spring容器加载了配置文件,只要遇到bean的配置,就会
为该bean创建对象,在容器范围内查找所有的bean,如果有@Resource注解,判断注解name属
性值是否与speing容器中ID的值做匹配,匹配成功则赋值;
(1)类扫描的注解解析器
component : 指的就是一个类;base-package 在该包
(2)基本步骤:
1. 导入命名空间
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">
@Component("a")
public class Person {
private String name;
private Integer age;
@Resource(name = "b")
private Student student;
public void showStudent() {
this.student.say();
}
}
(3)示例
1)Person类
@Component("a")
public class Person {
private String name;
private Integer age;
@Resource(name = "b")
private Student student;
public void showStudent() {
this.student.say();
}
}
@Component("b")
public class Student {
public void say(){
System.out.println("我是学生 SCAN");
}
}
@Test
public void testScan() {
Person person=(Person) context.getBean("a");
person.showStudent();
}
(4)总结xml与注解
1. xml书写麻烦,但是效率高;
2. 注解书写简单,但效率低;(可以忽略)
使用的注解有 : @Repository , @Service , @Controller ,分别对应 dao层,service层,action层;
(1)dao 接口
public interface DocumentDao {
void saveDocument();
}
(2)dao实现
@Repository("DocDao")
public class DocumentDaoImpl implements DocumentDao {
@Override
public void saveDocument() {
System.out.println("保存成功");
}
}
public interface DocumentService {
void saveDocument();
}
@Service("DocService")
public class DocumentServiceImpl implements DocumentService {
@Resource(name="DocDao")
private DocumentDao docdao;
@Override
public void saveDocument() {
docdao.saveDocument();
}
}
@Controller("DocController")
public class DocumentView {
@Resource(name="DocService")
private DocumentService doucService;
public void saveDoc(){
doucService.saveDocument();
}
}
@Test
public void testDocument() {
DocumentView view= (DocumentView)context.getBean("DocController");
view.saveDoc();
}
继承 :
在配置文件中 给 bean 添加 abstruct="true" 属性,告诉spring容器,该bean不能创建对象;
子类想用父类的属性,需要在bean 添加 parent="xxx abstruct" 属性,添加父类bean的id;
示例:
父类 :
public class Person {
private String name;
public void say(){
System.out.println("我是父类");
}
}
public class Student extends Person {
}
http://download.csdn.net/detail/lablenet/9379973