注釈@Resource実現メカニズム
spring 3.Xは私達のためにコンポーネントの自動スキャンの機構を導入して、それはclassitoryパスの下で@Component、@Service、@Controller、@Repository注解の種類を探して、そしてこれらの種類をspring容器の中で管理します.
まず注解の意味を説明します.
@Serviceは、ビジネス層のコンポーネントを表示するために使用されます.
@Controllerは、strutsのactionなどの制御層コンポーネントを表示するために使用されます.
@Repositoryはデータアクセスコンポーネント、すなわちDAOコンポーネントを表示するために使用されます.
@Componentはコンポーネントを指していますが、規格に合わない場合は、この注釈を使って表示してもいいです.
Spring自動装備機構を使用してXMLファイルに以下の情報を設定します.
一、contextの名前空間を導入する
一、
まず注解の意味を説明します.
@Serviceは、ビジネス層のコンポーネントを表示するために使用されます.
@Controllerは、strutsのactionなどの制御層コンポーネントを表示するために使用されます.
@Repositoryはデータアクセスコンポーネント、すなわちDAOコンポーネントを表示するために使用されます.
@Componentはコンポーネントを指していますが、規格に合わない場合は、この注釈を使って表示してもいいです.
Spring自動装備機構を使用してXMLファイルに以下の情報を設定します.
一、contextの名前空間を導入する
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" default-lazy-init="true">
二、配置ファイルにcontextを追加する:component-scanラベル<context:component-scan base-package="com.***.controller" />
このようにしてファイルに注釈を使用することができます.@Resourceを例にとって、2つの実施形態があります. 一、
<pre name="code" class="java"> //
@Resource(name = "newsService")
private NewsService newsService;
二、前者はデフォルトでフィールドの名前を取ってbean名前として依存オブジェクトを探しています.後者はデフォルトで属性名を取ってbean名前として依存オブジェクトを探しています.// set @Resource(name="newsDao") public void setNews(NewsDao newsDao) { this.newsDao = newsDao; }