Spring 装配bean的几种方式

spring装配bean的分为以下三种方式:

1.在XML中显示装配bean

  工作中最常见的就是隐示配置,但是有些情况下只能使用显示配置,比如引用第三方库,要想让spring管理这个bean,不可能修改它的源码,只能通过显示配置。

  话不多说,见例子

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:p="http://www.springframework.org/schema/p"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <!--使用bean标签去申明bean-->
 8     <bean id="waiter" class="xyz.mrwood.study.spring.example.Waiter" />
 9     <!--可以保用p标签来注入依赖的bean-->
10     <bean id="store" class="xyz.mrwood.study.spring.example.Store" p:waiter-ref="waiter" />
11 
12 </beans>

2.在JAVA中进行显示配置

  通过 @Configuration 和 @Bean来搭配完成。 

@Configuration
public class BeanConfig {

    /**
     * 申明bean
     * @return
     */
    @Bean
    public Waiter waiter() {

        return new Waiter();
    }

    @Bean
    public Store store() {

        Store store = new Store();
        store.setWaiter(waiter()); //通过调用bean的方法来注入
        return store;
    }
}

3.隐示配置,分为两步:组件扫描、自动装配。

  spring自动发现应用上下文创建的bean,分为两步:创建可被扫描的bean(@Component注解标记类),启动组件扫描(分为JAVA config配置、XML配置)

  注解标记类

 1 package xyz.mrwood.study.spring.example;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 /**
 6  * 项目:study-spring-example
 7  * 包名:xyz.mrwood.study.spring.example
 8  * 功能:
 9  * 时间:2016-12-13 22:05
10  * 作者:Mr.Kiwi
11  */
12 @Component
13 public class Waiter {
14 
15     public void service(String name){
16 
17         System.out.println("service to " + name);
18     }
19 
20 }

启动扫描的JAVA Config配置。

package xyz.mrwood.study.spring.example;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 项目:study-spring-example
 * 包名:xyz.mrwood.study.spring.example
 * 功能:
 * 时间:2016-12-13 22:13
 * 作者:Mr.Kiwi
 */
@Configuration
@ComponentScan
public class ApplicationConfig {
}
@Configuration这个注解是指定此类是一个配置类,
@ComponentScan 可以指定扫描主键的范围,如果没有,那么默认当前类所在的包及其子包下所有的类。

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="xyz.mrwood.study.spring.example" />

</beans>
@ComponentScan 的XML表现形式。

自动装配

Spring自动满足bean之间的依赖,通过autowired进行自动装配

package xyz.mrwood.study.spring.example;

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

/**
 * 项目:study-spring-example
 * 包名:xyz.mrwood.study.spring.example
 * 功能:
 * 时间:2016-12-13 22:08
 * 作者:Mr.Kiwi
 */
@Component
public class Store {

    @Autowired
    private Waiter waiter;

    public void service(){

        waiter.service("kiwi");
    }
}

 

依赖注入的两种方式:属性注入和构造器注入。

  属性注入

    使用property标签为属性注入value。

<?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:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
     
    <!-- 配置一个 bean -->
    <bean id="helloWorld" class="com.jackie.spring.helloworld.HelloWorld">
        <!-- 为属性赋值 -->
        <property name="name" value="Jackie"></property>
    </bean>
</beans>

  构造器注入

    使用标签constructor-arg 通过构造器注入,前提是POJO中必须有此构造器

<bean id="car" class="com.jackie.spring.helloworld.Car">
    <constructor-arg value="DaZhong" index="1"></constructor-arg>
    <constructor-arg value="Shanghai" index="0"></constructor-arg>
    <constructor-arg value="250000" type="float"></constructor-arg>
</bean>

 

Spring的配置文件及其简洁,我们甚至看不到声明bean,只有Context:component-scang该标签意为spring会扫描包下面的所有相关类,相关类是指具有以下字样的注解

@Component: 基本注解, 标识了一个受 Spring 管理的组件

@Respository: 标识持久层组件

@Service: 标识服务层(业务层)组件

@Controller: 标识表现层组件

标注了如上注解的类都是受Spring管辖的

Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似

 

posted @ 2017-09-24 20:11  vvning  阅读(856)  评论(0编辑  收藏  举报