[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 Sprint boot 学习之一
<properties> <project.build.sourceEncoding>UTF-</project.build.sourceEncoding> < ...
- [Java Sprint] Spring Configuration Using Java
There is no applicationContext.xml file. Too much XML Namespaces helped Enter Java Configuration Cre ...
- [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 ...
- [Java Sprint] Spring XML Configuration : Setter Injection Demo
In CustomerServiceImpl.java, we hardcoded 'HibernateCustomerRepositoryImpl' package com.pluralsight. ...
- 【Other】最近在研究的, Java/Springboot/RPC/JPA等
我的Springboot框架,欢迎关注: https://github.com/junneyang/common-web-starter Dubbo-大波-服务化框架 dubbo_百度搜索 Dubbo ...
- Spring Autowiring by Constructor
In Spring, "Autowiring by Constructor" is actually autowiring by Type in constructor argum ...
- Spring中的Bean的配置形式
Spring中Bean的配置形式有两种,基于XML文件的方式和基于注解的方式. 1.基于XML文件的方式配置Bean <?xml version="1.0" encoding ...
- Bean的自动装配及作用域
1.XML配置里的Bean自动装配 Spring IOC 容器可以自动装配 Bean,需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式.自动装配方式有: by ...
- Alpha冲刺(9/10)——追光的人
1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...
随机推荐
- JPA调用存储过程
@Transactional public BasAccount findByAccount(String account) { System.out.println(account); Query ...
- Importing Objective-C into Swift
Overview You can use Objective-C and Swift files together in a single project, no matter which langu ...
- Linux之基础命令——文件操作
ls(显示指定工作目录下的内容) -a 显示所有文件及目录 包括隐藏文件 -l 除文件名称外,还会将文件类型.权限.拥有者.文件大小等信息详细列出[可以ll简写] -r 将文件以相反次序显示(默认是a ...
- 原生j获取元素的几种方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- sizeof 感知重载,模板具现, 转换规则
问题:如何侦知任意型别 T 是否可以自动转换为型别 U? 方案:侦测转换能力的想法:合并运用 sizeof 和重载函数. 1 依赖 sizeof,sizeof 有着惊人的能力,你可以把 sizeof ...
- 杭电 2111 Saving HDU (贪心)
Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的. 一天,当他正在苦思冥想解困良策的时候,突然想到了自己 ...
- 看板娘 & 二次元 & live2d
live2d https://l2dwidget.js.org/dev.html https://github.com/xiazeyu/live2d-widget.js 看板娘 要切换看板娘吗? ht ...
- 一起去打CS(codevs 5059)
题目描述 Description 早就和lyk约好了去打cs,一直没找着时间,终于今天我家没人,他家也没人,总算可以出去了.但是偏偏天公不作美,某某人非要留那么多题要做.没办法只能尽快做完然后抓紧时间 ...
- android开发里跳过的坑——listview不显示
在蓝牙回调接口public void onLeScan(BluetoothDevice device, int arg1, byte[] arg2)里面调用adpter.notifyDataSetCh ...
- [NOIP2004] 提高组 洛谷P1090 合并果子
题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...