Spring Frameworkを使う利点のひとつに、依存性注入が簡単に使えることがあります。
Spring MVCコンテナに依存性を注入する一番簡単な方法は、@Autowiredというアノテーションを使うことです。
org.springframework.beans.factory.annotationというパッケージにあります。
クラスが依存性を見つけるためには、@Serviceというアノテーションが付けられていなければなりません。
Serviceというアノテーションが付けられたクラスは、「サービスである」という印になります。
org.springframework.stereotypeパッケージにあります。
また、設定ファイルには、component-scanエレメントを加える必要があります。
サービス(@Service)として宣言したクラスを、@Autowiredを使ってインジェクションしてみます。
在@Autowired声明的类里要写好Ssetter和Getter方法,要不会取到空值
全体のフォルダ構成は以下のとおりで、前に書いたSpringの記事のものとほとんど同じです。
このブログの「Spring」タグの記事を見てもらえればと思います。
まずはサービスから。
・FriendService
package service;
import domain.Friend;
public interface FriendService {
Friend add(Friend friend);
Friend get(long id);
}
上のインターフェースを実装したのが以下のクラスです。
FriendというJavaBeansのようなものを、IDと共にハッシュマップで持っています。
・FriendServiceImpl
package service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.stereotype.Service;
import domain.Friend;
@Service
public class FriendServiceImpl implements FriendService {
private Map<Long, Friend> friends = new HashMap<Long, Friend>();
private AtomicLong generator = new AtomicLong();
public FriendServiceImpl() {
Friend friend = new Friend();
friend.setName("urabe sho");
friend.setAge(28);
friend.setHeight(180.5F);
add(friend);
}
public Friend add(Friend friend) {
long newId = generator.incrementAndGet();
friend.setId(newId);
friends.put(newId,friend);
return friend;
}
public Friend get(long id) {
return friends.get(id);
}
}
で、上で作成したServiceクラスをインジェクションしてみます。
package controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import service.FriendService;
import domain.Friend;
import form.FriendForm;
@Controller
public class FriendController {
private static final Log logger =
LogFactory.getLog(FriendController.class);
@Autowired
private FriendService friendService;
@RequestMapping(value="/friend_input")
public String inputFriend() {
logger.info("inputFriend called");
return "FriendForm";
}
@RequestMapping(value="/friend_save")
public String saveFriend(FriendForm friendForm, RedirectAttributes redirectAttributes) {
logger.info("saveFriend called");
Friend friend = new Friend();
friend.setName(friendForm.getName());
friend.setAge(Integer.parseInt(friendForm.getAge()));
friend.setHeight(Float.parseFloat(friendForm.getHeight()));
//★このfriendServiceはnewで明示的にインスタンス化しなくても使える!!★
Friend savedFriend = friendService.add(friend);
redirectAttributes.addFlashAttribute("message","友人の登録に成功しました!");
return "redirect:/friend_view/" + savedFriend.getId();
}
@RequestMapping(value = "/friend_view/{id}")
public String viewFriend(@PathVariable Long id, Model model) {
Friend friend = friendService.get(id);
model.addAttribute("friend",friend);
return "FriendView";
}
}
@Autowiredをつけることで、インジェクションが可能となり、
Friend savedFriend = friendService.add(friend);
のように、friendServiceはnewで明示的にインスタンス化しなくても使えるようになります。
ただ、このサービスを使うためには、設定ファイルにサービスが存在するパッケージを記載しなければいけません。
context:component-scanの記載に注目です。
■springmvc-config.xml
以下のように、サービスが存在するパッケージをSpring MVCに知らせます。
そうすることで、サービスを依存性注入することができるようになります。
Spring MVCでPOSTした日本語が以下のように、文字化けする場合は、web.xmlに以下を追記してください。
フィルターをかませることによって、エンコードが統一されて、文字化けを解消できます・
http://blog.codebook-10000.com/entry/20140603/1401786427