Spring配置文件介绍(一)

本文介绍了Spring框架的核心部分,包括Spring的IOC容器和依赖注入概念,以及如何搭建Spring环境。通过一个简单的HelloWorld示例,展示了BeanFactory和ApplicationContext的使用,并解释了如何基于XML文件配置Bean。

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

一、Spring
        Spring是一个轻量级的框架,相当于一个平台性质,大大简化了Java企业级应用的开发,提供了强大的、稳定的功能。Spring框架大约由20个功能模块组成,这些模块被分为6个部分,如图所示:


        Spring Core是最基础部分,提供了IOC特性;Spring AOP是基于Spring Core的符合规范的面向切面编程的实现。

        Spring IOC容器是Spring最核心的部分,IOC(Inversion of Control),控制反转,也被称为依赖注入,是面向对象编程中的一种设计理念,用来降低程序代码之间的耦合度。
        通俗点讲,在实际应用中,多个类之间需要互相依赖才能完成某些特定的业务,而依赖注入就是以配置文件的方式来维护类与类之间的关系,而非硬编码,从而降低了系统之间的耦合,增大了可维护性。
二、搭建环境
        先来一个简单的HelloWorld
        1.下载spring的jar包:http://projects.spring.io/spring-framework/(本文使用的是4.1.4的版本)
        2.创建普通的java项目(不一定非得是web项目,简单起见)
        3.添加jar包:

 4.src下创建spring-config.xml文件(名字可以自己取),创建Student类、test类

(1)Student类:


 
public class Student {
	private int id;
	private String name;
	
	//省略get、set方法
	//为了方便显示,重写了toString方法
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
}

(2)spring-config.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:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util-4.0.xsd
	http://www.springframework.org/schema/p
	http://www.springframework.org/schema/p/spring-p-4.0.xsd">
	
	<!-- 以这种方式来声明Student类的实例 -->
	<bean id="student" class="com.wzj.entity.Student">
		<!-- 为Student对象的属性赋值 -->
		<property name="id" value="1"/>
		<property name="name" value="好人"/>
	</bean>
</beans>


 test类:

 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wzj.entity.Student;
 
public class Test {
	
	public static void main(String[] args) {
		//通过ApplicationContext接口的实现类实例化Spring上下文对象
		ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
		//通过getBean()方法来获取到Student类的对象
		Student student=(Student)context.getBean("student");
		System.out.println(student);
	}
 
}

4)运行结果: Student [id=1, name=好人]


三、Bean配置
1.BeanFactory和ApplicationContext
        Spring框架提供了两种IOC容器的实现:
(1)BeanFactory:IOC容器的基本实现
(2)ApplicationContext(推荐使用):提供了更多的高级特性,是BeanFactory的子接口
        BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;
        ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory

        ApplicationContext的实现类:

——   ClassPathXmlApplicationContext:从 类路径下加载配置文件
——  FileSystemXmlApplicationContext: 从文件系统中加载配置文件
2.配置Bean——基于xml文件的形式
(1)在spring配置文件中以<bean>元素来配置Bean实例,如:
 

<!-- 
		id:Bean的名称,在IOC容器中必须是唯一的,还可以指定多个名字,名字之间用英文逗号、分号或空格隔开
		class:Bean所对的类的全名,其实内部是通过反射来动态创建对象的
	 -->
	<bean id="student" class="com.hy.entity.Student">
		<!-- 为Student对象的属性赋值,value也可以作为子标签 -->
		<property name="id" value="1"/>
	</bean>

(2)获取bean实例:

//getBean()的参数名称要和配置文件中bean的id对应
    Student student=(Student)context.getBean("student");

以上就是我对Spring的一些见解,我们下篇见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值