VHow does dependency injection work in Spring?
Pytanie rekruteraSpring
Rekruterzy na stanowiska ze Springiem chcą usłyszeć, że rozumiesz, co dzieje się za adnotacjami. Stwierdzenie, że @Autowired „po prostu działa”, nie wystarczy. Liczy się wyjaśnienie, jak kontener tworzy i łączy beany.
Dobra struktura odpowiedzi na głos to: jaki problem rozwiązuje dana funkcja, jak działa w środku frameworka i jedna pułapka z prawdziwego projektu. Pułapki sprawiają, że odpowiedź brzmi jak doświadczenie, a nie jak wyuczony tekst.
- ✓Słowa → zwroty → zdania → mówienie
- ✓Sens odpowiedzi
- ✓Język
Częste pytania i przykładowe odpowiedzi
How does dependency injection work in Spring?
Jak działa wstrzykiwanie zależności (dependency injection) w Springu?
Co chce usłyszeć rekruter. Rola kontenera, wstrzykiwanie przez konstruktor i dlaczego pomaga w testowaniu.
Przykładowa odpowiedź po angielsku do ćwiczenia na głosIn 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?
Czym są zakresy (scope) beanów w Springu?
Co chce usłyszeć rekruter. Singleton jako wartość domyślna i dlaczego stan w singletonach jest niebezpieczny.
Przykładowa odpowiedź po angielsku do ćwiczenia na głosThe 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?
Jak działa @Transactional i jakie ma pułapki?
Co chce usłyszeć rekruter. Mechanizm proxy, problem wywołania wewnątrz tej samej klasy (self-invocation) i reguły wycofywania transakcji (rollback).
Przykładowa odpowiedź po angielsku do ćwiczenia na głosSpring 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?
Co robi autokonfiguracja w Spring Boot?
Co chce usłyszeć rekruter. Konfiguracja warunkowa i sposób jej nadpisania.
Przykładowa odpowiedź po angielsku do ćwiczenia na głosSpring 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?
Jak obsługujesz wyjątki w REST API opartym na Springu?
Co chce usłyszeć rekruter. Centralna obsługa i spójny format błędów.
Przykładowa odpowiedź po angielsku do ćwiczenia na głosI 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.
Jak wymawiać kluczowe terminy
- bean
- bin
- annotation
- an-o-TEJ-szn
- dependency
- di-PEN-den-si
- injection
- in-DŻEK-szn
- proxy
- PROK-si
- singleton
- SIN-gl-ton
Przydatne zwroty do odpowiedzi
- Under the hood, Spring creates a proxy that…Pod maską Spring tworzy proxy, które…
- The default behaviour is…, but I can change it by…Domyślne zachowanie to…, ale mogę je zmienić, używając…
- One pitfall I ran into was…Jedna z pułapek, które mnie spotkały, to…
- In a unit test, I would replace this with a mock.W teście jednostkowym zastąpię to mockiem.
- That depends on whether…To zależy od tego, czy…
- I would check the documentation for the exact rule, but the idea is…Dokładną regułę trzeba sprawdzić w dokumentacji, ale idea jest taka:…
Typowe błędy
- Samo wymienianie adnotacji. Wyjaśnij, co każda z nich każe robić frameworkowi.
- Nazywanie Springa „magią”. Sprawia to wrażenie, że go nie rozumiesz. Opisując, co się dzieje, używaj słów takich jak „kontener” i „proxy”.
- Niewspominanie o testowaniu. Rekruterzy lubią słyszeć, jak decyzja projektowa wpływa na testy jednostkowe i integracyjne.
- Mylenie Springa ze Spring Boot. Spring to framework, a Spring Boot dodaje autokonfigurację, startery i wbudowany serwer.
Najczęstsze pytania
Czego się uczyć: Springa czy Spring Boot?
Obu, w tej kolejności: najpierw podstawowych idei Springa (kontener, beany, proxy), potem tego, co dodaje Spring Boot. Rekruterzy często pytają o podstawowe idee na przykładach z Boota.
Czy trzeba znać Spring Security?
Na stanowiskach backendowych w większości firm tak, na poziomie podstawowym: uwierzytelnianie a autoryzacja, łańcuch filtrów i sposób zabezpieczenia endpointu. Powiedz szczerze, jakie masz doświadczenie.
Jak opisać swoje doświadczenie ze Springiem?
Podaj wersję, użyte moduły (Web, Data, Security) i jeden rozwiązany problem. Konkretny problem przekonuje bardziej niż lista adnotacji.
Przeczytać odpowiedź to nie to samo, co ją wypowiedzieć. Przećwicz każde pytanie na głos, a potem porównaj sens i język własnej odpowiedzi.
Wypróbuj SpeakRole ↗Inne tematy w tym obszarze