[Java Sprint] AutoWire

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Java Sprint] AutoWire相关的知识,希望对你有一定的参考价值。

Previous we have seen constructore injection: https://www.cnblogs.com/Answer1215/p/9484872.html

 

It would be easier to using autowire to reduce the code, and autowite has four different types:

  • byType
  • byName
  • constructor
  • no

 

First let\'s see how to use \'autowire="constructor"\':

    <bean name="customerService" class="com.pluralsight.service.CustomerServiceImpl" autowire="constructor">
        <!-- <constructor-arg index="0" ref="foo"></constructor-arg> -->
    </bean>

We comment out constructor injection and using autowire.

 

byName:

package com.pluralsight.service;

import com.pluralsight.model.Customer;
import com.pluralsight.repository.CustomerRepository;

import java.util.List;

public class CustomerServiceImpl implements CustomerService {


    //private CustomerRepository customerRepository = new HibernateCustomerRepositoryImpl();
    private CustomerRepository customerRepository;

    public CustomerServiceImpl () {

    }

    public CustomerServiceImpl (CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

 // if set autowire by name, so in applicationContext <bean name="customerRepository" ..>
// if <bean name="foo" ..> then this function should be rename public void setFoo(CustomerRepository customerRepository)
    public void setCustomerRepository(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }


    @Override
    public List<Customer> findAll() {
        return customerRepository.findAll();
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Define a class, using implementation-->
    <bean name="customerRepository" class="com.pluralsight.repository.HibernateCustomerRepositoryImpl"></bean>


    <!-- Setter injection: Inject HibernateCustomerRepositoryImpl to customerRepository -->
    <bean name="customerService" class="com.pluralsight.service.CustomerServiceImpl" autowire="byName">
        <!--<property name="customerRepository" ref="foo"></property>-->
        <!-- <constructor-arg index="0" ref="foo"></constructor-arg> -->
    </bean>
</beans>

 

byType:

    <bean name="customerService" class="com.pluralsight.service.CustomerServiceImpl" autowire="byType">
        <!--<property name="customerRepository" ref="foo"></property>-->
        <!-- <constructor-arg index="0" ref="foo"></constructor-arg> -->
    </bean>

It doesn\'t matter we use \'name="customerService"\' or \'name="foo"\', because it finding by type, so still will work.

以上是关于[Java Sprint] AutoWire的主要内容,如果未能解决你的问题,请参考以下文章

java spring component与autowire区别

如何在 graphql-java-tools Resolver 中 @Autowire 一个 JPA 存储库

@Autowired注解(转)

[Spring] Autowire

Java--使用@Autowired报错Could not autowire. No beans of ‘XX‘ type found.

Could not autowire field: private java.lang.Integer com.taotao.sso.service.impl.UserServiceImpl.SSO_