Pro Spring2.5 Over view ---- Ioc DI

1. Spring 包的組成:

 

1> spring-aop.jar This JAR contains all the classes you need to use Spring’s AOP features within your application. You also need to include this JAR in your application if you plan to use other features in Spring that use AOP, such as declarative transaction management.

 

2> spring-beans.jar: This archive contains all of Spring’s dependency injection. It contains the bean factories and supporting classes. In most cases, you will need to add spring-context.jar, which contains code needed to build the application context.

 

3> spring-context.jar: This JAR contains code needed to build the Spring application context; it packages the main ApplicationConext interface and its implementations together with code for instrumentation, JNDI,scheduling, themes, and validation.

 

4> spring-context-support.jar: This package contains utility Spring code—this means caching, instrumentation, e-mail and scheduling support, and the very interesting scripting languages support.

 

5> spring-core.jar: This contains the core files of the Spring Framework: it deals with annotations, enumerations, task execution, resource loading, and other utilities and exceptions you may find useful even outside the context of the Spring Framework.

 

6> spring-jdbc.jar: This package contains code for the JDBC support classes, namely the JdbcTemplate and JdbcDaoSupport classes

 

7> spring-jms.jar: This JAR contains code for JMS;

 

8> spring-orm.jar: This archive contains the files needed for object-relational mapping (ORM) tools. Including this package on your classpath will give you Spring support for Hibernate 3, iBATIS, JDO, JPA, and TopLink.

 

9> spring-test.jar: This package contains support code to write unit and integration tests using the Spring Framework. It supports the JUnit 3, JUnit 4, and TestNG testing frameworks. In addition, you can use classes from the org.springframework.mock package, which represent mock implementations of JNDI and web classes.

 

10> spring-tx.jar: This one contains support for core data access exceptions and transaction technologies. These two areas are closely bound together, because the transactions generally work with some data access code.

 

2. Ico 介紹:

 

Ioc 是 Inversion of control 簡寫。 DI 是 Dependency Injection 的縮寫。還有事 dependency lookup 依賴查找。 DI 和 DL是IOC 的兩種形式。DL和現有代碼的是耦合的, DI 和代碼是完全解耦的。

 

DI 分爲構造器(Constructor)DI 和setter DI.

 

beanFactory 是DI 的核心容器。bean是容器管理的組件(Component)。

 

一般的系統和spring交互都是通過beanfactory接口來完成的。

 

對於所有的beanfactory類來說,都要實現BeanDefinitionRegistry接口。

 

你可以讀取beanDefinition 數據從配置文件中。

 

創建beanfactory的方式之一:


XmlBeanFactory bf = new XmlBeanFactory(
new ClassPathResource("/META-INF/spring/beanfactorydemo1-context.xml"));

 

 

構造器的形式的注入的xml配置例子:

 

<bean id="encyclopedia"
class="com.apress.prospring2.ch03.di.ConfigurableEncyclopedia">
<constructor-arg>
<util:map>
<entry key="AgeOfUniverse" value="13700000000"/>
<entry key="ConstantOfLife" value="326190476"/>
</util:map>
</constructor-arg>

</bean>
<bean id="oracle" class="com.apress.prospring2.ch03.di.BookwormOracle">
<property name="encyclopedia" ref="encyclopedia"/>
</bean>

 

 

設置注入的類型的例子

 

<bean id="constructorConfusion"
class="com.apress.prospring2.ch03.beanfactory.ConstructorConfusionDemo">
<constructor-arg value="1" type="int" />
</bean>

 

配置屬性值的例子:

 

<bean id="injectSimpleDemo"
class="com.apress.prospring2.ch03.beanfactory.InjectSimpleDemo">
<property name="name" value="John Smith"/>
<property name="age" value="35"/>
<property name="height" value="1.79"/>
<property name="isProgrammer" value="true"/>
<property name="ageInSeconds" value="1103760000"/>

</bean>

 

用p 命名空間(namespace)的方式設置屬性

 

<bean id="injectSimpleDemo"
class="com.apress.prospring2.ch03.beanfactory.InjectSimpleDemo"
p:age="35" p:ageInSeconds="1103760000"
p:height="1.79" p:isProgrammer="false"
/>

 

用p命名空間設置bean的引用 p:name-ref ="ref_name"

 

<bean id="oracle" class="com.apress.prospring2.ch03.di.BookwormOracle"
p:encyclopedia-ref ="knowitall" />

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章