Category: Spring

  • Understanding the @Scope Annotation in Spring: How Bean Scopes Work

    What is @Scope This is a class-level annotation that allows the developer to define the scope of a Spring bean. By default, all Spring beans are singletons, but by using this annotation, you can modify that to the following scopes: Why is it important: How to use @Scope In every bean that you have, you…

  • How to Use @PropertySource in Your Spring Application

    What is the @PropertySource annotation? The @PropertySource annotation is used to tell Spring to load a specific properties file from a location provided in the annotation. It is used in conjunction with @Configuration at the class level. It allows you, as a developer, to externalize the configuration of your application. Why use @PropertySource? How to…

  • Spring Essentials: How to Use the @Bean Annotation for Custom Services

    In our application, we have a lot of objects. These objects are components of our application. When we want Spring to create, manage, and inject these objects, they are considered Spring beans. The @Bean annotation is a method-level annotation that we can use to tell Spring that we want it to manage the instance for…

  • Spring Framework Essentials: Understanding @ComponentScan

    @ComponentScan is an annotation that tells Spring where to find classes annotated as Spring beans. Using this annotation, we can instruct Spring to create beans only for the specified location. Spring will use the package provided in the annotation to look for beans to create. If no package is defined in the annotation, Spring will…

  • How to Use Spring’s @Profile Annotation for Flexible Configurations

    The profile annotation in spring allows you to segregate configurations that are available only in specific environments. If you mark a @Component (and its specializations) or any @Configuration or @Bean with the @Profile annotation, they will be available only if the profile specified is active. You can set the profile using application.properties by using the…

  • How to Use the @Import Annotation in Spring Framework

    The @Import annotation allows you to specify which configurations your Spring application should load. You can think of this annotation as your Java class’s import statements. In this case, it’s going to import only classes with the @Configuration annotation. It’s not the exact concept, but you get the idea. By using the @Import, you explicitly…

  • Method security with @Secured Annotation in Spring

    This annotation provides a way to add security configuration to business methods. It will use roles to check if a user has permission to call this method. The annotation is part of spring security. So to enable its usage you need the spring security dependency. Example Scenario You have an application that has a product…

  • Using the @Lookup Annotation in Spring

    The @Lookup annotation is an injection (like @Inject, @Resource, @Autowired) annotation used at the method level. This annotation tells Spring to overwrite the method, redirecting to the bean factory to return a bean matching the return type of the method. This can be useful for some bean scopes, such as the prototype scope, which will…

  • @PreDestroy and @PostConstruct in Spring: Managing Bean Lifecycle

    These annotations are called at specific moments in the bean lifecycle. They allow you to define methods executed after a bean is created and before the beans are destroyed. Both annotations come from Jakarta EE and can be used in Spring projects. They are useful for managing resources that are only used in the bean’s…

  • @Resource: The Versatile Bean Injection Annotation for Jakarta EE and Spring

    This annotation does bean injection, like the @Autowired and @Inject annotations. This annotation is packaged with Jakarta EE and will work on your Spring projects. You can use this annotation almost in the same way you use the other annotations used to inject dependencies. Using in-field injection and set methods is possible, but constructors are…