Java Config与xml的区别

本文探讨了Spring框架中Java Config与XML配置的区别,包括表达形式、bean定义注册和依赖注入关系的差异。文章通过代码示例展示了XML与Java Config在配置bean和处理依赖关系时的不同,并介绍了@Configuration、ComponentScan、@PropertySource等关键注解的用法。最后,作者预告了接下来将继续揭秘Spring Boot的相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot系列文章

第一章:初识JavaConfig,回顾Spring XML



回顾与探索Spring框架本质

Spring Boot作为开发者的利器,在企业的开发中越来越受欢迎,简化Spring应用的初始搭建和开发过程,该框架使用了特定的配置方式,让开发者不再局限于模板化的配置,下面我们来聊一聊Spring原始配置方式和Spring Boot的新配置方式有什么异同。


种一棵树最好的时间是10年前,其次就是现在,加油!
                                                                                   --by蜡笔小柯南

一、Spring IOC其实很简单

什么是IOC?

控制反转,在传统的Java开发模式中,当我们需要一个对象时,需要手动的去new一个对象,而在Spring的开发模式中,Spring容器使用了工厂模式为我们创建了所需要的对象,由Spring负责程序之间的关系,而不是由开发者的代码直接控制,这样,控制权就由开发者转到了Spring的手中,这就是控制反转。

1.代码实现

在使用Spring的过程中,一个典型的代码片段就是:

public class Demo{
	public static void mian(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		UserService userService = (UserService)context.getBean(UserService.class);
		userService.doSomething();
	}
}

那么这个beans.xml中具体写啥呢,这就涉及到了bean的注册,通过xml的方式定义一些bean,然后通过手动组装或者Spring的自动扫描机制,将bean注册到Spring的容器中

  • 以xml的方式来注册 一个单一的bean,示例如下:
<bean id = "userService" class = "..UserServiceImpl">
	...
</bean>

如果需要很多个bean,这样手动收集的方式太过于繁琐,所以,这里有了另一种方式

  • 通过扫描的方式注入多个bean
<!-- base-package填写包名 -->
<context:component-scan base-package = "com.yogurt">

二、初见Java Config

1.表达形式层面

基于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"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- bean定义 -->

</beans>

基于Java Config形式是这样的(示例):

@Configuration
public class MyConfiguration{
	bean定义
}

任何一个标注了@Configuration的Java类都定义为一个Java Config配置类

2.注册bean定义层面

基于xml形式是这样的(示例):

<bean id = "userService" class = "..UserServiceImpl">
	...
</bean>

基于Java Config形式是这样的(示例):

@Configuration
public class MyConfiguration{
	@Bean
	public UserService userService(){
		return new UserServiceImpl();
	}
}

任何一个标注了@Bean的方法,其返回值将作为一个bean定义注册到Spring IOC的容器中,方法名默认为定义该bean的id

3.表达依赖注入关系层面

在xml中bean与bean之间的依赖关系是这样的(XML):

<bean id = "userService" class = "..UserServiceImpl">
	<property name="dependencyService" ref="dependencyServiceImpl"/>
</bean>
<bean id="dependencyService" class="dependencyServiceImpl" />

在JavaConfig中bean与bean之间的依赖关系是这样的(JavaConfig):

@Configuration
public class MyConfiguration{
	@Bean
	public UserService userService(){
		return new UserServiceImpl(dependencyService());
	}
	@Bean
	public DependencyService dependencyService(){
		return new dependencyServiceImpl();
	}
}

如果一个bean的定义依赖其他的bean,则直接调用JavaConfig类中的依赖bean的创建方法就ok了

三、常用的Annotation

@Configuration我们已经见过,这里不再过多讲解

1.ComponentScan

@ComponentScan对应XML配置文件中 context:component-scan 元素,对添加了对应注解的类,如@Component,@Service,@Controller,@Repository,@Dao,将这些bean的定义注册到Spring的容器中,bean的默认id为将类名的第一个字母变为小写,其余不变,作为bean自己的id

2.@PropertySource与@PropertySources

@PropertySource用于从某些地方加载.properties文件,并将其中的属性加载到Spring容器中

// jdk8或者更高

@Configuration
@PropertySource("classpath:1.properties")
@PropertySource("classpath:2.properties")
@PropertySource("...")
public class MConfiguration{
	...
}

// jdk7及以下
@PropertySources({
	@PropertySource("classpath:1.properties"),
	@PropertySource("classpath:2.properties")
})
public class MConfiguration{
	...
}

3.@Import与@ImportResource

在XML的配置中,通过 <import resource=“xx.xml”/>将多个分开的容器配置合到统一的一个配置文件中,在JavaConfig中使用以下的方法

@Configuration
@Import(MyConfiguration.class)
public class MConfiguration{
	...
}

@Import只负责引入JavaConfig形式定义的IOC容器配置,如果有一些遗留的或者需要使用XML形式来配置的话,需要用@ImportResource将它们合并到一起

@Configuration
@Import(MyConfiguration.class)
@ImportResource("...")
public class MConfiguration{
	...
}

总结

我们主要回顾了Spring的常用配置方法,通过使用XML的方式和JavaConfig的方式来分别介绍了它们所对应的Spring和Spring Boot。接下来,我将用系列文章来揭秘Spring Boot的神秘面纱!
要想生活过得去,结尾总得带点绿···

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值