Spring(二)--Spring入门案例

Spring入门案例

1.需要的实体类

2.需要的接口和实现类

3.需要的service和实现类

/**
 * service层的作用
 *    在不改变dao层代码的前提下,增加业务逻辑操作
 */
public class StudentServiceImpl implements   StudentService {

    public StudentServiceImpl(){
        System.out.println("StudentServiceImpl的无参构造");
    }
    //创建出dao层实例   存在耦合 StudentDao dao=new StudentDaoImpl();
    StudentDao dao;
    public void sleep() {
        System.out.println("开始记录日志");  //系统级业务
             dao.sleep();  //主业务
        System.out.println("结束记录日志"); //系统级业务

    }
    public String eat() {
        System.out.println("开始记录日志"); //系统级业务
         String result=  dao.eat();//主业务
        System.out.println("结束记录日志"); //系统级业务
        return  result;
    }

    public StudentDao getDao() {
        return dao;
    }

    //DI 依赖注入
    public void setDao(StudentDao dao) {
        this.dao = dao;
    }
}

4.需要的核心配置文件

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
 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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

<!--管理dao层的对象-->
    <bean id="studentDao" class="com.xdf.dao.StudentDaoImpl"/>

<!--管理service层的对象-->
    <bean id="studentService" class="com.xdf.service.StudentServiceImpl">
        <!--给指定的dao属性赋值-->
        <property name="dao"  ref="studentDao"/>
    </bean>


    <!--创建student类的bean
      xml文件中所有的bean 都是单例的
      默认scope="singleton"
      scope="prototype"  设置原型   默认也是延迟加载

       lazy-init="true" 设置延迟加载
    -->
    <bean id="student" class="com.xdf.bean.Student" lazy-init="true">
         <property name="id" value="20"/>
         <property name="name" value="小黑"/>
    </bean>
</beans>

5.需要的测试类

public class StudentDemo {

    /**
     * 之前的方式
     */
   @Test
   public  void  test01(){
       //实例化service层对象
       StudentService service=new StudentServiceImpl();
       service.sleep();
   }
    /**
     * Spring容器的工作:
     * 01.创建各种bean对象
     * 02.管理bean之间的关系
     *
     *ApplicationContext接口有个实现类
     * ClassPathXmlApplicationContext("spring.xml")
     *   特点:
     *   spring.xml文件中配置的所有bean都实例化了!
     */
   @Test
   public  void  test02(){
       //通过spring容器来 实例化service层对象
       ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
       System.out.println("==========================================");
       //spring.xml文件中bean的id
      StudentService service= (StudentService) context.getBean("studentService");
      service.sleep();
   }

    /**
     * 实现按需加载  不是把核心文件中配置的所有bean都实例化!
     */
   @Test
   public  void  test03(){
       //通过spring容器来 实例化service层对象
       XmlBeanFactory context=new XmlBeanFactory(new ClassPathResource("spring.xml"));
       System.out.println("==========================================");
       //spring.xml文件中bean的id
      StudentService service= (StudentService) context.getBean("studentService");
      service.sleep();
   }

   /**
     * 从某个位置获取核心配置文件
     */
   @Test
   public  void  test04(){
       //通过spring容器来 实例化service层对象
       ApplicationContext context=new FileSystemXmlApplicationContext("E:/spring.xml");
       System.out.println("==========================================");
       //spring.xml文件中bean的id
      StudentService service= (StudentService) context.getBean("studentService");
      service.sleep();
   }

    /**
     * 验证单例模式
     * 所有由spring容器创建出来的对象 默认都是单例的
     */
    @Test
    public  void  test05(){
        //通过spring容器来 实例化service层对象
        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        System.out.println("==========================================");
        //spring.xml文件中bean的id
        Student stu1= (Student) context.getBean("student");
        System.out.println(stu1);
        Student stu2= (Student) context.getBean("student");
        System.out.println(stu1==stu2);
    }
}

    还会继续更新的!下次见咯!

原文地址:https://www.cnblogs.com/fl72/p/9625874.html