Auto Wire byType如何在Spring中设置bean?

时间:2018-10-08 06:52:21

标签: spring

这是我的applicationContext.xml配置

<bean name="foo" 
      class="com.bmc.repository.HibernateCustomerRepositoryImpl">
</bean>

<bean name="customerService" class="com.bmc.service.CustomerServiceImpl" autowire="byType">
</bean>

Java代码:

public class CustomerServiceImpl implements CustomerService {

    private CustomerRepository customerRepository;

    public void setFoo(CustomerRepository b) {
        this.customerRepository = b;
    }
}

customerService bean依赖于HibernateCustomerRepositoryImpl。

所以我的问题是:

  

1。。spring如何将HibernateCustomerRepositoryImpl bean与CustomerService绑定?使用setter方法?

     

2。。如果是,Method的名称应该是什么?应该是setPropertyName还是setBeanName?

     

3。。Spring将如何找到setter方法?使用匹配的参数类型?

1 个答案:

答案 0 :(得分:0)

<bean name="foo" id="foo" 
      class="com.bmc.repository.HibernateCustomerRepositoryImpl">
</bean>
<bean name="customerService" class="com.bmc.service.CustomerServiceImpl" autowire="byType">
    <property name="foo" ref="foo"></property>
</bean>