「Java入門」SpringのHttpInvoverを勉強するメモ

Javaコード:

1.Serializableインターフェイスを実現するためにエンティティクラスを作成する

package entity;
import java.io.Serializable;
public class Fruit implements Serializable {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}

2.インターフェイスを作成
package service;
import java.util.List;
import entity.Fruit;
public interface FruitService {
List<Fruit> getFruitList();
}
3.2のインターフェスを実現するクラスを作成
package service.impl;
import java.util.ArrayList;
import java.util.List;
import service.FruitService;
import entity.Fruit;
public class FruitServiceImpl implements FruitService {
public List<Fruit> getFruitList() {
List<Fruit> list = new ArrayList<Fruit>();
Fruit f1 = new Fruit();
f1.setName(“紫");
f1.setColor(“黄色");
Fruit f2 = new Fruit();
f2.setName(“林檎");
f2.setColor(“赤");
list.add(f1);
list.add(f2);
return list;
}
}
4.WEB-INFのweb.xmlのSpringMVCの情報を設定
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5.applicationContext.xmlに必要なbean情報をエクスポート
<bean id="furitService" class="service.impl.FruitServiceImpl"></bean>
<bean id="FuritService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
p:serviceInterface="service.FruitService" p:service-ref="furitService" />
6.WEB=INF直下にspringMvc-servlet.xmlを作成、urlMapping を配置
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/fruitService">FuritService</prop>
</props>
</property>
</bean>
</beans>
7.applicationContext.xmlにクライアントが必要なbean情報を取得
<bean id="getFruitService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
p:serviceInterface="service.FruitService"
p:serviceUrl="http://localhost:8080/SpringHttpInvoker/fruitService" />
8.検証ソースコード:
package startnews24;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.Fruit;
import service.FruitService;
public class Startnews24 {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
“applicationContext.xml");
FruitService fruitService = (FruitService) ctx
.getBean(“getFruitService");
List<Fruit> fruitList = fruitService.getFruitList();
for (Fruit fruit : fruitList) {
System.out.println(fruit.getColor() + “の" + fruit.getName());
}
}
}

Java

Posted by arkgame