[Java Sprint] Spring XML Configuration : Setter Injection Demo
2023-09-14 09:00:50 时间
In CustomerServiceImpl.java, we hardcoded 'HibernateCustomerRepositoryImpl'
package com.pluralsight.service; ... public class CustomerServiceImpl implements CustomerService { private CustomerRepository customerRepository = new HibernateCustomerRepositoryImpl(); @Override public List<Customer> findAll() { return customerRepository.findAll(); } }
To remove hardcoded Repository, we can use Setter Injection.
First, we defined a setter for 'customerRepository' and remove HibernateCustomerRepositoryImpl():
public class CustomerServiceImpl implements CustomerService { private CustomerRepository customerRepository; public void setCustomerRepository(CustomerRepository customerRepository) { this.customerRepository = customerRepository; } @Override public List<Customer> findAll() { return customerRepository.findAll(); } }
Second, we setter injection in /java/main/resources/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> </bean> </beans>
You can think about each <bean> represent a new Class in Java.
So, first bean:
<bean name="foo" class="com.pluralsight.repository.HibernateCustomerRepositoryImpl"></bean>
reference to HibernateCustomerRepositoryImpl class. Because we want to achieve the same effect:
private CustomerRepository customerRepository = new HibernateCustomerRepositoryImpl();
Second bean 'customerService' is actual a setter injection, we want to inject first bean (HibernateCustomerRepositoryImpl) into it and assign to 'customerRepository' property:
<bean name="customerService" class="com.pluralsight.service.CustomerServiceImpl"> <property name="customerRepository" ref="foo"></property> </bean>
Lastly, we want to use our beans in Application.java:
package com.pluralsight; import com.pluralsight.service.CustomerService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main (String[] args) { // CustomerService service = new CustomerServiceImpl(); // Find the applicationContext.xml file ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // Using application context to replace hardcodedCustomerServiceImpl CustomerService service = appContext.getBean("customerService", CustomerService.class); System.out.println(service.findAll().get(0).getFirstname()); } }
相关文章
- java泛型指定继承和实现
- java volatile关键字的作用_Java并发编程彻底搞懂volatile关键字「建议收藏」
- Java POI 导出Excel经典实现 导出Excel弹出下载框 Spring Boot【不定期更新】
- java scanner怎么用_Java中Scanner类的用法及使用步骤分享!「建议收藏」
- 用JAVA写一个冒泡排序「建议收藏」
- java webservice 实例_Java WebService 简单实例(附实例代码)
- Java 入门练习(36 - 40)
- 【说站】java同步的使用条件
- Java数组循环_java遍历object数组
- Java设计模式之命令模式
- Java-Spring框架-基于xml方式注入属性
- java多线程系列(九)—ArrayBlockingQueue源码分析详解编程语言
- 注册MySQL,让你的Java技能更上一层楼(java注册mysql)
- Java base64 图片编码转换详解编程语言
- Java MySQL数据库安装步骤指南(javamysql下载)
- 时间Redis中Java设定过期时间的技巧(redisjava过期)
- 部署Java开发下的Linux部署(java开发linux)
- Linux与Java结合,打造完美解决方案(linux和java)
- Java桥接Oracle让数据轻松流动(java桥接oracle)
- java字符串求并集的方法