java spring IOC的三种方式之XML形式

IOC

IOC就是将获取对象的方式将给框架,或者交给容器进行,有三种方式

  1. 基于Xml 元数据模式
  2. 基于注解的配置
  3. 基于Java config

XML形式

相关的注解

需要再相应的对象类class上使用 @Component修饰,表示是一个beacon。,然后再XML中配置,启动自动扫描,或者直接配置也可以,但是自动扫描会更好一些。

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"
    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"
>
//自动扫描的
<context:component-scan base-package="com.demo.model"/>
//手动配置的,手动配置的话可以额外配置其他属性。比如这个<qualifier>
    <bean id="CleanAir" class="com.demo.model.CleanAir">
    	<qualifier value="cleanair"/> 
    </bean> 
    <bean id="DirtyAir" class="com.demo.model.DirtyAir">
    	<qualifier value="dirtyair"/>
    </bean>
    <bean id="person" class="com.demo.model.Person"/>
</beans>

对应的类

java 复制代码
package com.demo.model;

import org.springframework.stereotype.Component;

@Component
public class CleanAir implements IAir {
    @Override
    public String toString() {
    	
        return  "CleanAir";
    }
}
java 复制代码
package com.demo.model;

import org.springframework.stereotype.Component;

@Component
public class DirtyAir implements IAir {
    @Override
    public String toString() {
    	
        return  "DirtyAir";
    }
}
java 复制代码
package com.demo.model;

public interface IAir {

}
java 复制代码
package com.demo.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class Person {
	
	//@Autowired //对构造函数进行标注

    public Person(@Qualifier("dirtyair")IAir air) {
        
        this.air = air;
    }
    
    IAir air;
    
    public void Breath()
    {
    	System.out.print(this.air.toString());
    }
}

Main函数

java 复制代码
package com.demo.ioc;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.demo.model.*;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {	//这个就是XML配置的,然后使用beanFactory进行获取bean。
        ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"});
        BeanFactory factory=context;
        Person person=(Person)factory.getBean("person");


        person.Breath(); 
        
    }

}

其他

只使用@Qualifier("")注解,而不使用@Autowired 注解,为什么结果还是争取的呢

@Qualifier("") 注解和 @Autowired 注解都是 Spring 框架中的依赖注入注解。它们的作用是不同的。

@Autowired 注解用于自动装配 bean,它告诉 Spring 在相应的 bean 中查找匹配的依赖项并自动进行注入。如果没有找到匹配的 bean,Spring 会抛出 NoSuchBeanDefinitionException 异常。

@Qualifier 注解用于指定 bean 的名称,在有多个相同类型的 bean 时,通过指定 @Qualifier 注解可以明确指定需要注入哪一个 bean。如果没有指定 @Qualifier 注解或者指定的名称与实际的 bean 不匹配,Spring 会抛出 BeanCreationException 异常。

如果你只使用 @Qualifier("") 注解而不使用 @Autowired 注解,这实际上是告诉 Spring 你想要手动指定要注入的 bean。但如果你没有在相应的 bean 中使用 @Qualifier 注解来指定名称,或者没有在 Spring 配置中定义相应的 bean,那么 Spring 就无法找到要注入的 bean,结果会是相同的。

因此,如果你只使用 @Qualifier 而不使用 @Autowired,你必须确保在 Spring 配置中定义了相应的 bean,并在需要注入的类中手动使用 @Qualifier 注解来指定名称。否则,结果将是争取的。

相关推荐
【D'accumulation】22 分钟前
典型的MVC设计模式:使用JSP和JavaBean相结合的方式来动态生成网页内容典型的MVC设计模式
java·设计模式·mvc
试行37 分钟前
Android实现自定义下拉列表绑定数据
android·java
茜茜西西CeCe43 分钟前
移动技术开发:简单计算器界面
java·gitee·安卓·android-studio·移动技术开发·原生安卓开发
救救孩子把1 小时前
Java基础之IO流
java·开发语言
小菜yh1 小时前
关于Redis
java·数据库·spring boot·redis·spring·缓存
宇卿.1 小时前
Java键盘输入语句
java·开发语言
浅念同学1 小时前
算法.图论-并查集上
java·算法·图论
立志成为coding大牛的菜鸟.1 小时前
力扣1143-最长公共子序列(Java详细题解)
java·算法·leetcode
鱼跃鹰飞1 小时前
Leetcode面试经典150题-130.被围绕的区域
java·算法·leetcode·面试·职场和发展·深度优先
爱上语文2 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring