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. 按键精灵txt判断

      句子 = "度阿斯达娘阿婶是大的百度知道" 词 = "百度知道" MyPos = Instr(句子, 词) If MyPos > 0 Then Tra ...

  2. leetcode_951. Flip Equivalent Binary Trees_二叉树遍历

    https://leetcode.com/problems/flip-equivalent-binary-trees/ 判断两棵二叉树是否等价:若两棵二叉树可以通过任意次的交换任意节点的左右子树变为相 ...

  3. Java Web数据库篇之MySQL特性

    MySQL ExplainEXPLAIN 命令的输出内容大致如下: mysql> explain select * from user_info where id = 2\G********** ...

  4. C#反射的使用

    1.先定义个类,编译成dll,用于调用 nameSpace Test{ public class Class1 { private string _name; private int _age; pu ...

  5. python 实现代理服务器

    # encoding:utf-8 import socket import thread import re def getAddr(d): a = re.search("Host: (.* ...

  6. shell 循环 read line

    cat dockerlist |while read line;do docker rmi  $line ;done

  7. mysql事件【定时器】

    一,借鉴[luo奔的蜗牛] 1.创建一张表 create table mytable ( id int auto_increment not null, name ) not null default ...

  8. mysql limit关键字

    select * from table_name limit [index, ] length; limit后面跟2个参数: index:索引号,从0开始计算,表示从哪一行开始: length:长度, ...

  9. go的指针学习

    1)指针是什么? 一个指针变量可以指向任何一个值的内存地址它指向那个值的内存地址 说白了就是可以先存储内存的地址,在用内存地址找到对应值 2)go中的使用 Go 语言的取地址符是 &,放到一个 ...

  10. -- HTML标记大全参考手册[推荐]

    --  HTML标记大全参考手册[推荐]总类(所有HTML文件都有的) 文件类型 <HTML></HTML> (放在档案的开头与结尾) 文件主题 <TITLE>&l ...