[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"global-header-tutorials-static":3,"article-constructor-confusion-in-spring":4,"initial-similar-fetch":24},[],{"alias":5,"title":6,"description":7,"content":8,"thumbnail":9,"keywords":10,"categories":19,"tags":22,"createdAt":23},"constructor-confusion-in-spring","Constructor Confusion in Spring Framework","Constructor Confusion and how to handle it in Spring Framework","{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Constructor confusion happens when the class or component has more than one constructor.  So, the Spring IoC container doesn't know which constructor is to be used for dependency injection.\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Please explore our other tutorials:\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"marks\":[{\"type\":\"link\",\"attrs\":{\"href\":\"https:\u002F\u002Fcsbyte.com\u002Fblog\u002Fspring-framework-project-for-gradle-with-intellij-idea\",\"target\":\"_blank\",\"rel\":\"noopener noreferrer\",\"class\":\"text-primary font-medium underline underline-offset-4 decoration-primary\u002F30 hover:decoration-primary transition-all\",\"title\":null}}],\"text\":\"How to create a clean Spring Framework project for Gradle with IntelliJ IDEA\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"marks\":[{\"type\":\"link\",\"attrs\":{\"href\":\"https:\u002F\u002Fcsbyte.com\u002Fblog\u002Fspring-dependency-injection-and-inversion-control\",\"target\":\"_blank\",\"rel\":\"noopener noreferrer\",\"class\":\"text-primary font-medium underline underline-offset-4 decoration-primary\u002F30 hover:decoration-primary transition-all\",\"title\":null}}],\"text\":\"Dependency Injection (DI) and Inversion of Control (IoC) in Spring\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"marks\":[{\"type\":\"link\",\"attrs\":{\"href\":\"https:\u002F\u002Fcsbyte.com\u002Fblog\u002Fdependency-pull-lookup-spring\",\"target\":\"_blank\",\"rel\":\"noopener noreferrer\",\"class\":\"text-primary font-medium underline underline-offset-4 decoration-primary\u002F30 hover:decoration-primary transition-all\",\"title\":null}}],\"text\":\"Dependency Pull and Contextualized Dependency Lookup in Spring Framework\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"marks\":[{\"type\":\"link\",\"attrs\":{\"href\":\"https:\u002F\u002Fcsbyte.com\u002Fblog\u002Ftypes-of-dependency-injection-spring\",\"target\":\"_blank\",\"rel\":\"noopener noreferrer nofollow\",\"class\":null,\"title\":null}}],\"text\":\"Types of Dependency Injection in Spring Framework\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Constructor Confusion Example\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Let's look at the sample example:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"java\"},\"content\":[{\"type\":\"text\",\"text\":\"package org.csbyte.constructorConfusion;\\n\\nimport org.springframework.context.ApplicationContext;\\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\\nimport org.springframework.context.annotation.ComponentScan;\\nimport org.springframework.context.annotation.Configuration;\\nimport org.springframework.stereotype.Component;\\nimport org.springframework.stereotype.Service;\\n\\npublic class ConstructorConfusion {\\n    public static void main(String[] args) {\\n        ApplicationContext container = new AnnotationConfigApplicationContext(NotificationAppConfig.class);\\n        NotificationService manager = container.getBean(NotificationService.class);\\n\\n        manager.sendAlert(\\\"+1-555-0199\\\", \\\"Your package has shipped!\\\");\\n    }\\n}\\n@Configuration\\n@ComponentScan\\nclass NotificationAppConfig {\\n}\\n\\n\\n@Component(\\\"smsService\\\")\\nclass SMSService  {\\n    public void sendNotification(String recipient, String message) {\\n        System.out.println(\\\"SMS Sent to \\\" + recipient + \\\": \\\" + message);\\n    }\\n}\\n\\n@Component(\\\"emailService\\\")\\nclass EmailService  {\\n    public void sendNotification(String recipient, String message) {\\n        System.out.println(\\\"Email Sent to \\\" + recipient + \\\": \\\" + message);\\n    }\\n}\\n\\n@Service\\nclass NotificationService {\\n\\n    private SMSService smsSender;\\n    private EmailService emailSender;\\n\\n    \u002F\u002F No-args constructor\\n    public NotificationService() {\\n        System.out.println(\\\"Executing No-Args Constructor\\\");\\n    }\\n\\n    \u002F\u002F Single dependency constructor\\n    public NotificationService(SMSService smsService) {\\n        System.out.println(\\\"Executing Single-Arg Constructor (SMS)\\\");\\n        this.smsSender = smsService;\\n    }\\n\\n    \u002F\u002F Multi-dependency constructor\\n    public NotificationService(SMSService smsService, EmailService emailService) {\\n        System.out.println(\\\"Executing Multi-Arg Constructor (SMS & Email)\\\");\\n        this.smsSender = smsService;\\n        this.emailSender = emailService;\\n    }\\n\\n    public void sendAlert(String phoneNumber, String message) {\\n        System.out.println(\\\"\\\\n--- Initiating Broadcast ---\\\");\\n        if (smsSender != null) smsSender.sendNotification(phoneNumber, message);\\n        if (emailSender != null) emailSender.sendNotification(phoneNumber, message);\\n    }\\n}\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Here, we do have three constructors: no-args, single-arg, and multiple-arg constructors. \"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Spring by default looks for a single primary constructor. If it finds multiple constructors, it tries to use based on the beans available in context, but it gets confused or becomes ambiguous which constructor needs to be used for multiple constructors.\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Basically, the constructor confusion arises\"}]},{\"type\":\"bulletList\",\"content\":[{\"type\":\"listItem\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Ambiguous data type: Multiple constructors accept different data types, e.g \"},{\"type\":\"text\",\"marks\":[{\"type\":\"code\"}],\"text\":\"String\"},{\"type\":\"text\",\"text\":\" vs \"},{\"type\":\"text\",\"marks\":[{\"type\":\"code\"}],\"text\":\"Int\"},{\"type\":\"text\",\"text\":\"  \"}]}]}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"java\"},\"content\":[{\"type\":\"text\",\"text\":\"    private String someValue;\\n\\n    public ConstructorConfusion(String someValue) {\\n        System.out.println(\\\"String called\\\");\\n        this.someValue = someValue;\\n    }\\n\\n    public ConstructorConfusion(@Value(\\\"90\\\") int someValue) {\\n        System.out.println(\\\"int called\\\");\\n        this.someValue = \\\"Number: \\\" + Integer.toString(someValue);\\n    }\"}]},{\"type\":\"bulletList\",\"content\":[{\"type\":\"listItem\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Multiple Bean Matches: If multiple constructors match beans.\"}]}]}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"In our above notification system code, Spring will fall back to the default no-arg constructor because any other constructors are not injected via the \"},{\"type\":\"text\",\"marks\":[{\"type\":\"code\"}],\"text\":\"@Autowire\"},{\"type\":\"text\",\"text\":\" annotation.\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"If you remove the no-args constructor and run the code, we will get the following error:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"Failed to instantiate [org.csbyte.constructorConfusion.NotificationService]: No default constructor found\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Adding @Autowired\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"What happens if we mark the multiple constructors as autowired? Spring will crash during context initialization.\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"java\"},\"content\":[{\"type\":\"text\",\"text\":\"    @Autowired\\n    public NotificationService(SMSService smsService) {\\n        System.out.println(\\\"Executing Single-Arg Constructor (SMS)\\\");\\n        this.smsSender = smsService;\\n    }\\n\\n    \u002F\u002F Multi-dependency constructor\\n    @Autowired\\n    public NotificationService(SMSService smsService, EmailService emailService) {\\n        System.out.println(\\\"Executing Multi-Arg Constructor (SMS & Email)\\\");\\n        this.smsSender = smsService;\\n        this.emailSender = emailService;\\n    }\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"If we are using IntelliJ IDEA, it shows the compile-time error \"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"Only one constructor can have @Autowire annotation \"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":3},\"content\":[{\"type\":\"text\",\"text\":\"Use a Single @Autowired Annotation\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"In order to resolve these issues, we need to create a single constructor that will be annotated by Autowired\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"java\"},\"content\":[{\"type\":\"text\",\"text\":\"    @Autowired\\n    public NotificationService(SMSService smsService, EmailService emailService) {\\n        System.out.println(\\\"Executing Multi-Arg Constructor (SMS & Email)\\\");\\n        this.smsSender = smsService;\\n        this.emailSender = emailService;\\n    }\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Best Practice\"}]},{\"type\":\"bulletList\",\"content\":[{\"type\":\"listItem\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Place a single \"},{\"type\":\"text\",\"marks\":[{\"type\":\"code\"}],\"text\":\"@Autowired\"},{\"type\":\"text\",\"text\":\" annotation for the desired constructor.\"}]}]},{\"type\":\"listItem\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Or create a single constructor for dependency injection; no \"},{\"type\":\"text\",\"marks\":[{\"type\":\"code\"}],\"text\":\"@Autowired\"},{\"type\":\"text\",\"text\":\" is needed.\"}]}]}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null}}]}","\u002Fuploads\u002Fthumbnails\u002Fd0f09fc0-e1f9-49d1-bf9a-7d1c41050196_di_confusio.jpeg",[11,12,13,14,15,16,17,18],"spring-framework-7","spring-boot-4","coding","programming","spring","ioc","spring-boot","di",[20,21],"Spring Framework","Spring Boot",[15,17],"2026-05-21T14:13:31.437Z",[25,32,38,44,50,56,62,68,74,79,85,91,97],{"alias":26,"title":27,"description":28,"thumbnail":29,"createdAt":30,"tutorialAlias":31,"lessonAlias":31},"springdoc-openapi-swagger-ui-in-spring","Implementing springdoc-openapi swagger ui in Spring Boot application","Adding springdoc-openapi swagger ui in a Spring Boot application","https:\u002F\u002Fapi.csbyte.com\u002Fuploads\u002Feditor\u002F3501ace5-c9b0-4819-8d35-d8714390c075_swagger_product_highlighted.png","2026-06-30T10:39:32.703Z",null,{"alias":33,"title":34,"description":35,"thumbnail":36,"createdAt":37,"tutorialAlias":31,"lessonAlias":31},"spring-boot-multi-stage-build-with-docker","Creating spring boot multi-stage build with docker","How to create a Spring Boot multi-stage build with Docker","\u002Fuploads\u002Fthumbnails\u002F8931e2f7-a669-4fbb-855b-5ebe396f462e_multi-stage-build-docke.jpeg","2026-06-20T06:34:41.356Z",{"alias":39,"title":40,"description":41,"thumbnail":42,"createdAt":43,"tutorialAlias":31,"lessonAlias":31},"configure-profile-in-spring-boot","Configure different spring profiles in spring boot application","Managing Spring Boot profiles for different environments along with Docker","\u002Fuploads\u002Fthumbnails\u002F59a52e59-4b56-469d-8902-41af5e0f13dd_Spring-profil.jpeg","2026-06-20T04:51:27.098Z",{"alias":45,"title":46,"description":47,"thumbnail":48,"createdAt":49,"tutorialAlias":31,"lessonAlias":31},"global-exception-handling-in-spring-boot","Implementing Robust Global Exception Handling in Spring Boot","How to Handle Errors in Spring Boot: An Architectural Guide","https:\u002F\u002Fapi.csbyte.com\u002Fuploads\u002Feditor\u002F554489e1-d142-4ea5-a393-994a13d7b113_exception-handling.png","2026-06-12T17:15:16.253Z",{"alias":51,"title":52,"description":53,"thumbnail":54,"createdAt":55,"tutorialAlias":31,"lessonAlias":31},"spring-boot-create-libraries-gradle","Building Reusable Spring Boot Libraries with Gradle","Architectural Guide: Building Reusable Spring Boot Libraries with Gradle","https:\u002F\u002Fapi.csbyte.com\u002Fuploads\u002Feditor\u002Ff4fb088d-1018-4d2b-ae6c-cdd5d0fd60ae_spring-Initializr.png","2026-06-12T15:55:20.994Z",{"alias":57,"title":58,"description":59,"thumbnail":60,"createdAt":61,"tutorialAlias":31,"lessonAlias":31},"multi-build-project-gradle-spring-boot","Setting up a multi-project build in Gradle for Spring Boot","How to set up a multi-project build in Gradle for Spring Boot","https:\u002F\u002Fapi.csbyte.com\u002Fuploads\u002Feditor\u002Fd43933e4-a7a3-404b-a300-8f02db98f549_multi-build-project.png","2026-06-12T04:15:23.045Z",{"alias":63,"title":64,"description":65,"thumbnail":66,"createdAt":67,"tutorialAlias":31,"lessonAlias":31},"java-version-mismatch-gradle","Java Version Mismatch for Gradle Build","How to Fix Gradle Error: Dependency requires at least JVM runtime version 17. This build uses a Java 8 JVM","\u002Fuploads\u002Fthumbnails\u002Fa4c6c910-5623-4db9-93f5-a515135adb99_gradle_version_mismatc.jpeg","2026-06-09T12:15:21.729Z",{"alias":69,"title":70,"description":71,"thumbnail":72,"createdAt":73,"tutorialAlias":31,"lessonAlias":31},"dependency-pull-lookup-spring","Dependency Pull and Contextualized Dependency Lookup in Spring Framework","Dependency Pull and Contextualized Dependency Lookup IoC Types in Spring Framework","\u002Fuploads\u002Fthumbnails\u002Fd1132443-313b-4dce-b713-55f356ef6051_depndency_pull.png","2026-05-20T15:26:06.494Z",{"alias":75,"title":76,"description":76,"thumbnail":77,"createdAt":78,"tutorialAlias":31,"lessonAlias":31},"types-of-dependency-injection-spring","Types of Dependency Injection in Spring Framework","\u002Fuploads\u002Fthumbnails\u002Fafb32399-6dda-41c4-b07c-7decb8257bbb_di_constructor_sette.jpeg","2026-05-19T17:01:51.972Z",{"alias":80,"title":81,"description":82,"thumbnail":83,"createdAt":84,"tutorialAlias":31,"lessonAlias":31},"spring-framework-project-for-gradle-with-intellij-idea","How to create a clean Spring Framework project for Gradle with IntelliJ IDEA","Create a clean Spring Framework project for Gradle with IntelliJ IDEA","https:\u002F\u002Fapi.csbyte.com\u002Fuploads\u002Feditor\u002F0e4abad5-17e2-4311-8cf0-7d8f67af975a_spring-framework.png","2026-05-19T10:28:20.866Z",{"alias":86,"title":87,"description":88,"thumbnail":89,"createdAt":90,"tutorialAlias":31,"lessonAlias":31},"spring-dependency-injection-and-inversion-control","Dependency Injection (DI) and Inversion of Control (IoC) in Spring","Mastering Dependency Injection (DI) and Inversion of Control (IoC) in Spring: A Practical Guide for Building a Notification System","\u002Fuploads\u002Fthumbnails\u002F53c64b94-f188-40b4-9ae9-ad97612d688b_spring_d.jpeg","2026-05-19T05:31:45.138Z",{"alias":92,"title":93,"description":94,"thumbnail":95,"createdAt":96,"tutorialAlias":31,"lessonAlias":31},"setting-nginx-ssl-for-spring-boot-application","Setup Nginx and SSL for Spring Boot Application","Setting Nginx as a reverse proxy in our Spring Boot application","\u002Fuploads\u002Fthumbnails\u002F5433c987-9e86-4d90-9cce-a831d1598ba4_spring-boot-nginx.png","2026-05-04T05:36:23.075Z",{"alias":98,"title":99,"description":100,"thumbnail":101,"createdAt":102,"tutorialAlias":31,"lessonAlias":31},"deploy-spring-boot-application-with-docker","Deploy Spring Boot Application with Docker on Ubuntu Server","The ultimate guide to deploying our Spring Boot application with Docker on an Ubuntu server","https:\u002F\u002Fapi.csbyte.com\u002Fuploads\u002Feditor\u002F55a11844-d1e9-4e83-80e5-c6bb6df7d58b_intellij-idea-build.png","2026-05-03T05:58:55.642Z"]