spring框架的概述以及spring中基于xml的ioc配置

1.spring的概述

什么是spring?

Spring是一个开源的轻量级的应用程序开发框架,其目的是简化企业的应用程序开发,降低侵入性,Spring提供的IOC和AOP功能,可以将组件之间的耦合度降到最低,便于后期的维护和升级,实现了软件的高内聚低耦合思想。

Spring优势?

方便解耦,简化开发 ;AOP编程支持;声明式事务的支持

方便程序的测试 ;方便集成各种优秀框架;降低JavaEE API的使用难度

2.spring中基于xml的ioc配置

使用IDEA编译器,创建Maven工程后,配置pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.qlu</groupId>
    <artifactId>day01_qlu_03spring</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
    </dependencies>

</project>

xml的ioc配置:

<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd" >
<!--    把对象的创建交给spring来管理-->
    <bean id="userService" class="cn.qlu.service.impl.UserServiceImpl"></bean>

    <bean id="userDao" class="cn.qlu.dao.impl.UserDaoImpl"></bean>


</beans>

简单测试:


public class Client {
    /**
     * 获取spring 容器的Ioc核心容器,并根据id获取对象
     * @param args
     */
    public static void main(String[] args) {
       //获取核心容器  采用立即加载的方式 单例模式使用  BeanFactory是延时加载
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取对象
        UserService userService = (UserService) ac.getBean("userService");
        UserDao userDao =ac.getBean("userDao",UserDao.class);
        System.out.println(userService);
        System.out.println(userDao);

    }
}

Application Context 是 spring 中较高级的容器。和 BeanFactory 类似,它可以加载配置文件中定义的 bean,将所有的 bean 集中在一起,当有请求的时候分配 bean。

最常被使用的 ApplicationContext 接口实现:

  • FileSystemXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径。

  • ClassPathXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你不需要提供 XML 文件的完整路径,只需正确配置 CLASSPATH 环境变量即可,因为,容器会从 CLASSPATH 中搜索 bean 配置文件。

  • WebXmlApplicationContext:该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值