Spring 5.3.23 applicationContext.xmlでコンストラクタを行うサンプル

環境
Spring 5.3.23
JDK8
Eclipse 4.14.0

操作方法
1.実行ファイル(ArkgameApp.java)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ArkgameApp {
public static void main(String[] args) {
//applicationContext.xmlを読み込み
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
//DIコンテナからbeanを取得
UserService cft= (UserService)context.getBean("testUserService");
System.out.println("情報: "+cft.getUser());
context.close();
}
}
package com.arkgame.test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ArkgameApp { public static void main(String[] args) { //applicationContext.xmlを読み込み ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //DIコンテナからbeanを取得 UserService cft= (UserService)context.getBean("testUserService"); System.out.println("情報: "+cft.getUser()); context.close(); } }
package com.arkgame.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ArkgameApp {

      public static void main(String[] args) {
            
            //applicationContext.xmlを読み込み
            ClassPathXmlApplicationContext context =
                        new ClassPathXmlApplicationContext("applicationContext.xml");
            
            //DIコンテナからbeanを取得
            UserService cft= (UserService)context.getBean("testUserService");

            System.out.println("情報: "+cft.getUser());
            context.close();

      }

}

説明
new ClassPathXmlApplicationContext(“applicationContext.xml");
applicationContext.xmlを読み込みます。
context.getBean(bean名)

2.UserServiceクラスの定義(UserService.java)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.test;
public class UserService {
//クラスUserのインスタンス
User user;
/**クラスのコンストラクタ */
public UserService(User user) {
this.user = user;
}
public String getUser() {
//インスタンスuserの関数を呼び出す
return user.geUserName();
}
}
package com.arkgame.test; public class UserService { //クラスUserのインスタンス User user; /**クラスのコンストラクタ */ public UserService(User user) { this.user = user; } public String getUser() { //インスタンスuserの関数を呼び出す return user.geUserName(); } }
package com.arkgame.test;

public class UserService {
      
      //クラスUserのインスタンス
      User user;
      
      /**クラスのコンストラクタ */
      public UserService(User user) {
            this.user = user;
      }
      public String getUser() {
            //インスタンスuserの関数を呼び出す
            return user.geUserName();
      }
}

3.Userクラスの定義(User.java)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.test;
public class User {
//文字列を返す
public String geUserName() {
return "品川 太郎";
}
}
package com.arkgame.test; public class User { //文字列を返す public String geUserName() { return "品川 太郎"; } }
package com.arkgame.test;

public class User {

      //文字列を返す
      public String geUserName() {
            return "品川 太郎";
      }
}

4.設定ファイル(applicationContext.xml)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?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.xsd">
<!-- Userクラスのbean(インスタンス)testUser -->
<bean id="testUser" class="com.arkgame.test.User"></bean>
<!-- UserServiceクラスのbean(インスタンス) testUserService -->
<bean id="testUserService" class="com.arkgame.test.UserService">
<!-- constructor-argタグを使用して引数Userクラスを指す -->
<constructor-arg ref="testUser"></constructor-arg>
</bean>
</beans>
<?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.xsd"> <!-- Userクラスのbean(インスタンス)testUser --> <bean id="testUser" class="com.arkgame.test.User"></bean> <!-- UserServiceクラスのbean(インスタンス) testUserService --> <bean id="testUserService" class="com.arkgame.test.UserService"> <!-- constructor-argタグを使用して引数Userクラスを指す --> <constructor-arg ref="testUser"></constructor-arg> </bean> </beans>
<?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.xsd">

    <!--  Userクラスのbean(インスタンス)testUser -->
    <bean id="testUser" class="com.arkgame.test.User"></bean>
    
    <!-- UserServiceクラスのbean(インスタンス) testUserService  -->
    <bean id="testUserService" class="com.arkgame.test.UserService">
        <!-- constructor-argタグを使用して引数Userクラスを指す -->
        <constructor-arg ref="testUser"></constructor-arg>
    </bean>
</beans>

説明
<bean id="bean名" class="パッケージ名">
<constructor-arg ref="コンストラクタの引数"></constructor-arg>
</bean>

5.実行確認
「ArkgameApp.java」を右クリックして「実行」->「Javaアプリケーション」をクリックするとコンソールに「情報: 品川 太郎」と表示されます。

SpringMVC

Posted by arkgame