Spring XML Configuration

XML

  • First Approach
  • Simpler
  • Separation of Concerns

--------------------------------------------------------------

Copy Demo

image.png

改pom.xml



-------------------------------------------------------------

applicationContext.xml

  • Name doesn't matter
  • Spring Context sort of a HashMap
  • Can simply be a registry
  • XML configuration begins with this file
  • Namespaces aid in configuration/validation


--------------------------------------------------------------

Application Context Demo

resources -》New -》 Spring Bean Configuration File
image.png
image.png


Namespaces



XML Declaration


Beans

  • Essentially Classes
  • Replaces keyword new
  • Define Class, use Interface
<bean name="customerRepository" class="com.pluralsight.repository.HibernateCustomerRepositoryImpl" />

---------------------------------------------------------------------------------

Setter Injection

  • Setter Injection
  • Constructor Injection
  • Used together
  • Better for Existing Code

------------------------------------------------------------------------

Setter Injection Demo


=====》》

Source -》Sort Members

applicationContext.xml


Application.java

<?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">

	<bean name="foo"
		class="com.pluralsight.repository.HibernateCustomerRepositoryImpl" />

	<bean name="customerService"
		class="com.pluralsight.service.CustomerServiceImpl">
		<property name="customerRepository" ref="foo"></property>
	</bean>

</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.pluralsight.service.CustomerService;
import com.pluralsight.service.CustomerServiceImpl;

public class Application {

	public static void main(String[] args) {
		
		//CustomerService service = new CustomerServiceImpl();
		
		ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		CustomerService service = appContext.getBean("customerService", CustomerService.class);
		
		System.out.println(service.findAll().get(0).getName());
	}
}

--------------------------------------------------------------------------

Constructor Injection

  • Guaranteed Contract
  • Constructor Defined
  • Used together
  • Index based

xml

class

Appliction.java



--------------------------------------------------------------------------

Autowire

  • Spring Automatically Wires Beans
  • byType
  • byName
  • constructor
  • no

Autowire by Constructor


Autowire by Name and Type



By name


===============》



--------------------------------------------------------------------------

Summary

  • applicationContext.xml
  • Bean Definition
  • Setter Injection
  • Constructor Injection
  • Auto wiring

----------------------------------------------------------------------

Annotation Configuration Using XML
https://blog.csdn.net/u012596785/article/details/79811114








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值