Spring框架(IOC&DI)xXML形式和注解形式

本文详细介绍了Spring框架的IOC(控制反转)和DI(依赖注入)概念,包括XML配置和注解配置的方式。讨论了ApplicationContext接口的实现类,Bean的id和name配置,以及三种配置Bean的方式:无参构造、静态工厂和实例工厂。此外,还涵盖了Bean的作用域,如singleton和prototype,并解释了不同scope的应用场景。文章进一步探讨了Bean的生命周期,以及如何在XML中配置初始化和销毁方法。最后,文章深入讲解了依赖注入的不同方式,如构造方法注入、Set注入以及使用注解进行配置,以及如何通过import导入多个XML配置文件。
摘要由CSDN通过智能技术生成

IOC

Spring的依赖包

pom.xml

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

在resource下创建applicationContext.xml

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

    <!--把对象的创建交给spring来管理-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
</beans>

spring的ioc:通过标签去创建对象的。

测试代码:

/**
- 模拟一个表现层,用于调用业务层
  */
  public class Client {
   
  	// 模拟Action
    /**
     * 测试由ApplicationContext对象获取spring容器中创建的对象
     * @param args
     */
    public static void main(String[] args) {
   
        // 代码之间存在依赖关系(耦合)
        // AccountService accountService = new AccountServiceImpl();
        // 由spring创建对象(完成对象的解耦)
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过名称调用(通过spring容器中的id属性)(推荐)
        AccountService accountService = (AccountService) ac.getBean("accountService");
        // 通过类型调用(通过spring容器中的class属性)
        // AccountService accountService = ac.getBean(AccountServiceImpl.class);
        accountService.saveAccount();
    }
}

【**ApplicationContext **接口的实现类 】

  • ClassPathXmlApplicationContext 它是从类的根路径下加载配置文件 (推荐)
  • FileSystemXmlApplicationContext 它是从磁盘路径下加载配置文件,配置文件可以在磁盘的任意位置
  • AnnotationConfigApplicationContext 当我们使用注解配置容器对象时,需要使用此类来创建spring容器,它用来读取注解

id和name的配置

id中不能出现特殊字符(容器中的唯一标识),name中可以出现特殊的字符(表示引用)

可以指定多个name,之间可以用分号(“;”)、空格(“ ”)或逗号(“,”)分隔开,如果没有指定id,那么第一个name为标识符,其余的为别名; 若指定了id属性,则id为标识符,所有的name均为别名。如:

<bean name="alias1 alias2;alias3,alias4" id="hello1" class="com.zyh.spring3.hello.HelloWorld"> </bean>

此时,hello1为标识符,而alias1,alias2,alias3,alias4为别名,它们都可以作为Bean的键值;


配置Bean的三种方式

第一种:采用无参构造的方式实列化的方式

(1)applicationContext.xml

<!--创建Bean的三种方式 -->
<!-- 第一种方式:使用默认构造函数创建。
        在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
        采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

(2)需要无参的构造方法

public class AccountServiceImpl implements AccountService {
   
    //可以不写,默认就是无参构造
    public AccountServiceImpl(){
   
        System.out.println("对象创建了");
    }
}
第二种:采用静态工厂实列化的方式

(1)applicationContext.xml

<!-- 第二种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器) -->
<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>

(2)AccountServiceImpl.java

public class AccountServiceImpl implements AccountService {
   
}

(3)StaticFactory.java,静态工厂类

/**
 * 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
 */
public class StaticFactory {
   
    public static AccountService getAccountService(){
   
        return new AccountServiceImpl();
    }
}

第三种:采用实例工厂(非静态的)实例化的方式

(1)applicationContext.xml

<!-- 第三种方式: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器) -->
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>

(2)AccountServiceImpl.java

public class AccountServiceImpl implements AccountService {
   

}

(3)InstanceFactory.java


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值