Weather Forecast


Java Spring Components Language features Public New

Hard 

5min


Consider the following two classes:

@Service
public class WeatherForecastService {

    private Thermometer thermometer;

    @Autowired
    public WeatherForecastService(Thermometer thermometer) {
        this.thermometer = thermometer;
    }

    @Scheduled(fixedDelay = 10000)
    public void takeTemperatureMeasurement() {
        thermometer.measure();
    }   
}

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class FakeThermometer implements Thermometer {

    @Override
    public int measure() { return 10; };
}

Supposing Spring uses component scanning to discover beans, select all of the statements that are correct.

Instantiation of WeatherForecastService will fail because Thermometer is an interface and not a class.
Instantiation of WeatherForecastService will fail, because only @Repository beans may be injected into @Service beans.
Automatic execution of takeTemperatureMeasurement() every 10 seconds can be enabled by annotating the Spring configuration class with @EnableScheduling.
Automatic execution of takeTemperatureMeasurement() every 10 seconds can be enabled by using <task:annotation-driven/> in XML configuration.
A new instance of the FakeThermometer class will be created each time the takeTemperatureMeasurement() method is executed.