Spring Dependency Injection With Examples

This post shows what is dependency injection in Spring framework along with examples of injecting dependencies using XML configuration as well as using annotations. Table of contents What is dependency injection Dependency injection in Spring Spring dependency injection example with XML configuration Spring dependency injection example with annotations What is…

Continue reading

Fix LazyInitializationException: could not initialize proxy Error

While working with Hibernate you might have encountered the following error- org.hibernate.LazyInitializationException could not initialize proxy – no Session In this tutorial we’ll see why this error comes up and what is the best way to fix it. Why LazyInitializationException : could not initialize proxy You may encounter this error…

Continue reading

Spring @ComponentScan Annotation

Spring framework can automatically detect the classes annotated with @Component and other stereotype annotations like @Repository, @Service, @Controller. You need some way to tell Spring where to look for classes annotated with one of these annotations. For that you can use Spring @ComponentScan annotation, you can also specify the base…

Continue reading

Spring @Autowired Annotation

Spring @Autowired annotation is used to automatically inject dependencies. Where to apply @Autowired annotation You can apply the @Autowired annotation to setter methods. See example. You can apply the @Autowired annotation to constructors. See example. You can apply @Autowired to fields. See example. You can also apply the annotation to…

Continue reading