DEV Community

Mac
Mac

Posted on

Spring Boot : @Profile

// MessageReader.java
public interface MessageReader {
    String read();
}

// FileMessageReader.java
@Profile("default")
@Service
public class FileMessageReader implements MessageReader {

    @Override
    public String read() {
        return "Read from file";
    }
}

// DbMessageReader .java
@Profile("prod")
@Service
public class DbMessageReader implements MessageReader {

    @Override
    public String read() {
        return "Read from DB";
    }
}

// App.java
@Autowired
private MessageReader messageReader;

If profile prod is active, DbMessageReader should be injected into MessageReader.

Top comments (0)