Spring3.1からの新機能
Spring3.1からの新機能
Bean定義のプロファイル機能
Bean定義をプロファイルという形でグループ化、有効範囲を指定できる機能。<?xml version="1.0" encoding="UTF-8" ?> <beans ...> <beans profile="test"> <bean id="dataSource" class="org.springframework...DriverManagerDataSource" /> <bean ...> </beans> <beans profile="production"> <jee:jndi-lookup id="dataSource" jndi-name="..." /> <bean ...> </beans>
テストの時はDIコンテナの作成時にtestを指定。
GenericXmlApplicationContext factory = new GenericXmlApplicationContext(); factory.getEnvironment().setActiveProfiles("test"); factory.load("classpath:/META-INF/spring/beans.xml"); factory.refresh();
UTのときはSpringJUnit4ClassRunnerでtestを指定。
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:/META-INF/spring/bean.xml") @ActiveProfiles("test") public class BeanProfileSpringTest{... Webアプリ(web.xml)で指定(ContextLoaderListenerの場合)。 >|xml| <context-param> <param-name>spring.profiles.active</parame-name> <param-value>production</param-value> </context-param>
Webアプリ(web.xml)で指定(DispatcherServletの場合)。
<servlet> <servlet-name>dispatcherServlet</servlet-name> ... <init-param> <param-name>spring.profiles.active</parame-name> <param-value>test</param-value> </init-param> ... </servlet>
組込みデータベースサポート
<?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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"> ... <jdbc:embedded-database id="dataSource"> <jdbc:script location="classpath:/META-INF/sql/ddl.sql"/> <jdbc:script location="classpath:/META-INF/sql/data.sql"/> </jdbc:embedded-database> ... </beans>
Object/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:jdbc="http://www.springframework.org/schema/oxm" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-jdbc-3.1.xsd"> <oxm:jaxb2-marshaller id="marshaller"> <oxm:class-to-be-bound name="sample.customer.biz.domain.Customer" /> </oxm:jaxb2-marshaller> <bean id="marshallingConverter" class="org.springframeowrk.http.converter.xml.MarshallingHttpMessageConverter"> <property name="marshaller" ref="marshaller" /> <property name="unmarshaller" ref="marshaller" /> </bean> </beans>
-
- Castor
- XMLBean
- JiBX
- XStream
Cache Abstraction機能
- キャッシュを抽象化する機能
- CacheManagerをBean定義ファイルに設定
- アノテーションやBean定義でキャッシュを実現
<?xml version="1.0" encoding=!UTF-8"?> <beans ... xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" ... http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd""> <!-- アノテーション利用時 --> <cache:annotation-driven /> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <list> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"> <property name="name" value="users" /> </bean> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"> ... </list> </property> </bean> ...
@Cacheable(value="users", key="#userId") public User findById(long userId){ ...
@CacheEvict(value="users", key="#user.id") public void update(User user){ ...
キャッシュの設定をソースに書きたくない場合。
<cache:advice id="cacheAdvice"> <cache:caching cache="users"> <cache:cacheable method="findById" key="#userId" /> <cache:cache-evict method="update" key="#user.id" /> ... </cache:advice>
非同期実行/スケジューリング実行
<beans ... xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" ... http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd""> <task:annotation-driven />
@Async public void foo() { // 戻り値を指定してもnullしか戻らない
@Async public Future<String> foo() { ... String result = ... return new AsyncResult<String>(result); } Future<String> asyncResult = object.foo(); ... String result = asyncResult.get();