精通SpringFamework(1) -装配Bean

在基于springframework 的应用中,java对象生存在spring的容器 container 中,具体是一个hashMap实现,spring容器负责创建对象,装配对象,配置对象,并在整个bean的生命周期管理对象,从new() 到finalize()。

1.spring framework 三种bean 的装配方式

1.1在XML 中显示配置

早起的spring 框架中 bean 都是在xml 中显示配置,那时J2EE 开发所说的SSH 只剩下spring 在业界比较流行。
xml 中会配置几百行的bean ,bean的依赖组装关系都体现在xml 中。

不过当下几乎没有公司使用spring 时会在xml 中显示的配置
初学者可以自己写一个demo 尝试理解一下

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


    <bean id="helloWorld" class="com.fancv.spring.HelloWorld">
        <property name="message" value="Hello World!"/>
        <property name="age" value="21"/>
    </bean>
    <bean id="person2" class="com.fancv.spring.HelloWorld">
        <constructor-arg index="0" value="lisi" />
        <constructor-arg index="1" value="21" />
    </bean>
    <Context:component-scan base-package="com.fancv.scan"/>

</beans>

注意xml 中的语法,xmlns 和xsi 中的对应关系,xsi 中 成对出现,笔者没有深入研究中spring中xml的配置文件,顺应技术发展趋势,xml 配置会逐步淡出技术栈

不过对于历史项目的维护,就有必要了,大家根据自己情况选择

##1.2 在java中显示的配置
直白的说就是在java中写代码,生成一个bean 注入到ApplicationContext

例如:

@Component
public class MyDisk {
    
    

    @Bean(name = "minCar")
    public Car getCar() {
    
    
        return new Car();
    }
}

这种java配置中显示注入比较常用,例如你的项目中引用了第三份服务的包(企业工商信息服务,银行服务)

无法通过自动化装配把你需要的第三方Bean 实例注入到你的ApplicationContext

1.3隐式 的Bean 发现机制和自动装配

业界常用的方式,使用了@Component, @Autowired,@Qualifier(“minCar”),@Named, @Configuration注解

使用时,注意装配时的歧义,Bean 的类型

2.混合配置

当你想同时使用 XML 配置和注解自动装配时,混合配置必不可少,核心注解

@ImportResource(“classpath:cd-config.xml”)

@Component
@ImportResource("classpath:cd-config.xml")
public class MyDisk {
    
    

    @Bean(name = "minCar")
    public Car getCar() {
    
    
        return new Car();
    }
}

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


    <bean id="helloWorld" class="com.fancv.spring.HelloWorld">
        <property name="message" value="Hello World!"/>
        <property name="age" value="21"/>
    </bean>
    <bean id="person2" class="com.fancv.spring.HelloWorld">
        <constructor-arg index="0" value="lisi" />
        <constructor-arg index="1" value="21" />
    </bean>
    <Context:component-scan base-package="com.fancv.scan"/>

</beans>

1.XML 中注明扫描路径,在扫描路径下 使用注解

2.使用注解 扫描整个项目目录,在其中一个config文件中,引入独立的XX.xml 配置

3.混合配置可以引入多个配置文件

3.Spring Framework 的Bean实例存放在哪里

通过调试spring的程序
在这里插入图片描述
在这里插入图片描述
如上图所示,Bean 实例存放在一个currentHashMap中

分析SpringFrameWork 的源码,你会发现springframework框架

仅仅包含:
spring-aop
spring-beans
spring-context
spring-core
spring-expression
spring-jcl
总共6个jar 包,
spring 仅仅是一个java Bean 的容器,帮助你的java 项目使用IOC 的形势更好的解耦,你不必在为维护Bean 操心,只需要把 你在进程中使用的Bean 注入(spring 框架Bean的初始化)到Spring 中,你就可以在你的程序员中 方面的使用 Bean。

文章禁止任何形式的转载,保留所有权利

猜你喜欢

转载自blog.csdn.net/keep_learn/article/details/114498546