- オブジェクトの生成
- Dependency Injection
- Aspect Oriented Programming
の3つの機能を3回のpostに分けて書きます。まずは、オブジェクトの生成から書きます。
XML設定ファイルを利用したオブジェクトの生成
- // src/cc/co/goodpreparations/Main.java
- package cc.co.goodpreparations;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
- Person person = (Person)context.getBean("person");
- person.saySomething();
- }
- }
- // src/cc/co/goodpreparations/Person.java
- package cc.co.goodpreparations;
- public class Person {
- private String message;
- public void setMessage(String message){
- this.message = message;
- }
- public void saySomething(){
- System.out.println(message);
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- src/Beans.xml -->
- <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-3.0.xsd">
- <bean id="person" class="cc.co.goodpreparations.Person">
- <property name="message" value="Hello!"/>
- </bean>
- </beans>
フィールド値の設定
オブジェクトを生成するときにフィールドの初期値を設定出来ます。同一のクラスから生成されているがフィールド初期値が異なるインスタンスは、異なるBean idをつけることで区別できます。XMLのproperty要素内でフィールドの設定をしていますが、ここで設定するフィールドには必ずsetter(この例の場合はPerson#setMessage)を定義しなければいけません。(JavaBeansなので当然ですが。)
- // src/cc/co/goodpreparations/Main.java
- package cc.co.goodpreparations;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
- Person taro = (Person)context.getBean("taro");
- taro.saySomething();
- Person hanako = (Person)context.getBean("hanako");
- hanako.saySomething();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- src/Beans.xml -->
- <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-3.0.xsd">
- <bean id="taro" class="cc.co.goodpreparations.Person">
- <property name="message" value="ぼく太郎!"/>
- </bean>
- <bean id="hanako" class="cc.co.goodpreparations.Person">
- <property name="message" value="わたし花子!"/>
- </bean>
- </beans>
Beanのスコープ
Beanを生成するときに、そのスコープを設定することができます。- singleton
- prototype
- request
- session
- global-session
3.-5.はWebアプリケーションに特化したものなので、ここでは1.と2.のみを説明します。まずはsingleton
- // src/cc/co/goodpreparations/Main.java
- package cc.co.goodpreparations;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
- Person person = (Person)context.getBean("person");
- person.setMessage("Goodbye!");
- person.saySomething();
- Person person2 = (Person)context.getBean("person");
- person2.saySomething();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- src/Beans.xml -->
- <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-3.0.xsd">
- <bean id="person" class="cc.co.goodpreparations.Person" scope="singleton">
- <property name="message" value="Hello!"/>
- </bean>
- </beans>
>Goodbye! >Goodbye!となります。ちなみにデフォルトのスコープはsingletonです。scope属性を明記しなかった場合、そのBeanのスコープはsingletonになります。
次に、prototypeを試してみます。先ほどと同様の例でscope属性のみをprototypeに変更すると、実行結果は
>Goodbye! >Hello!になります。スコープをprototypeにすると、毎回異なるインスタンスが生成されます。
コールバック関数の指定
Beanが生成されたとき/破棄されるときに、何か処理をしたい場合はコールバック関数を指定することができます。
- // src/cc/co/goodpreparations/Main.java
- package cc.co.goodpreparations;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
- Person person = (Person)context.getBean("person");
- person.saySomething();
- person = null;
- ((AbstractApplicationContext) context).registerShutdownHook();
- }
- }
- // src/cc/co/goodpreparations/Person.java
- package cc.co.goodpreparations;
- public class Person {
- private String message;
- public void setMessage(String message){
- this.message = message;
- }
- public void saySomething(){
- System.out.println(message);
- }
- public void initFunc() {
- System.out.println("A Person Bean is created!");
- }
- public void destroyFunc() {
- System.out.println("A Person Bean is destroyed!");
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- src/Beans.xml -->
- <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-3.0.xsd">
- <bean id="person" class="cc.co.goodpreparations.Person" scope="singleton"
- init-method="initFunc" destroy-method="destroyFunc">
- <property name="message" value="Hello!"/>
- </bean>
- </beans>
0 件のコメント:
コメントを投稿