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的更多相关文章

  1. java Sprint boot 学习之一

    <properties> <project.build.sourceEncoding>UTF-</project.build.sourceEncoding> < ...

  2. [Java Sprint] Spring Configuration Using Java

    There is no applicationContext.xml file. Too much XML Namespaces helped Enter Java Configuration Cre ...

  3. [Java Sprint] Spring XML Configuration : Constructor Injection Demo

    Previous we see how to do Setter injection: https://www.cnblogs.com/Answer1215/p/9472117.html Now le ...

  4. [Java Sprint] Spring XML Configuration : Setter Injection Demo

    In CustomerServiceImpl.java, we hardcoded 'HibernateCustomerRepositoryImpl' package com.pluralsight. ...

  5. 【Other】最近在研究的, Java/Springboot/RPC/JPA等

    我的Springboot框架,欢迎关注: https://github.com/junneyang/common-web-starter Dubbo-大波-服务化框架 dubbo_百度搜索 Dubbo ...

  6. Spring Autowiring by Constructor

    In Spring, "Autowiring by Constructor" is actually autowiring by Type in constructor argum ...

  7. Spring中的Bean的配置形式

    Spring中Bean的配置形式有两种,基于XML文件的方式和基于注解的方式. 1.基于XML文件的方式配置Bean <?xml version="1.0" encoding ...

  8. Bean的自动装配及作用域

    1.XML配置里的Bean自动装配 Spring IOC 容器可以自动装配 Bean,需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式.自动装配方式有: by ...

  9. Alpha冲刺(9/10)——追光的人

    1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...

随机推荐

  1. 洛谷 P1339 [USACO09OCT]热浪Heat Wave (堆优化dijkstra)

    题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...

  2. ARM开发板如何选型-I.MX6Q开发板

    拥有丰富扩展能力,供货周期长的开发平台,省事安心   处理器:迅为-i.MX6开发板恩智浦Cortex-A9 四核i.MX6Q处理器,主频1GHz,内存2G,存储16GB. 系统支持:i.MX6开发板 ...

  3. OpenFlow_tutorial_3_Learn_Development_Tools

    一.Several Utilities OpenFlow Tutorial VM 中预装了一些OpenFlow特性的工具和一般通用网络的工具. 1.Openflow Controller:处于Open ...

  4. 04Hibernate连接数据库环境配置

    Hibernate连接数据库环境配置

  5. 01CSS使用方法

    CSS使用方法 内联定义 内联定义即是在对象的标记内使用对象的style属性定义适用其的样式表属性. 内部样式表 <style type="text/css"></style> ...

  6. A7. JVM 垃圾回收收集器(GC 收集器)

    [概述] 如果说收集算法是内存回收的方法论,那么垃圾收集器就是内存回收的具体实现.Java 虚拟机规范中对垃圾收集器应该如何实现没有任何规定,因此不同的厂商.不同版本的虚拟机所提供的垃圾处理器都可能会 ...

  7. MyBatis 实现分页功能

    MySQL 的分页功能是基于内存的分页(即查出来所有记录,再按起始位置和页面容量取出结果). 案例:①根据用户名(支持模糊查询).用户角色 id 查询用户列表(即根据用户名称或根据用户角色 id 又或 ...

  8. mysql 存储过程批量删除重复数据

    表结构: LOAD DATA INFILE '/usr/local/phone_imsi_12' replace INTO TABLE tbl_imsi2number_new FIELDS TERMI ...

  9. jQuery的鼠标移入与移出事件

    mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...

  10. 将json格式转为url参数格式的方法(xjl456852整理修改)

    测试页面: <html> <head> <script type="text/javascript" src="jquery-1.11.3. ...