Spring 5.3.16でXMLを使ってクラスのコンストラクタを実行する

環境
Spring 5.3.16
Eclipse 2019-12
JavaSE 1.8

1.pom.xmlの配置

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.arkgame.springconstuct</groupId>
<artifactId>springcontract</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.16</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.arkgame.springconstuct</groupId> <artifactId>springcontract</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.16</version> </dependency> </dependencies> </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.arkgame.springconstuct</groupId>
  <artifactId>springcontract</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  	<dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context</artifactId>
                  <version>5.3.16</version>
            </dependency>
      </dependencies>
</project>

2.applicationContext.xml設定ファイル
場所:src/main/java/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:cftuser -->
<bean id="cftuser" class="com.arkgame.study.User"></bean>
<!--UserServiceクラスのbean:userBean constructor-argタグを使用 -->
<bean id="userBean" class="com.arkgame.study.UserService">
<constructor-arg ref="cftuser"></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:cftuser --> <bean id="cftuser" class="com.arkgame.study.User"></bean> <!--UserServiceクラスのbean:userBean constructor-argタグを使用 --> <bean id="userBean" class="com.arkgame.study.UserService"> <constructor-arg ref="cftuser"></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:cftuser  -->
      <bean id="cftuser" class="com.arkgame.study.User"></bean>
      
      <!--UserServiceクラスのbean:userBean constructor-argタグを使用  -->
    <bean id="userBean" class="com.arkgame.study.UserService">
            <constructor-arg ref="cftuser"></constructor-arg>
    </bean>
</beans>

3.Userクラスの定義
User.java
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class User {
//名前
public String getUsername() {
String strName = "山田 一郎";
return strName;
}
//年齢
public int getAge() {
return 32;
}
}
package com.arkgame.study; public class User { //名前 public String getUsername() { String strName = "山田 一郎"; return strName; } //年齢 public int getAge() { return 32; } }
package com.arkgame.study;

public class User {

      //名前
      public String getUsername() {
            String strName = "山田 一郎";
            return strName;
      }
      //年齢
      public int getAge() {
            return 32;
      }
}

4.UserServiceクラスの定義
UserService.java
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class UserService {
// Userクラスのインスタンス変数の宣言
private final User user;
//UserServiceのコンストラクタ
public UserService(User user) {
this.user = user;
}
public String getUser() {
return user.getUsername();
}
public int getAge() {
return user.getAge();
}
}
package com.arkgame.study; public class UserService { // Userクラスのインスタンス変数の宣言 private final User user; //UserServiceのコンストラクタ public UserService(User user) { this.user = user; } public String getUser() { return user.getUsername(); } public int getAge() { return user.getAge(); } }
package com.arkgame.study;

public class UserService {
    // Userクラスのインスタンス変数の宣言
      private final User user;

      //UserServiceのコンストラクタ
      public UserService(User user) {
            this.user = user;
      }

      public String getUser() {

            return user.getUsername();
      }

      public int getAge() {

            return user.getAge();
      }
}

5.動作確認ファイル

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserApp {
// 配置ファイル名
private static final String FILENAME = "applicationContext.xml";
// Beanインスタンス名
private static final String BEANNAME = "userBean";
public static void main(String[] args) {
// applicationContext.xmlを読み込み
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(FILENAME);
// DIコンテナからbean(インスタンス)を取得
UserService userService = (UserService) context.getBean(BEANNAME);
System.out.println("名前:" + userService.getUser());
System.out.println("年齢:" + userService.getAge());
context.close();
}
}
package com.arkgame.study; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserApp { // 配置ファイル名 private static final String FILENAME = "applicationContext.xml"; // Beanインスタンス名 private static final String BEANNAME = "userBean"; public static void main(String[] args) { // applicationContext.xmlを読み込み ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(FILENAME); // DIコンテナからbean(インスタンス)を取得 UserService userService = (UserService) context.getBean(BEANNAME); System.out.println("名前:" + userService.getUser()); System.out.println("年齢:" + userService.getAge()); context.close(); } }
package com.arkgame.study;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserApp {

      // 配置ファイル名
      private static final String FILENAME = "applicationContext.xml";

      // Beanインスタンス名
      private static final String BEANNAME = "userBean";

      public static void main(String[] args) {

            // applicationContext.xmlを読み込み
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(FILENAME);

            // DIコンテナからbean(インスタンス)を取得
            UserService userService = (UserService) context.getBean(BEANNAME);
            System.out.println("名前:" + userService.getUser());
            System.out.println("年齢:" + userService.getAge());
            
            context.close();
      }

}

6.実行結果
名前:山田 一郎
年齢:32

SpringMVC

Posted by arkgame