zoukankan      html  css  js  c++  java
  • Spring bean配置 入门

     Spring 的入门案例:(IOC)

       IOC 的底层实现原理(结构图2.01)

        

                            图:2.01

      

      IOC:Inversion of Control 控制反转,指的是对象的创建权反转(交给)给Spring,

          作用是实现了程序的解耦合

      步骤一:下载 Spring 的开发包:

        官网:http://spring.io/

        下载地址: http://repo.springsource.org/libs-release-local/org/springframework/spring

      创建 web 项目,引入 Spring 的开发包:

            导入基本的jar包(4+2)

          

               图:2.02

       引入相关配置文件:

          log4j.properties  //日志文件

          applicationContext.xml

        引入约束:

          spring-framework-4.2.4.RELEASEdocsspring-framework-referencehtmlxsd-configur ation.html 

        

           <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">

          </beans>

        

        编写相关的类(开发工具:eclispe):       

          //新建 Dao_Intf 接口 

         public interface Dao_Intf {

            public void sayHello();

           }

          

        //实现Dao_Intf接口并实现接口的方法

         public class Dao_Impl implements Dao_Intf {

            @Override

            public void sayHello() {

            System.out.println("Hello Spring...");

          }

        }

            

      完成配置:

        // 新建 xml文件  名字一般是给 applicationContext.xml  看自己的需求吧

          <!-- Spring的入门案例================ -->

         <bean id="Dao_Impl" class="com.xiao.spring_bean.Dao_Impl"></bean>

        <!-- id是唯一的,name可重名,需要注意 -->

     

      完成上面的步骤,编写测试程序:

          需要导入 junit-4.10.jar ,main方法也可以

         // Spring 的方式:

        @Test

       public void demo1() {

        // 创建 Spring 的工厂类:

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 通过工厂解析 XML 获取 Bean 的实例.

        Dao_Intf dao_Intf = (Dao_Intf ) applicationContext.getBean("Dao_Impl");//需要对应id的名称,不然会报异常

        dao_Intf.sayHello();

       }

      

      IOC :控制反转,将对象的创建权交给了 Spring.

      DI :Dependency Injection 依赖注入.需要有IOC 的环境,Spring 创建这个类的过程中,Spring 将类的依赖的属性设置进去.

      ............................................................................................................................................................................................................

                                                      欢迎留言、感谢观看!

  • 相关阅读:
    系统并发报too much open files 错误
    plsql 安装Some Oracle Net versions cannot connect from a path with parentheses
    mysql登录报错
    web.xml配置文件详解之一servlet配置
    hibernate createQuery查询传递参数的两种方式
    mysql登录连接远程数据库命令行
    java 项目打包部署 过程
    flex ArrayCollection的新增与删除的同步
    加大eclipse以及jvm的内存
    2017 八月 UFED Series Releases 系列 6.3 重大更新发布
  • 原文地址:https://www.cnblogs.com/zhunong/p/11784996.html
Copyright © 2011-2022 走看看