Jimoshi_Spring 框架学习(一)--Spring实现IOC思想、javabean对象、Spring对象和对象之间关系

来源:互联网 发布:网络服务器外包合同 编辑:程序博客网 时间:2024/05/17 06:52
Jimoshi成长经历:前面的笔记后面再慢慢整理-------方便自己

目录:Spring框架概念、使用Spring、使用Spring实现IOC思想、通过Spring实现IOC思想的几种方式、javabean对象几种注入类型、Spring自动装载对象、Spring对象和对象之间关系(三种关系),设置单例和多例

Spring 框架学习(一):

一、Spring框架概念

  1、IOC 控制反转

  2、AOP 面向切面

    2.1.方便解耦,简化开发
 
    2.2.通过AOP对事务和日志进行支持,在不影响业务代码同时,加入事务管理和日志支持

    2.3.Spring通过动态代理和反射来实现

二、如何使用Spring(管理bean对象)
 
  1、引入spring的包(在pom.xml中)
   
  代码示例:
  <!-- 添加spring -->
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
     <version>4.0.6.RELEASE</version>
  </dependency>

  2、配置bean.xml(通过配置bean.xml 管理项目中javabean对象)

 
   示例:通过一个人的类来测试效果(Person类)

   2.1.建立Person类模板(com.zr.model中)
   
   代码示例:
    
   public class Person {
    public void dosome(){
        System.out.println("你好");
    }
   }

   2.2.配置bean.xml(src.main.resources中创建bean.xml文件)
   
   代码示例:

   <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd">          
   <bean id="person1" class="com.zr.model.Person"></bean>
   </beans>

  3、测试(在model中创建测试类TestSpring类)
 
  代码示例:

        @Test
    public void test1(){
        //通过这个容器读取bean.xml文件
    ApplicationContext ac =    new ClassPathXmlApplicationContext("bean.xml");
    Person p1 = (Person) ac.getBean("person1");
    Person p2 = (Person) ac.getBean("person1");
    //验证Spring加载时默认以单例模式生成
    System.out.println(p1==p2);
    p1.dosome();
    }
 
三、使用Spring实现IOC思想

  1、对比:在没有IOC思想(实现老师让学生查询书籍)

    1.1.在(com.zr.dao中)创建二个学生模板类( XiaoLiDao类,XiaowangDao类)

    代码示例:
 
    public class XiaoLiDao {
    public void find(){
        System.out.println("找到了计算机书籍");
    }
    }
    public class XiaowangDao {
    public void find(){
        System.out.println("找到了文学书籍");
    }
    }

    1.2.在(com.zr.service中)创建一个查找服务的模板类(FindService类)

    代码示例:

    public class FindService {
    //提供的业务服务
    public  void  find(){
        //XiaowangDao  xw = new XiaowangDao();
        //xw.find();
        XiaoLiDao  xl  = new XiaoLiDao();
        xl.find();
    }
     }

    1.3.测试(在测试类TestSpring类中添加)
    
     @Test
    public void test2(){
        FindService fs = new FindService();
        fs.Find();
    }

  2、对比:使用IOC思想(实现老师让学生查询书籍)


    2.1.在(com.zr.model)中创建FindDao接口
     
    代码示例:
    public interface FindDao {
        public void find();
     }

    2.2.创建(com.zr.dao.impl)并且在其中添加(XwFindDaoImpl类和XzFindDaoImpl类)且实现FindDao接口

    代码示例:

    XwFindDaoImpl类:
    public class XwFindDaoImpl implements FindDao{
    @Override
    public void find() {
        // TODO Auto-generated method stub
        System.out.println("找到了文学书籍");
    }
    }
    XzFindDaoImpl类:
    public class XzFindDaoImpl implements FindDao {
    @Override
    public void find() {
        // TODO Auto-generated method stub
        System.out.println("找到了计算机书籍");
    }
    }

    2.3.在(com.zr.service)中创建FindService接口

    代码示例:
    
    public interface FindService {
          public void find();
    }

    2.4.创建(com.zr.service.impl)并且在其中添加(TeacherFindServiceImpl类)且实现FindService接口

    代码示例:
   
    public class TeacherFindServiceImpl implements FindService{
        private FindDao finddao;
        public void setFinddao(FindDao finddao) {
          this.finddao = finddao;
         }
    @Override
    public void find() {
        // TODO Auto-generated method stub
        finddao.find();
    }
     }

    2.5.测试(在测试类TestSpring类中添加)

    代码示例:

        @Test
    public void test3(){
        TeacherFindServiceImpl fs = new TeacherFindServiceImpl();
        //fs.setFinddao(new XzFindDaoImpl());
        fs.setFinddao(new XwFindDaoImpl());
        //相当于在find核心代码并未发生改变,通过传入具体的实现,可以动态的选择由谁来执行
        fs.find();
    }

   3、对比:通过Spring使用IOC思想来管理对象

    3.1.配置bean.xml文件

    代码示例:

    <!-- dao -->
      <bean id="xiaowang" class="com.zr.dao.impl.XwFindDaoImpl"></bean>
      <bean id="xiaozhang" class="com.zr.dao.impl.XzFindDaoImpl"></bean>
    <!-- service 通过属性的方式进行注入,name代表teacher下面属性名ref选择哪个对象注入进来-->
      <bean id="teacher" class="com.zr.service.impl.TeacherFindServiceImpl">
          <property name="finddao" ref="xiaozhang"></property>
      </bean>

    3.2.测试(在测试类TestSpring类中添加)

    代码示例:
 
        @Test
    public void test4(){
          //通过这个容器读取bean.xml文件
      ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
      FindService fs = (FindService) ac.getBean("teacher");
      fs.find();
    }

四、通过Spring实现IOC思想的几种方式
 
  1、通过属性注入
   1.1.在(com.zr.model)中创建Dog类
   
   代码示例:
 
    public class Dog {
    private int did;
    private String dname;
    public int getDid() { return did;}
    public void setDid(int did) { this.did = did; }
    public String getDname() { return dname; }
    public void setDname(String dname) { this.dname = dname; }

   1.2.配置bean.xml文件
   
   代码示例:

    <!-- 属性的方式注入 -->
    <bean id="dog" class="com.zr.model.Dog">
        <property name="did" value="1"></property>
        <property name="dname" value="小黑"></property>
    </bean>
 
   1.3.测试(在com.zr.model中创建TestSpring1)

   代码示例:

       @Test
    public void test1(){
         ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
         Dog dog = (Dog) ac.getBean("dog");
         System.out.println(dog.getDid());
         System.out.println(dog.getDname());
      }
        }


  2、通过构造方法(通过参数的类型和索引)
   
   2.1.在(com.zr.model)中的Dog类中添加构造方法

   代码示例:

   public Dog() {
    super();
    // TODO Auto-generated constructor stub
    }
   public Dog(int did, String dname) {
    super();
    this.did = did;
    this.dname = dname;
   }

   2.2.配置bean.xml文件

   <!-- 通过够着函数(可以选择通过类型或者索引) -->
   <bean id="dog1" class="com.zr.model.Dog">
     <!--  <constructor-arg type="int" value="2"></constructor-arg>
           <constructor-arg type="String" value="小黑1"></constructor-arg> -->
      <constructor-arg index="0" value="2"></constructor-arg>
      <constructor-arg index="1" value="小黑1"></constructor-arg>
   </bean>

  2.3.测试(在com.zr.model中创建TestSpring1中添加)
   
  代码示例:
   
        @Test
    public void test1(){
      ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
      Dog dog1 = (Dog) ac.getBean("dog1");
      System.out.println(dog1.getDid());
      System.out.println(dog1.getDname());
      }
        }
 
  3、通过工厂(静态和非静态)

   3.1.在com.zr.model中添加DogFactory非静态工厂类,DogFactory1静态工厂类

   代码示例:

    DogFactory非静态工厂类,
     public class DogFactory {
    public Dog createDog(){
        Dog dog = new Dog();
        dog.setDid(1);
        dog.setDname("xiaowang");
        return dog;
    }
    }
    DogFactory1静态工厂类。
    public class DogFactory1 {
    public static Dog createDog(){
        Dog dog = new Dog();
        dog.setDid(1);
        dog.setDname("xiaowang1");
        return dog;
    }
    }

   3.2.配置bean.xml文件

   代码示例:

   <!-- 通过非静态工厂方式注入 -->
   <bean id="df" class="com.zr.model.DogFactory"></bean>
   <bean id="dog2" factory-bean="df" factory-method="createDog"></bean>

   <!-- 通过静态工厂方式注入 -->
   <bean id="dog3" class="com.zr.model.DogFactory1" factory-method="createDog" ></bean>
   
   3.3测试(在com.zr.model中创建TestSpring1中添加)

   代码示例:

       @Test
    public void test1(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
     //Dog dog2 = (Dog) ac.getBean("dog2");
         //System.out.println(dog2.getDid());
     //System.out.println(dog2.getDname());
     Dog dog3 = (Dog) ac.getBean("dog3");
     System.out.println(dog3.getDid());
     System.out.println(dog3.getDname());
    }
}
 
  4、泛型注入(Spring4提出)


五、javabean对象几种注入类型

  1、基本类型注入:
  <bean id="dog" class="com.zr.model.Dog">
     <property name="did"  value="1"></property>
     <property name="dname" value="xiaohei"></property>
  </bean>

  2、JavaBean 对象的注入:
  <bean id="teacher" class="com.zr.service.impl.TeacherFindServiceImpl">
     <property name="finddao" ref="xiaozhang"></property>
  </bean>

  3、集合类型的注入:

   3.1.在(com.zr.model)中创建People类

   代码示例:
   
    private int pid;
    private String pname;
    private Dog dog;
    private List<String> is;
   (进行get,set)

   3.2.配置bean.xml文件

   代码示例:

   <!-- 属性的注入,对象的注入,集合的注入 -->
   <bean id="people" class="com.zr.model.People">
      <property name="pid" value="1"></property>
      <property name="pname" value="小黑"></property>
      <property name="dog" ref="dog3"></property>
      <property name="is">
         <list>
           <value>sing</value>
           <value>dance</value>
         </list>
      </property>
    </bean>

   3.3.测试(在测试类TestSpring1类中添加)
    
   代码示例:
 
        @Test
    public void test2(){
         ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
         People p = (People) ac.getBean("people");
         System.out.println(p.getPname());
         System.out.println(p.getDog().getDname());
         System.out.println(p.getIs().get(0));
        
    }

六、Spring自动装载对象(在bean.xml中的beans末尾添加声明文件)

    声明文件(default-autowire)可以自动装载对象(默认是不需要)
     byName  可以通过名字
     byType  可以通过类型
     constructor 可以通过构造方法

七、Spring对象和对象之间关系(三种关系),设置单例和多例
    1、继承  
    2、依赖  
    3、引入(一定会使用)

  设置单例和多例:
  默认以单例的形式创建对象
  多例:
  配置<bean id="people"  class="com.zr.model.People" scope="prototype" >
1 0