VHow does dependency injection work in Spring?
Senso della risposta✓
Lingua2
Domanda dell’intervistatoreSpring
Chi intervista per i ruoli Spring vuole sentire che capisci che cosa succede dietro le annotazioni. Dire che @Autowired «funziona e basta» non è sufficiente. Spiegare come il container crea e collega i bean, invece, sì.
Una buona struttura per le risposte orali è: quale problema risolve la funzionalità, come funziona dentro il framework e un'insidia incontrata in un progetto reale. Le insidie fanno sembrare la risposta frutto di esperienza e non di memoria.
- ✓Parole → espressioni → frasi → parlato
- ✓Senso della risposta
- ✓Lingua
Domande comuni e risposte di esempio
How does dependency injection work in Spring?
Come funziona la dependency injection in Spring?
Che cosa vuole sentire l'intervistatore. Il ruolo del container, l'iniezione tramite costruttore e perché aiuta i test.
Risposta di esempio in inglese da esercitare ad alta voceIn Spring, I do not create my dependencies with new. I declare what a class needs, usually in the constructor, and the container creates the objects, which we call beans, and passes them in. At startup Spring scans the classes, builds a graph of beans and connects them. I prefer constructor injection, because dependencies are explicit and can be final, and in a unit test I can pass a mock directly without starting Spring. Field injection hides dependencies and makes classes harder to test.
What are bean scopes in Spring?
Che cosa sono gli scope dei bean in Spring?
Che cosa vuole sentire l'intervistatore. Singleton come valore predefinito e perché lo stato dentro i singleton è pericoloso.
Risposta di esempio in inglese da esercitare ad alta voceThe default scope is singleton: Spring creates one instance per application context and shares it. Prototype creates a new instance every time the bean is requested. In web applications there are also request and session scopes. The most important thing to remember is that singleton beans are shared between threads, so they must not keep mutable request-specific state in fields. If I need per-request data, I pass it as method arguments or use a request-scoped bean.
How does @Transactional work, and what are its pitfalls?
Come funziona @Transactional e quali sono le sue insidie?
Che cosa vuole sentire l'intervistatore. Il meccanismo del proxy, il problema dell'auto-invocazione e le regole di rollback.
Risposta di esempio in inglese da esercitare ad alta voceSpring wraps the bean in a proxy. When a method with @Transactional is called from outside, the proxy opens a transaction, calls the real method, and commits or rolls back. One pitfall is self-invocation: if a method calls another transactional method in the same class, the call does not go through the proxy, so the annotation is ignored. Another is that by default Spring rolls back only on unchecked exceptions, not on checked ones, unless I set rollbackFor. I also keep transactions short and avoid remote calls inside them.
What does Spring Boot auto-configuration do?
Che cosa fa l'autoconfigurazione di Spring Boot?
Che cosa vuole sentire l'intervistatore. La configurazione condizionale e come sovrascriverla.
Risposta di esempio in inglese da esercitare ad alta voceSpring Boot looks at the libraries on the classpath and the properties I set, and configures sensible defaults for me. For example, if it sees a database driver and a connection URL, it creates a DataSource. Each auto-configuration class is guarded by conditions, like "only if this class is present" or "only if I did not define my own bean". That last condition is important, because it means I can always override the default by defining my own bean. When something behaves strangely, I enable the conditions report to see what was applied and why.
How do you handle exceptions in a Spring REST API?
Come gestisci le eccezioni in una REST API con Spring?
Che cosa vuole sentire l'intervistatore. Una gestione centralizzata e un formato di errore coerente.
Risposta di esempio in inglese da esercitare ad alta voceI use a class with @RestControllerAdvice and methods annotated with @ExceptionHandler. This way controllers stay clean, and every error is converted to the same JSON format: a status code, a short error code and a message that is safe to show. I map business exceptions to the right HTTP status, for example 404 for a missing resource and 409 for a conflict. I never return stack traces to clients. I log the details with a correlation ID, so support can find the full error in the logs.
Come pronunciare i termini chiave
- bean
- biin
- annotation
- an-na-tèi-shon
- dependency
- di-pèn-den-si
- injection
- in-giéc-shon
- proxy
- pròc-si
- singleton
- sìn-ghel-ton
Frasi utili per rispondere
- Under the hood, Spring creates a proxy that…Sotto il cofano, Spring crea un proxy che…
- The default behaviour is…, but I can change it by…Il comportamento predefinito è…, ma posso cambiarlo con…
- One pitfall I ran into was…Un'insidia che ho incontrato è stata…
- In a unit test, I would replace this with a mock.In un test unitario, lo sostituirei con un mock.
- That depends on whether…Dipende dal fatto che…
- I would check the documentation for the exact rule, but the idea is…Controllerei la documentazione per la regola esatta, ma l'idea è…
Errori comuni da evitare
- Limitarsi a elencare le annotazioni. Spiega che cosa fa fare al framework ciascuna di esse.
- Chiamare Spring «magia». Dà l'impressione che tu non lo capisca. Usa parole come "container" e "proxy" per descrivere che cosa succede.
- Non parlare dei test. Agli intervistatori piace sentire come una scelta di progettazione influisce sui test unitari e sui test di integrazione.
- Confondere Spring e Spring Boot. Spring è il framework; Spring Boot aggiunge l'autoconfigurazione, gli starter e un server incorporato.
Domande frequenti
Devo studiare Spring o Spring Boot?
Entrambi, in quest'ordine: prima le idee di base di Spring (container, bean, proxy), poi quello che Spring Boot aggiunge. Gli intervistatori chiedono spesso le idee di base usando esempi tratti da Boot.
Devo conoscere Spring Security?
Per i ruoli backend nella maggior parte delle aziende sì, a livello base: autenticazione contro autorizzazione, la catena di filtri e come proteggi un endpoint. Di' con onestà che cosa hai usato.
Come descrivo la mia esperienza con Spring?
Indica la versione, i moduli che hai usato (Web, Data, Security) e un problema che hai risolto. Un problema concreto convince più di un elenco di annotazioni.
Leggere una risposta non è la stessa cosa che dirla. Esercita ogni domanda ad alta voce, poi confronta il significato e la lingua della tua risposta.
Prova SpeakRole ↗Altri argomenti in quest’area