かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[Java]Spring Framework 2.5入門 「コンテナの作成」

前に2つほどSpring Framework2.5のエントリを書きました。
前回のエントリは、とりあえず動くものを!というスタンスで書いてあります。

  1. [Java][NetBeans]Spring Framework 2.5入門 その1
  2. [Java][NetBeans]Spring Framework 2.5入門 その2

今回からは、ちょっと立ち返って基本からおさえていこうと思います。

 

ということで、今回のネタはSpring Frameworkのコンテナの作り方です。
Hello world作った時にもClassPathXmlApplicationContextというクラスのインスタンスを作ってますが、要はこいつのことです。

コンテナのクラス

コンテナには、下に示す2つのインターフェースを実装したクラスをよく使います。

  1. org.springframework.beans.factory.BeanFactory
  2. org.springframework.context.ApplicationContext

BeanFactoryは、非常にシンプルで、基本的にコンテナ内にあるオブジェクトを取得するためのメソッドが中心に定義されています。
ApplicationContextは、BeanFactoryに比べるととても複雑です。Spring Frameworkが備える便利機能とかを使うためにはこちらを使うことになります。(実はApplicationContextはBeanFactoryを拡張したもの)ということで、ここではApplicationContextについて中心的に見ていきます。

さて、ApplicationContextが機能盛りだくさんと書きましたが、基本的にインターフェースなので実装が無いと話しになりません。ApplicationContextを実装したクラスとしてよく使うのが、Hello worldの時にも出てきたClassPathXmlApplicationContextになります。これは、クラスパス上の定義ファイルを読み込むコンテナです。実際問題クラスパス上のものを読むのがほとんどなので、これを使っていれば間違いありません。因みに、ファイルシステム上の構成ファイルを読み込むFileSystemXmlApplicationContextというクラスもあります。

ということで、コンテナのインスタンスを作成してみます。
クラスパス上の何処か(ここではルートに置きました)に、application.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
</beans>

恐らくこれが、最小のSpringの定義ファイルになります。

この定義ファイルを読み込んでClassPathXmlApplicationContextクラスのインスタンスを作ります。

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

ApplicationContext context = new ClassPathXmlApplicationContext(
        "application.xml");

コンストラクタの引数に、さっき作った定義ファイルへのパスを指定します。配列を指定することで、複数の定義ファイルを読み込むことも出来ます。

ApplicationContext context = new ClassPathXmlApplicationContext(
        new String[] {"application.xml", "other.xml"});

ということで、今回は、コンテナの作り方でした。

投稿日時 : 2009年2月12日 13:07

Feedback

# [Java]Spring Framework入門 「beanの登録」 2009/02/13 0:57 かずきのBlog

[Java]Spring Framework入門 「beanの登録」

#  NetBeans ??? Spring Framework ???????????? - Technology 2009/08/25 0:02 Pingback/TrackBack

NetBeans ??? Spring Framework ???????????? - Technology

# re: [Java]Spring Framework 2.5入門 「コンテナの作成」 2010/07/27 12:37 あああ

あああ

# cheap nike shoes 2012/12/07 22:31 http://superairmaxshoes.webs.com/

I regard something truly special in this web site.

# handbags burberry uk 2012/12/15 22:51 http://www.burberryuksale.co/2012-burberry-handbag

Ill be down again the track to look into other posts that.

# burberry outlet 2012/12/18 14:43 http://www.burberryuksale.co

I genuinely like em. It's unlike you wear these out walking around town persons.

# sacs Burberry 2012/12/19 13:25 http://sacburberrysoldesfr.webnode.fr/blog

Ill be back down the track to see other threads that.

# longchamp uk sale 2012/12/21 2:33 http://bagslongchamp.webeden.co.uk

we re-watched god, the father of the particular Rings trilogy, the Godfather trilogy, and approximately twenty various movies that many of us loved and hadn¡¯t watched within a while.

# longchamp pliage tote 2012/12/22 17:19 http://sacspliagelongchamps.webnode.fr

My business is impressed by the quality of information on this website. There are loads of good methods here.

# www.K77.fr 2013/03/05 0:33 http://www.k77.fr/

Well-being is really a perfume a person stream directly on other types without any obtaining a limited sheds directly on your own self. www.K77.fr http://www.k77.fr/

# K88.fr 2013/03/05 0:33 http://www.k88.fr/

Add‘w not waste matter your efforts even on a the world/sweetheart,which one isn‘w not ready to waste matter their valuable time period upon you. K88.fr http://www.k88.fr/

# Jordan Retro 4 2013/03/05 0:34 http://www.jordanretro4air.com/

Assume‘to spoil some time using a wow/female,who else isn‘to prepared to spoil his or her point in time with you. Jordan Retro 4 http://www.jordanretro4air.com/

# lunettes de vue ray ban 2013/03/05 0:34 http://www.g33.fr/

Get into‘metric ton waste materials your schedule with a mankind/female patient,what person isn‘metric ton happy to waste materials any energy done to you. lunettes de vue ray ban http://www.g33.fr/

# www.F77.fr 2013/03/05 0:38 http://www.f77.fr/

In order every management within your worthy of, matter your buddies. www.F77.fr http://www.f77.fr/

# casquette supreme 2013/03/15 4:56 http://www.b44.fr/

Around the globe may well body, yet somehow to just one consumer may well on earth. casquette supreme http://www.b44.fr/

# casquette superman 2013/03/17 9:59 http://www.b77.fr/

Adoration is definitely fallible during the arrival, about the germinates tougher as we age whether it is very well provided with. casquette superman http://www.b77.fr/

# destockchine 2013/03/24 0:30 http://c99.fr/

Could be The almighty wishes for united states of america based on one or two inappropriate everyday people previously assembly the right choice, making sure that weight training in conclusion fulfill the guy or girl, we're going to understand how to you should be pleased. destockchine http://c99.fr/

# destockchine 2013/03/24 0:31 http://d77.fr/

An absolute friend is a who also overlooks a person's backsliding and then can handle a person's positive results. destockchine http://d77.fr/

# usine23 2013/03/24 0:31 http://e55.fr/

On affluence much of our colleagues appreciate our company; inside hard knocks when they're older much of our colleagues. usine23 http://e55.fr/

# 3suisses 2013/04/07 0:57 http://ruenee.com/

Perhaps Oplagt prefers united states of america based on many incorrectly citizens in the past appointment the correct one, in order for when we finally ultimately match the someone, let's learn how to sometimes be gracious. 3suisses http://ruenee.com/

# bonprix 2013/04/08 7:14 http://ruemee.com/

When you wish an accounting system of one's deserving of, score neighbors. bonprix http://ruemee.com/

# Fantastic beat ! Ӏ ԝould liҝе to apprentice еven as you amend youг web site, how cаn i subscribe f᧐r a weblog website? The account helped me a acceptable deal. I haɗ been tiny bit familiar ߋf tһis youг broadcast offered brilliant transparent idea 2018/05/06 15:32 Fantastic beat ! Ι woulԁ like to apprentice even a

Fantastic beat ! I ?ould ?ike t? apprentice even as you amend yo?r web site,ho? can i subscribe
foг a weblog website? The account helped mе a acceptable deal.
? had been tiny b?t familiar of this your broadcast offered brilliant transparent idea

# I һave ƅeen browsing online gгeater than 3 hours ass ⲟf late, yet I by no means fоund any attention-grabbing artiicle ⅼike yοurs. It's beautiful value enough for me. In my opinion, іf alⅼ website owners ɑnd bloggers made just right c᧐ntent material aѕ 2018/05/17 8:36 І hɑve beеn bbrowsing onliine greater than 3 hߋurs

Ι haνe been browsing online geeater than 3 ?ouгs ?? of late,
yet ? byy no medans found anny attention-grabbing article lke ??urs.
It'? beautiful ?alue enough f?r me. In my opinion,
if all website owners аnd bliggers mаde just rig?t content material ?s y?u probably ?i?, the internet wwill probably be
a lot more useful tgan evеr bеfore.

# I am sure this piece off writing has touched all the internet users, its really really pleasant post on building up new weblog. 2018/07/04 9:13 I am sure this piece of wrriting has touched all

I aam sure this piehe of writing has touched all
the internet users, its really really pleasant
post on building up new weblog.

# PbsBgcUFcvrisKvz 2018/12/20 4:28 https://www.suba.me/

Sb4uYa views are fastidious in favor of new visitors.

# Hi there! Do you know if they make any plugins to assist with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains. If you know of any please share. Appreciate it! 2021/07/10 1:16 Hi there! Do you know if they make any plugins to

Hi there! Do you know if they make any plugins to assist with SEO?

I'm trying to get my blog to rank for some targeted keywords but I'm not
seeing very good gains. If you know of any please share.
Appreciate it!

# I like the efforts you have put in this, thanks for all the great posts. 2021/07/18 4:09 I like the efforts you have put in this, thanks fo

I like the efforts you have put in this, thanks for all
the great posts.

# Simply wanna remark that you have a very decent web site, I love the pattern it really stands out. 2022/03/22 20:40 Simply wanna remark that you have a very decent we

Simply wanna remark that you have a very decent
web site, I love the pattern it really stands out.

# I am regular reader, how are you everybody? This article posted at this website is truly good. 2024/02/15 8:49 I am regular reader, how are you everybody? This a

I am regular reader, how are you everybody? This article posted at this website is
truly good.

# I really appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You've made my day! Thanks again! 2024/03/18 11:44 I really appreciate this post. I have been looking

I really appreciate this post. I have been looking everywhere for this!
Thank goodness I found it on Bing. You've made my day! Thanks again!

タイトル  
名前  
Url
コメント