Spring Frameworkの初期設定に関して
Spring Frameworkの初め方に関して、再度、まとめたいと思います。
Spring Frameworkですが、Javaのフレームワークで、昔は、DIコンテナの部分だけ使うことが多かった気がします。
今は、Spring MVCとか、Spring Batchとか、使いやすくなったのと、
アノテーションで、設定ファイルとか、あんまり書かなくよくなったとか、
いろいろ活用すると開発がかなり楽かなぁと思います。
それで、今回は、Spring 4.x系とMaven3の環境です。
まずは。Spring Frameworkのmavenの設定ですが、
Springは、3.x系から種類により分かれています。
基本的な設定は、pom.xmlに下記の設定を定義すれば大丈夫だと思います。
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.2.RELEASE</version> </dependency>
Java自体が多分、そうゆう思想なんだと思いますが、
Springも基本的には、インターフェイスと実装を分離して考えてます。
なので、インターフェイスを多用する考えで、使って行きます。
まずは、インターフェイスを作ります。
package com.chronos.logic; public interface Logic { String getMessage(); }
次にそのインターフェイスの実装クラスを作ります。
実装クラスは、まぁ〜普通のクラスとあまり変わりません。
ただし、そのクラスをDIコンテナの管理対象にする必要があります。
DIコンテナは、そのクラスが管理対象かどうかを設定ファイルか、アノテーションで、判断しています。
例えば、ロジッククラスの場合、
下の様に@Componentを使います。
package com.chronos.logic; @Component("logic") public class LogicImpl implements Logic { }
これで、DIコンテナ管理対象時には、logicと言う名前で、インスタンスが管理されます。
実際は、何種類かアノテーションがあるのですが、
例えば、サービスクラスとかだと、下のアノテーションを使ったり、
@Service("service")
@Repository("repository")
になると思います。
それで、このままだと、DIができないので、
少し、設定ファイルを記述する必要があります。
Springの設定ファイルを作成します。
Springは、applicationContext.xmlに設定を記述して行きます。
applicationContext.xmlは、下記の様なもので、beansの中に定義を書いて行きます。
<?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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> </beans>
@Componentや@Serviceとかのアノテーションを記述した場合、
下の定義を追加する必要があります。
<context:component-scan base-package="com.chronos.logic" scoped-proxy="targetClass"/>
これは、com.chronos.logicパッケージ配下を全てスキャンしてくれます。
そこで、@Componentや@Serviceとかのアノテーションが記載されたクラスを見つけると
DIコンテナに登録して、あとは、自由に使えるようになります。
WEBアプリケーションで使う場合は、web.xmlに定義する必要があります。
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring/applicationContext.xml</param-value> </context-param>
あとは、必要なクラスにDIインジェクションするように記述するだけです。
DIインジェクションする場合もアノテーションで記述します。
記述方法は、下記のような記述方法になります。
@Service("message") public MessageImpl implements Message { @Autowired @Qualifier("logic") protected Logic logic; }
バッチ処理の場合は、自分で、applicationContext.xmlを読む必要があります。
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml"); Message message = (Message)context.getBean("message");