「SpringBoot」@Configurationでインスタンスを取得

書式
1.@Configurationの設定
@Configuration
public class クラス名
2.ApplicationContextで起動ファイルを設定
ApplicationContext 変数名 = new AnnotationConfigApplicationContext(クラス名.class);
使用例
1.アノテーション名@Configurationと@Beanを追加する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Test {
@Bean
public String getAddr() {
return "tokyo";
}
}
package com.arkgame.study; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Test { @Bean public String getAddr() { return "tokyo"; } }
package com.arkgame.study;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Test {
      @Bean
      public String getAddr() {
            return "tokyo";		
      }
}

2.起動ファイルを修正

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootApplication
public class Test1Application {
public static void main(String[] args) {
ApplicationContext cft = new AnnotationConfigApplicationContext(Test.class);
SpringApplication.run(Test1Application.class, args);
}
}
package com.arkgame.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @SpringBootApplication public class Test1Application { public static void main(String[] args) { ApplicationContext cft = new AnnotationConfigApplicationContext(Test.class); SpringApplication.run(Test1Application.class, args); } }
package com.arkgame.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@SpringBootApplication
public class Test1Application {

      public static void main(String[] args) {
            ApplicationContext cft = new AnnotationConfigApplicationContext(Test.class);
            SpringApplication.run(Test1Application.class, args);
      }
}

3.pom.xmlの設定

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

Spring Boot

Posted by arkgame