spring bean的装配

     spring框架的一个核心就是DI,IOC容器管理

1.<context:component-scan>

      在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean。

     <context:component-scan>,另一个<context:annotation-config/>标签根本可以移除掉,因为已经被包含进去了

     http://blog.csdn.net/chunqiuwei/article/details/16115135

2.

三种装配bean的方式

创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入(DI)的本质

  • 在XML中进行显式配置
  • 在Java中进行显式配置
  • 隐式的bean发现机制和自动装配
  • 1.在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:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--使用bean标签去申明bean-->
        <bean id="waiter" class="xyz.mrwood.study.spring.example.Waiter" />
        <!--可以保用p标签来注入依赖的bean-->
        <bean id="store" class="xyz.mrwood.study.spring.example.Store" p:waiter-ref="waiter" />
    
    </beans>
    2.隐式配置分成两步 1、组件扫描 2、自动装配
组件扫描分为 创建组件和开启扫描

创建组件:创建可被扫描的bean 
使用@Component注解标记类 

package xyz.mrwood.study.spring.example;
import org.springframework.stereotype.Component;
@Component
public class Waiter {
	public void service(String name){
		System.out.println("service to " + name);
	}
}

开启扫描:有两种方法可以启用:1、java config配置 2、XML配置 

1. Java config配置

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class ApplicationConfig {
}

2. 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来进行依赖的注入** `Store.class`
package xyz.mrwood.study.spring.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Store {

    @Autowired
    private Waiter waiter;

    public void service(){

        waiter.service("kiwi");
    }
}

  1. spring中所有bean都会有一个ID,我们通过@Component设置的,自动扫描时会以类名首字母小写为ID。如果想要自定义就要设置@Component的参数

   http://blog.csdn.net/u011719271/article/details/53649816

。。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值