0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

メモ

Posted at
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MessageService {

    @Value("${greeting.morning}")
    private String morningMessage;

    @Value("${greeting.afternoon}")
    private String afternoonMessage;

    @Value("${greeting.evening}")
    private String eveningMessage;

    @Value("${error.E101}")
    private String errorE101Message;

    @Value("${error.E102}")
    private String errorE102Message;

    public String getMorningMessage() {
        return morningMessage;
    }

    public String getAfternoonMessage() {
        return afternoonMessage;
    }

    public String getEveningMessage() {
        return eveningMessage;
    }

    public String getErrorE101Message() {
        return errorE101Message;
    }

    public String getErrorE102Message() {
        return errorE102Message;
    }
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageLogger {

    private static final Logger logger = LoggerFactory.getLogger(MessageLogger.class);

    @Autowired
    private MessageService messageService;

    public void logMessages() {
        logger.info("Morning Message: {}", messageService.getMorningMessage());
        logger.info("Afternoon Message: {}", messageService.getAfternoonMessage());
        logger.info("Evening Message: {}", messageService.getEveningMessage());
        logger.error("Error E101 Message: {}", messageService.getErrorE101Message());
        logger.error("Error E102 Message: {}", messageService.getErrorE102Message());
    }
}
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner run(MessageLogger messageLogger) {
        return args -> {
            messageLogger.logMessages();
        };
    }
}

~~~Java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MessageService {

    @Value("${greeting.morning}")
    private String morningMessage;

    public String getMorningMessage(String name) {
        return String.format(morningMessage, name);
    }
}
@Component
public class MessageLogger {

    private static final Logger logger = LoggerFactory.getLogger(MessageLogger.class);

    @Autowired
    private MessageService messageService;

    public void logMessages() {
        String name = "John"; // 例えば、名前をJohnとしますが、実際の名前をここに指定します
        String morningMessage = messageService.getMorningMessage(name);
        logger.info("Morning Message: {}", morningMessage);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?