忍者ブログ

Noracorn Try And Error Programサバサバと働くITエンジニア。忘れやすい頭からサラサラと流れ落ちる情報を、書き留めるブログです。

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

BeanFactory

Bean.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    <bean id="taro" class="data.Person">
        <property name="id">
            <value>1</value>
        </property>       
        <property name="name">
            <value>スプリング太郎</value>
        </property>       
        <property name="age">
            <value>27</value>
        </property>       
    </bean>
</beans>   

web.xmlに以下を追加
----------------
    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

Person.java
----------------
package data;

public class Person {
 private String id;
 private String name;
 private int age;
 
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}

Person.java
----------------
package main;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import data.Person;

public class Main {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Resource resource = new ClassPathResource("/WEB-INF/Bean.xml");
  BeanFactory beanFactory = new XmlBeanFactory(resource);
  Person taro = (Person)beanFactory.getBean("taro");
  
  System.out.println("id :" + taro.getId());
  System.out.println("name :" + taro.getName());
  System.out.println("age :" + taro.getAge());
 }

}
 

拍手[0回]

PR
Comment
name
title
color
mail
URL
comment
pass   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
コメントの修正にはpasswordが必要です。任意の英数字を入力して下さい。
この記事へのトラックバック
この記事にトラックバックする: