spring-IOC容器(三)

一、通过工厂方法配置Bean:

.xml

    <!-- 
        class属性:指向静态工厂方法的全类名
        factory-method:指向静态工厂方法的名字
        constructor-arg:如果工厂方法需要传递参数,则用constructor-arg来配置参数
     -->
    <!-- 通过静态工厂方法配置bean,不是配置静态工厂方法实例,而是配置bean实例 -->
    <bean id="car1" class="com.atguigu.spring.beans.factory.StaticCarFactory" 
    factory-method="getCar">
        <constructor-arg value="ford"></constructor-arg>
    </bean>
    
    <!-- 配置工厂的实例 -->
    <bean id="carFactory" class="com.atguigu.spring.beans.factory.InstanceCarFactory"></bean>
    <!-- 通过实例工厂方法来配置bean -->
    <bean id="car2" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="audi"></constructor-arg>
    </bean>

.java

1、静态工厂方法:

/**
 * 静态工厂方法:直接调用某一个类的静态方法就可以返回bean的实例
 */
public class StaticCarFactory {
    private static Map<String, Car> cars = new HashMap<String,Car>();
    
    static {
        cars.put("audi", new Car("audi",400000.0));
        cars.put("ford", new Car("ford",230000.0));
    }
    
    //静态工厂方法
    public static Car getCar(String name) {
        return cars.get(name);
    }
}

2、实例工厂方法:

/*
 * 实例工厂方法:实例工厂的方法,即需要创建工厂本身,再调用工厂的实例方法来返回bean的实例
 * */
public class InstanceCarFactory {

    private Map<String, Car> cars = null;
    
    public InstanceCarFactory() {
        cars = new HashMap<String,Car>();
        cars.put("audi", new Car("audi", 620000.0));
        cars.put("ford", new Car("ford", 340000.0));
    }
    
    //实例工厂方法
    public Car getCar(String brand) {
        return cars.get(brand);
    }
}

二、通过FactoryBean配置Bean:

.xml:

<!--通过FactoryBean来配置Bean的实例
      class: 指向FactoryBean的全类名
      property: 配置FactoryBean的属性
      但实际返回的却是FactoryBean的getObject()方法返回的实例
-->
<bean id="car" class="com.atguigu.spring.beans.factorybean.CarFactoryBean">
    <property name="brand" value="BMW"></property>
</bean>

.java:

需要实现FactoryBean<T>接口

public calss XXX implements FactoryBean<T>{
}

三、通过注解配置Bean:

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

@Respository:标识持久层(数据持久层)组件;

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

@Controller:标识表现层组件; 

<!-- 需要引用 context命名空间
     base-package:指定Spring IOC 容器扫描的包
     resource-pattern:指定扫描的资源-->
<context:component-scan base-package="com.atguigu.spring.beans.collection.Person"
    resource-pattern="repository/*.class">
</context:component-scan>
<!-- context:exclude-filter:子节点指定排除哪些指定表达式的组件
     context:include-filter: 子节点指定包含哪些指定表达式的组件,需要和use-default-filters配合使用-->
<context:component-scan base-package="com.atguigu.spring.beans.collection.Person"
    use-default-filters="false">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    <context:exclude-filter type="assignable" expression="com.atguigu.spring.beans.collection.Person2"/>
    <!-- 使用context:include-filter时,需要把use-default-filters设置为false(默认为true) -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    <context:include-filter type="assignable" expression="com.atguigu.spring.beans.collection.Person2"/>
</context:component-scan>

@Autowired注解自动装配具有兼容类型的单个Bean属性(构造器,普通字段即使非public,一切具有参数的方法都可以应用@Autowired注解)

默认情况下,所有使用@Autowired注解的属性都需要设置,当Spring找不到匹配的Bean装配属性时,会抛异常,若某一属性允许不被设置,可以设置@Autowired注解的required属性为false。

默认情况下,当IOC容器里存在多个类型兼容的Bean时,通过类型的自动装配将无法工作,此时可以在@Qualifier注解里提供Bean的名称。Spring允许对方法的入参标准@Qualifier已指定注入Bean的名称。

@Autowired 注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配

@Autowired 注解也可以应用在集合属性上,此时的Spring读取该集合的类型信息,然后自动装配所有与之兼容的Bean

@Autowired 注解用在java.util.Map上时,若该Map的键值为String,那么Spring将自动装配与之Map值类型的Bean,此时Bean的名称作为键值

Spring还支持@Resource和@Inject注解,这两个注解和@Autowired注解功能类似(@Resource注解要求提供一个Bean名称的属性)

@Resource(name = "sysConfigItemDaoImpl")
@Autowired()
@Qualifier("sysConfigItemDaoImpl")
public void setDao(SysConfigItem sysConfigItemDao) {
        this.sysConfigItem = sysConfigItem;
    }

 四、泛型依赖注入:

BaseService<T>和BaseRepository<T>