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. 合并百度影音的离线数据 with python 第二版 基于yield

    重新整理了一下代码. 增加了bdv,mkv的处理流程. 目前暂时支持windows平台. bdv,mkv,rmvb的不同处理流程 # -*- coding: UTF-8 -*- import os i ...

  2. 博客之旅 gogogo!

    听说写博客的人都很牛~ 上班一年多了,想记录点什么,so,就写博客吧,整理一些技术点与工作生活心得 欢迎各位道友交流学习 :)

  3. Seating Arrangement

    1997: Seating Arrangement Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 543     Solved:  ...

  4. CREATE OPERATOR - 定义一个新的操作符

    SYNOPSIS CREATE OPERATOR name ( PROCEDURE = funcname [, LEFTARG = lefttype ] [, RIGHTARG = righttype ...

  5. gitlab数据迁移到docker容器

    需求:想把服务器上的gitlab迁移到docker容器里面注意:gitlab的迁移,必须保持gitlab的版本一致,此处使用的是:8.4.3,数据库版本和类型一致,此处使用的是postgresql 9 ...

  6. 20181017 work - Inno Setup 禅道 xp系统 楷体字体 win10倒计时 nwjs chosen

    win10 闹钟和时钟 用个倒计时 Inno Setup 编译器 把应用程序打包成安装程序 nwjs-v0.14.7-win-ia32 XP专用 chrome用的49的版本 作用是把浏览器地址的程序打 ...

  7. 常用的HTTP方法有哪些?

    GET: 用于请求访问已经被URI(统一资源标识符)识别的资源,可以通过URL传参给服务器POST:用于传输数据给服务器,主要功能与GET方法类似,但一般推荐使用POST方式.PUT: 传输数据,报文 ...

  8. 04CSS文本字体及排版

    CSS文本字体 字体——font-family font-family:字体1,字体2,字体3,……:应用font-family属性可以一次定义多个字体,而在浏览器读取字体时, 会按照定义的先后顺序来 ...

  9. JavaSE-18 常用工具类

    学习要点 Object类 枚举 包装类 Math类 Random类 字符串处理 日期时间 Object类 1  什么是Object类 Object类存储在java.lang包中,是所有java类(Ob ...

  10. pom.xml配置引用项目时不生效

    1 在项目pom.xml配置中引用项目A,但是编译时,取提数引起是B: 2 原因是:[Java Build Path - Projects] 引用的还是老的项目B,删除该引用即可解决.