`

spring概述

    博客分类:
  • J2EE
阅读更多

 

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


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics