インテグレーションについて説明します。
RestTemplateクラスを使用して簡単にRESTfulなアクセスが実現できます。
例
サーバー側:
@RequestMapping(value="/user/{userId}", method=RequestMethod.GET) |
クライアント側:
RestTemplate template = new RestTemplate();
String result = restTemplate.getForObject("http://foo.bar/apl/user/{userId}", String.class,"1"); |
リモーティング機能を使用して、Spring Frameworkアプリケーションから別プロセスのGlassFish Serverクラスターへ配備されたSpring Frameworkアプリケーションの呼び出しを実現できます。
本機能を利用するという方法で、アプリケーションの分離が可能になるため、負荷分散などのスケーラブルな運用ができます。本製品ではリモーティング機能として次の2つの方式をサポートします。
HttpInvoker
JmsInvoker
注意
リモーティング機能は、将来的に機能が廃止されるため非推奨となりました。新規使用を控え、現在使用している場合は他の方法への移行を検討してください。
例
HttpInvokerの定義例(Web側)
■applicationContext.xmlまたはサーブレット名-servlet.xml
… <!-- 業務アプリケーションで使用するBeanの定義--> <bean id="businessService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://localhost/backapp/remoting/httpinvokeapp" /> <property name="serviceInterface" value="test.ListService" /> </bean> … |
HttpInvokerの定義例(AP側)
■web.xml
<?xml version="1.0" encoding="Shift_JIS"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>httpinvokeapp</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>httpinvokeapp</servlet-name> <url-pattern>/remoting/httpinvokeapp</url-pattern> </servlet-mapping> </web-app> |
注) servlet-classにはDispatcherServletではなくHttpRequestHandlerServletを使用してください。
■applicationContext.xml
… <!-- 業務アプリケーションで使用するBeanの定義--> <bean id="businessService" class="test.ListServiceImpl"> <property name="employeeDao" ref="employeeDao00"/> </bean> <bean id="employeeDao00" class="test.EmployeeDaoImpl" /> <bean name="httpinvokeapp" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="businessService" /> <property name="serviceInterface" value="test.ListService" /> </bean> … |
注) bean名とサーブレット名を同一の名前にしてください。
JmsInvokerの定義例(Web側)
■applicationContext.xmlまたはサーブレット名-servlet.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:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springbeans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd"> … <!-- 業務アプリケーションで使用するBeanの定義--> <jee:jndi-lookup id="connectionFactory" jndi-name="jmsfactory01"/> <jee:jndi-lookup id="messageQueue" jndi-name="queue01"/> <bean id="businessService" class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean"> <property name="serviceInterface" value="test.ListService" /> <property name="connectionFactory" ref="connectionFactory"/> <property name="queue" ref="messageQueue"/> </bean> </beans> |
JmsInvokerの定義例(AP側)
■applicationContext.xmlまたはサーブレット名-servlet.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:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springbeans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd"> … <!--業務アプリケーションで使用するBeanの定義--> <jee:jndi-lookup id="connectionFactory" jndi-name="jmsfactory01"/> <jee:jndi-lookup id="messageQueue" jndi-name="queue01"/> <bean id="businessService" class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean"> <property name="serviceInterface" value="test.ListService" /> <property name="connectionFactory" ref="connectionFactory"/> <property name="queue" ref="messageQueue"/> </bean> </beans> |
EJB連携を使用することで、EJB 3.0以前の資産をPlain Old Java Object(POJO)のように扱うことができます。また、EJB 3.xの資産との連携も可能です。
例
Bean定義ファイルに連携先のEJB資産を指定します。
Bean定義ファイル:
<bean id ="testEJBLocal" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean"> <!-- Beanマッピング名を指定 --> <property name="jndiName" value="ejb/TimeSender" /> <!-- 連携するEJBアプリケーションのパスを指定 --> <property name="businessInterface" value="com.example.ejb.TestEJBLocal" /> </bean> |
何度も呼び出され、かつ毎回同じ値を返すメソッドに対してCache機能を使用すると、アプリケーションの性能を向上させることができます。
例
アプリケーションのクラスに@Cacheableアノテーションを追記します。
@Cacheable(value = "Employees") |
bean定義ファイルに、<cache>タグとcacheManagerを定義します。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springbeans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <cache:annotation-driven /> … <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="default"/> <bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="Employees"/> </set> </property> </bean> </beans> |