[Java Sprint] Spring XML Configuration : Constructor Injection Demo
2023-09-14 09:00:50 时间
Previous we see how to do Setter injection: https://www.cnblogs.com/Answer1215/p/9472117.html
Now let's see how to cover setter injection to coustructor injection. Notice, don't need to compare which one is better, you can use both.
Different from setter injection which use 'name', constructor injection using 'index'.
applicationContext.xml
<?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="foo" class="com.pluralsight.repository.HibernateCustomerRepositoryImpl"></bean> <!-- Setter injection: Inject HibernateCustomerRepositoryImpl to customerRepository --> <bean name="customerService" class="com.pluralsight.service.CustomerServiceImpl"> <!--<property name="customerRepository" ref="foo"></property>--> <constructor-arg index="0" ref="foo"></constructor-arg> </bean> </beans>
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; } /* public void setCustomerRepository(CustomerRepository customerRepository) { this.customerRepository = customerRepository; } */ @Override public List<Customer> findAll() { return customerRepository.findAll(); } }
相关文章
- java random函数原理_详解JAVA中Random()函数的用法
- java 怎样卸载一个类_Java 动态卸载类[通俗易懂]
- java helloworld源代码_Java Hello World源代码剖析
- java motherfree video_Java Config 下的Spring Test方式
- java中static关键字的作用_Java:Java中static关键字作用
- Java后台接收参数出现java.lang.Integer cannot be cast to java.lang.Double错误(已解决)[通俗易懂]
- java获取服务器路径_Java获取此次请求URL以及服务器根路径的方法「建议收藏」
- java 生成xml dom4j_Java生成xml——DOM4J生成
- java获取窗口_获取窗口句柄[通俗易懂]
- java spel_SPEL表达式注入-入门篇
- Java 零拷贝_java高性能编程
- java 文件上传到服务器_Java上传文件到服务器端的方法「建议收藏」
- VS Code Java 7 月更新:Lombok 支持重大提升, Spring 增强新功能!
- 「 [Java] Public与class的区别 」
- Java Netty框架自建DNS代理服务器教程
- Java-Spring框架-基于xml方式注入属性
- Java中的异常
- java使用ImageReader判断图片的类型详解编程语言
- 程序Linux下构建Java应用:从入门到精通(linux下开发java)
- java调用http接口并返回json对象详解编程语言
- Java多线程15:Queue、BlockingQueue以及利用BlockingQueue实现生产者/消费者模型详解编程语言
- 安装Java程序轻松搞定Linux系统快速上手(安装javalinux)
- MySQL驱动程序:Java集成简易操作(mysql的java驱动)
- Java实现Redis事务管理(redis事务java)
- Java程序员的MySQL数据库之旅(java操作mysql数据库)
- JAVA/JSP学习系列之四(OrionAppServer的安装)
- java中spring定时任务实现代码