spring概述

 

一、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,就从这个容器里面获取即可。


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值