lesson one:spring ioc,属性注入方式,实现自己的beanfactoy

 

spring ioc 使用的两大关健技术和一个设计模式

     1,jdom

     2, 反射机制

     3,工厂模式(单例模式)

属性注入的方式:

      第一种:是通过set方法

<bean name="student" class="net.nw.vo.Students"></bean>
    <bean name="studentDAO" class="net.nw.impl.StudentsDAOImpl"></bean>
    <bean name="studentService" class="net.nw.service.StudentsService">
        <property name="studentsDAO" ref="studentDAO"></property>
    </bean>
beanx文件
 1     private StudentsDAO studentsDAO;
 2 
 3     public StudentsDAO getStudentsDAO()
 4     {
 5         return studentsDAO;
 6     }
 7 
 8     public void setStudentsDAO(StudentsDAO studentsDAO)
 9     {
10         this.studentsDAO = studentsDAO;
11     }
Service 类中的属性
 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentsService service = (StudentsService) ctx.getBean("studentService");
        Students students = new Students();
        students.setSid("001");
        students.setSname("peter.peng");
        students.setAge(100);
        boolean flag = service.saveStudents(students);
得到实例


    第二种方式:构造方法,不是很方便,少用。
   

    第三方式:注解注入 推荐这种方式开发当然也可以有第一步

    java annotation 一般用这种

          第一步:写一个bean.xml文件,namesplace导入的是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:annotation-config />
</beans>
注解注入的命名空间

   第二步:写bean文件     

<bean name="studentsDAO" class="net.nw.impl.StudentsDAOImpl" />
    <bean name="student" class="net.nw.vo.Students" />
    <bean name="studentService" class="net.nw.service.StudentsService"></bean>
bean文件

        第三步:在引用的地方加上注解如下

    @Resource(name = "studentsDAO")
    private StudentsDAO studentsDAO;

    @Resource(name = "student")
    private Students student;// 利用构造方法来注入

    public boolean saveStudents(Students student)
    {
        if (studentsDAO.saveStudents(student))
        {
            System.out.println(student);
            System.out.println(student.getAge());
            return true;
        } else
            return false;
    }
加入注解引用

      注意:如果注解的名空在BEA里找不到的话那就就会按类形来找。

    spring annotation

       @AutoWired 这是spring的注解,默认是按类型来匹配的      @Qualifier 是按名字来匹配的

//@Resource(name = "studentsDAO")//java标准的注解
    @Autowired()  //按类型来注解
    @Qualifier("studentsDAO")//按名称来注解  这个注解一定的加上@Autowired
    private StudentsDAO studentsDAO;

    //@Resource(name = "student")//java标准的注解
    private Students student;// 

    public boolean saveStudents(Students student)
    {
        if (studentsDAO.saveStudents(student))
        {
            System.out.println(student);
            System.out.println(student.getAge());
            return true;
        } else
            return false;
    }
spring的注解方式

 

 

posted on 2013-05-19 08:51  peter.peng  阅读(301)  评论(0)    收藏  举报