[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. ...
随机推荐
- opencv-flag
http://blog.csdn.net/yiyuehuan/article/details/43701797 在Mat类中定义了这样一个成员变量: /*! includes several bit- ...
- Android原生方式获取经纬度
两种定位方式:GPS定位.WiFi定位优劣: 如果项目定位要求较高还是建议使用三方地图库 GPS定位相比Wifi定位更精准且可在无网络情况下使用,但在室内基本暴毙无法使用WiFi定位没有室内外限制也不 ...
- Oracle中的执行计划
使用autotrace sqlplus系统参数:SQL> set autotrace trace onSQL> select * from dual;DUM---XExecution Pl ...
- hystrix 解决服务雪崩效应
1.服务雪崩效应 默认情况下tomcat只有一个线程池去处理客户端发送的所有服务请求,这样的话在高并发情况下,如果客户端所有的请求堆积到同一个服务接口上, 就会产生tomcat的所有线程去处理该服务接 ...
- SQL Server中 sysobjects、sysolumns、systypes
1.sysobjects 系统对象表. 保存当前数据库的对象,如约束.默认值.日志.规则.存储过程等 在大多数情况下,对你最有用的两个列是Sysobjects.name和Sysobjects.x ...
- 三、spring中高级装配(1)
大概看了一下第三章的内容,我从项目中仔细寻找,始终没有发现哪里有这种配置,但是看完觉得spring还有这么牛B的功能啊,spring的厉害之处,这种设计程序的思想,很让我感慨... 一.环境与prof ...
- HTML location 用法(获取当前URL)
Location 对象 Location 对象包含有关当前 URL 的信息. Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问. 属性 loc ...
- jquery的$().each,$.each的区别02
在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法.两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点. $().each,对于这个方法,在d ...
- 第二次:Ubuntu16.04 安装Docker
sudo apt-get update, 就这一个命令执行了多半天,不知道网络缘故还是怎么的,管他呢,装完总是好的. # step 1: 安装必要的一些系统工具 sudo apt-get update ...
- RNN与情感分类问题实战-加载IMDB数据集
目录 Sentiment Analysis Two approaches Single layer Multi-layers Sentiment Analysis Two approaches Sim ...