zoukankan      html  css  js  c++  java
  • spring helloword

    控制反转:

    Inversion on Control , 控制反转 IOC

    对象的创建交给外部容器完成,这个就做控制反转.

    依赖注入,  dependency injection

    处理对象的依赖关系

    区别:

     控制反转, 解决对象创建的问题 【对象创建交给别人】

    依赖注入,

    在创建完对象后, 对象的关系的处理就是依赖注入 【通过set方法依赖注入】

    AOP

    面向切面编程。切面,简单来说来可以理解为一个类,由很多重复代码形成的类。

    切面举例:事务、日志、权限;

    1) 源码, jar文件:spring-framework-3.2.5.RELEASE

    commons-logging-1.1.3.jar           日志

    spring-beans-3.2.5.RELEASE.jar        bean节点

    spring-context-3.2.5.RELEASE.jar       spring上下文节点

    spring-core-3.2.5.RELEASE.jar         spring核心功能

    spring-expression-3.2.5.RELEASE.jar    spring表达式相关表

    以上是必须引入的5jar文件,在项目中可以用户库管理!

    2) 核心配置文件: applicationContext.xml  

    Spring配置文件:applicationContext.xml / bean.xml

    约束参考:

    spring-framework-3.2.5.RELEASEdocsspring-framework-referencehtmlsingleindex.html

    <beans xmlns="http://www.springframework.org/schema/beans"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns:p="http://www.springframework.org/schema/p"

        xmlns:context="http://www.springframework.org/schema/context"

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

    </beans>   

    public class App {

    // 1. 通过工厂类得到IOC容器创建的对象

    @Test

    public void testIOC() throws Exception {

    // 创建对象

    // User user = new User();

    // 现在,把对象的创建交给springIOC容器

    Resource resource = new ClassPathResource("cn/itcast/a_hello/applicationContext.xml");

    // 创建容器对象(Bean的工厂), IOC容器 = 工厂类 + applicationContext.xml

    BeanFactory factory = new XmlBeanFactory(resource);

    // 得到容器创建的对象

    User user = (User) factory.getBean("user");

    System.out.println(user.getId());

    }

    //2. (方便)直接得到IOC容器对象

    @Test

    public void testAc() throws Exception {

    // 得到IOC容器对象

    ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/a_hello/applicationContext.xml");

    // 从容器中获取bean

    User user = (User) ac.getBean("user");

    System.out.println(user);

    }

    }

  • 相关阅读:
    【60.97%】【BZOJ 1925】 [Sdoi2010]地精部落
    【14.06%】【hdu 5904】LCIS
    【50.40%】【BZOJ 4553】[Tjoi2016&Heoi2016]序列
    【52.55%】【BZOJ 4520】K远点对
    洛谷——P2446 [SDOI2010]大陆争霸
    Python模块之re
    Docker从入门到实战
    分析增加站点权重的四大切入点(转载)
    远程桌面连接不上|windows server 2003 sp2 termdd.sys(转载)
    不同服务器数据库之间的数据操作(转载)
  • 原文地址:https://www.cnblogs.com/hello-liyb/p/8252099.html
Copyright © 2011-2022 走看看