spring配置步骤

一、依赖关系:类之间的依赖和方法之间的依赖

二、解耦合:减低程序之间的依赖关系,在实际开发中,应该做到,编译期不依赖,运行时才依赖

三、解决方法:

(1)使用反射创建对象,避免使用new关键字

(2)通过读取配置文件,来获取要创建的对象全限定类名

四、spring IOC配置步骤

(1)导坐标

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
</dependencies>

(2)配置bean.xml

在mevan工程中,resources中建立

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

(3)获取对象

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

IAcountService acountService = (IAcountService)ac.getBean("acountService");
IAcountDao iAcountDao = (IAcountDao)ac.getBean("acountDao");

(4)ApllicationContext的三个常用实现类

ClassPathXmlApplicationContext:可以加载类路径下的配置文件,要求配置文件必须在类路径下
FileSystemXmlApplicationContext:可以加载磁盘任意路径下的配置文件(必须有访问权限)
AnnotationConfigApplicationContext:用于读取注解创建容器

(5)ApplicationContext:在构建核心容器时,创建对象采取的策略是:立即加载方式。即只要一读取完配置文件就马上创建配置文件中配置的对象;

       单例模式,只创建一次,多数情况下采用此接口定义对象

        BeanFactory:在构建核心容器时,创建对象采取的策略是:延迟加载方式。根据id获取对象的时刻创建对象

       什么时候用,什么时候创建。多例对象使用

(6)idea---类---右键----Diagrams,查看类的继承关系----右键---show Implements,查看类的实现

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值