Wednesday, March 23, 2016

Spring

Container Framework, to config bean and maintain relationship between beans

Bean:

javabean
service
action
data source
dao
ioc(inverse of control)
di(dependency injection)



http://maven.springframework.org/release/org/springframework/spring/

Require jar:
spring jar
common.logging.jar

ioc
Inverse of Control: 
Move the control of beans creation, and relationship maintenance from your code to Spring.
So do not need to maintain code, just maintain applicationContext config. This is mainly done by DI in spring.


DI
Dependency Injection





applicationContext.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:context="http://www.springframework.org/schema/context"
 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.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 
 <bean>: 
 when spring framework loads
 spring will auto create a bean instance and put it into memory
-->
<!--
 equivalent to: 
 UserService userService = new UserService();
 userService.setName("abc");
-->
<bean id="userService" class="com.service.UserService">
 <property name="name">
  <value>abc</value>
 </property>
 <!-- 
  ref is to another bean's if, used when the property is also a bean  
  -->
 <property name="bybService" ref="bybService"></property>
</bean>
<bean id="bybService" class="com.service.BybService">
 <property name="name">
  <value>fff</value>
 </property>
</bean>
</beans>

Get a ApplicationContext

  1. ClassPathXmlApplicationContext: by class path
  2. FileSystemXmlApplicationContext: by directory path
  3. XmlWebApplicationContext: for web application to load


Get a Bean from ApplicationContext(Immediately):

//Init all beans, create instances for all singleton beans immediately
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

//Two ways
//class path directory
ApplicationContext ac0 = new ClassPathXmlApplicationContext("com/gvace/impl/beans.xml");

//file system directory
ApplicationContext ac1 = new FileSystemXmlApplicationContext("src/com/gvace/impl/beans.xml"); 

//get configs from multiple files
String[] configLocations = new String[]{"applicationContext.xml","applicationContext-editor.xml"};
ApplicationContext ac2 = new ClassPathXmlApplicationContext(configLocations);
UserService userService = (UserService)ac.getBean("userService");
//get configs by pattern
ac = new ClassPathXmlApplicationContext("applicationContext*.xml");
//get bean by id





Get a Bean from Bean Factory(Delay):

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); 
//Init the bean, but not creating instances

factory.getBean("userService");
//create an instance


Scope

singleton: initialized instance when bean creates by application context
prototype: one bean to many instances
request: get a new instance for a bean in each request
session: get a new instance for a bean in each session
global session: get a new instance for a bean in whole spring container




No comments:

Post a Comment