一、Spring概述
The Spring Framework is a lightweight solution and a potential one-stop-shop for building your enterprise-ready applications.However, Spring is modular, allowing you to use only those parts that you need, without having to bring in the rest.
Spring is designed to be non-intrusive, meaning that your domain logic code generally has no dependencies on the framework itself.
二、Ioc和DI(控制反转和依赖注入)
The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans.The configuration metadata is represented in XML, Java annotations, or Java code.
1.配置文件的格式有3种:
(1)基本的xml格式;
(2)Annotation-based configuration: Spring 2.5 introduced support for annotation-based
configuration metadata.
(3)ava-based configuration: Starting with Spring 3.0, many features provided by the Spring
JavaConfig project became part of the core Spring Framework. Thus you can define beans
external to your application classes by using Java rather than XML files. To use these new
features, see the @Configuration, @Bean, @Import and @DependsOn annotations.
xml格式的配置文件
Spring configuration consists of at least one and typically more than one bean definition that the container must manage. XML-based configuration metadata shows these beans configured as <bean/> elements inside a top-level <beans/> element.
例如:
<?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: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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="Chinese" class="com.test.Chinese"/>
<bean id="American" class="com.test.American">
</bean>
</beans>
2.初始化容器
Instantiating a Spring IoC container is straightforward. The location path or paths supplied to an ApplicationContext constructor are actually resource strings that allow the container to load configuration metadata from a variety of external resources such as the local file system, from the Java CLASSPATH, and so on.
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
三、配置文件
1.配置文件可以有多个,然后在一个配置文件中把其他的配置文件引入,例如:
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
2.或者直接用构造函数引入多个配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml",
"daos.xml"});
四、总结
springIoc其实就是个容器,它根据配置文件创建bean,用户想使用哪个bean,就从这个容器里面获取即可。
分享到:
相关推荐
Spring概述与IOC,对于刚入门的工程师有用
Spring概述ppt课件.ppt
spring概述_spring_.zip
Java进阶之Spring概述共3页.pdf.zip
T22.2_Spring 概述 java 经典教程 经典教材
Spring概述 ①Spring是一个开源框架 ②Spring为简化企业级开发而生,使用Spring,JavaBean就可以实现很多以前要靠EJB才能实现的功能。同样的功能,在EJB中要通过繁琐的配置和复杂的代码才能够实现,而在Spring中却...
软件开发框架(J2EE)Spring概述习题与答案.docx软件开发框架(J2EE)Spring概述习题与答案.docx
软件开发框架(J2EE)Spring概述习题与答案.pdf软件开发框架(J2EE)Spring概述习题与答案.pdf
Spring概述开源架构源码2021.pdf
文档附带代码,具体可查看 http://blog.csdn.net/partner4java 中 《partner4java 讲述Spring入门》之第一步:Spring概述与Spring IoC
软件开发框架(J2EE)Spring概述实训指导书.pdf软件开发框架(J2EE)Spring概述实训指导书.pdf
AVA视频教程,第一课Spring001_Spring概述,动力节点,王勇讲。该视频收录于《【免豆】动力节点_王勇_JAVA视频教程_Spring(48讲)》
J2EE Spring 概述
spring框架简介,介绍sprng发展历程、优势、体系结构、概述
Spring的一些基本概念的介绍,例如IoC,AOP等,并介绍了这么使用Spring
本书首先是一本通过通俗案例讲解Spring的教程;同时也是一本深入挖掘Spring及相关框架结构、设计原理;更是一本探讨J2EE软件开发中的艺术的书。本书还想讲述一条开源框架设计中金科玉律:思想决定一切,万变不离其宗...
方便解耦、简化开发 Spring就是一个大工厂,可以将所有对象的创建和依赖关系的维护工作都交给Spring容器管理,大大的降低了组件之间的耦合性。 支持AOP Spring提供了对AOP的支持,它允许将一些通用任务,如安全、...
1、Spring概述 2、一切从bean开始 3、俯瞰Spring架构设计 4、Spring源码下载 二阶段 1、什么是IOC/DI 2、SpringIOC体系结构 3、源码分析-IOC容器的初始化 4、源码分析-IOC容器的依赖注入 5、源码分析-IOC容器的高级...