VHow does dependency injection work in Spring?
Mülakat sorusuSpring
Spring pozisyonlarında mülakatçılar, anotasyonların arkasında neler olduğunu anladığınızı duymak ister. @Autowired'ın “sadece çalıştığını” söylemek yetmez. Container'ın bean'leri nasıl oluşturup birbirine bağladığını söylemek yeter.
Sözlü cevaplar için iyi bir yapı şudur: özelliğin hangi sorunu çözdüğü, framework'ün içinde nasıl çalıştığı ve gerçek bir projede karşılaştığınız bir tuzak. Tuzaklar, cevabın ezber gibi değil deneyim gibi duyulmasını sağlar.
- ✓Kelimeler → ifadeler → cümleler → konuşma
- ✓Yanıtın anlamı
- ✓Dil
Sık sorulan mülakat soruları ve örnek cevaplar
How does dependency injection work in Spring?
Spring'de dependency injection nasıl çalışır?
Mülakatçı ne duymak ister. Container'ın rolü, constructor injection ve bunun neden test etmeyi kolaylaştırdığı.
Yüksek sesle çalışmak için İngilizce örnek cevapIn 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?
Spring'de bean scope'ları nelerdir?
Mülakatçı ne duymak ister. Varsayılan olarak singleton ve singleton'ların içinde durum (state) tutmanın neden tehlikeli olduğu.
Yüksek sesle çalışmak için İngilizce örnek cevapThe 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?
@Transactional nasıl çalışır ve tuzakları nelerdir?
Mülakatçı ne duymak ister. Proxy mekanizması, aynı sınıf içinden çağrı (self-invocation) sorunu ve rollback kuralları.
Yüksek sesle çalışmak için İngilizce örnek cevapSpring 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?
Spring Boot auto-configuration ne yapar?
Mülakatçı ne duymak ister. Koşullu yapılandırma ve bunun nasıl geçersiz kılınacağı.
Yüksek sesle çalışmak için İngilizce örnek cevapSpring 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?
Spring tabanlı bir REST API'de exception'ları nasıl yönetirsiniz?
Mülakatçı ne duymak ister. Merkezi yönetim ve tutarlı bir hata formatı.
Yüksek sesle çalışmak için İngilizce örnek cevapI 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.
Anahtar terimler nasıl telaffuz edilir
- bean
- biin
- annotation
- e-nı-TEY-şın
- dependency
- di-PEN-dın-si
- injection
- in-CEK-şın
- proxy
- PROK-si
- singleton
- SİNG-gıl-tın
Cevap vermek için kullanışlı kalıplar
- Under the hood, Spring creates a proxy that…Arka planda Spring, şunu yapan bir proxy oluşturur:…
- The default behaviour is…, but I can change it by…Varsayılan davranış…, ama bunu … ile değiştirebilirim.
- One pitfall I ran into was…Karşılaştığım tuzaklardan biri şuydu:…
- In a unit test, I would replace this with a mock.Birim testte bunu bir mock ile değiştirirdim.
- That depends on whether…Bu, … olup olmadığına bağlı.
- I would check the documentation for the exact rule, but the idea is…Kesin kuralı dokümantasyondan kontrol ederdim, ama fikir şu:…
Sık yapılan hatalar
- Yalnızca anotasyonları sıralamak. Her birinin framework'e ne yaptırdığını açıklayın.
- Spring'e “sihir” demek. Bu, onu anlamadığınız izlenimini verir. Olup biteni anlatmak için “container” ve “proxy” gibi sözcükler kullanın.
- Testten söz etmemek. Mülakatçılar, bir tasarım kararının birim ve entegrasyon testlerini nasıl etkilediğini duymayı sever.
- Spring ile Spring Boot'u karıştırmak. Spring framework'ün kendisidir; Spring Boot ise auto-configuration, starter'lar ve gömülü sunucu ekler.
Sıkça sorulan sorular
Spring mi, Spring Boot mu çalışmalıyım?
İkisini de, şu sırayla: önce Spring'in temel fikirleri (container, bean'ler, proxy'ler), sonra Spring Boot'un bunların üzerine eklediği şeyler. Mülakatçılar temel fikirleri çoğu zaman Boot örnekleriyle sorar.
Spring Security bilmem gerekir mi?
Çoğu şirketteki backend pozisyonları için temel düzeyde evet: kimlik doğrulama (authentication) ile yetkilendirme (authorisation) farkı, filter chain ve bir endpoint'i nasıl koruduğunuz. Neleri kullandığınızı dürüstçe söyleyin.
Spring deneyimimi nasıl anlatmalıyım?
Sürümü, kullandığınız modülleri (Web, Data, Security) ve çözdüğünüz bir sorunu söyleyin. Somut bir sorun, anotasyon listesinden daha ikna edicidir.
Bir cevabı okumak ile onu söylemek aynı şey değildir. Her soruyu yüksek sesle çalışın, ardından kendi cevabınızın anlamını ve dilini karşılaştırın.
SpeakRole’i dene ↗Bu alandaki diğer konular