### Desktop Component: Singleton Computer Implementation Source: https://context7.com/congdeng/springdemo/llms.txt A Spring-managed singleton bean that implements the Computer interface, representing desktop functionality. It includes a constructor to show instantiation and a compile method. Usage example demonstrates retrieving and using the bean from the Spring context. ```java package org.example; import org.springframework.stereotype.Component; @Component public class Desktop implements Computer { public Desktop() { System.out.println("Desktop object created"); } @Override public void compile() { System.out.println("Compiling in Desktop"); } } // Usage example - retrieving from Spring context ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Desktop desktop = context.getBean(Desktop.class); desktop.compile(); // Output: "Desktop object created" // "Compiling in Desktop" ``` -------------------------------- ### Laptop Component: Prototype-Scoped Primary Computer Source: https://context7.com/congdeng/springdemo/llms.txt A prototype-scoped Spring bean marked as the primary implementation of the Computer interface. It demonstrates how Spring handles different bean scopes and how the @Primary annotation selects this bean when multiple implementations exist. Usage example highlights distinct instances for prototype beans. ```java package org.example; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("laptop!") @Primary @Scope("prototype") public class Laptop implements Computer { public Laptop() { System.out.println("Laptop object created"); } @Override public void compile() { System.out.println("Compiling in Laptop"); } } // Usage example - prototype scope creates new instances ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Computer laptop1 = context.getBean("laptop!", Computer.class); Computer laptop2 = context.getBean("laptop!", Computer.class); // Output: "Laptop object created" // "Laptop object created" // laptop1 and laptop2 are different instances due to prototype scope ``` -------------------------------- ### Spring Main Application Entry Point Source: https://context7.com/congdeng/springdemo/llms.txt Launches a Spring application using annotation-based configuration. It initializes the AnnotationConfigApplicationContext, retrieves a bean, and invokes its methods. This serves as a typical application bootstrap process. ```java package org.example; import org.example.config.AppConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main(String[] args) { // Initialize Spring container with annotation-based config AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); // Retrieve bean from container Alien alien = context.getBean(Alien.class); // Use the bean System.out.println(alien.getAge()); alien.coding(); } } // Expected console output: // Alien object created // Laptop object created // 21 // coding // Compiling in Laptop ``` -------------------------------- ### Project Build and Run Commands (Bash) Source: https://context7.com/congdeng/springdemo/llms.txt Provides bash commands to clean and package the Maven project, and then run the compiled application. It also includes the expected output from executing the application. ```bash # Build the project mvn clean package # Run the application java -cp target/SpringDemo-1.0.jar org.example.App # Expected output: # Alien object created # Laptop object created # 21 # coding # Compiling in Laptop ``` -------------------------------- ### Loading Spring XML Configuration in Java Source: https://context7.com/congdeng/springdemo/llms.txt Demonstrates how to load an XML-based Spring configuration file within a Java application. It uses ClassPathXmlApplicationContext to initialize the Spring container and retrieve beans defined in the XML. ```java // Usage example - loading XML configuration import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Alien alien = context.getBean(Alien.class); alien.coding(); // Output: "Alien object created" // "Desktop object created" // "coding" // "Compiling in Desktop" ``` -------------------------------- ### Java Spring @Configuration with Component Scanning Source: https://context7.com/congdeng/springdemo/llms.txt Configures a Spring application context using @Configuration and @ComponentScan annotations. It enables automatic discovery of beans annotated with @Component. This approach simplifies configuration by reducing boilerplate code. ```java package org.example.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("org.example") public class AppConfig { // Component scanning automatically discovers @Component annotated classes } // Usage example - initializing Spring context with annotation config import org.springframework.context.annotation.AnnotationConfigApplicationContext; AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Alien alien = context.getBean(Alien.class); alien.coding(); context.close(); ``` -------------------------------- ### Maven Project Configuration (XML) Source: https://context7.com/congdeng/springdemo/llms.txt Defines project metadata and dependencies for a Maven project, including Spring context and JUnit. This configuration is essential for building and managing the project's libraries. ```xml 4.0.0 org.example SpringDemo 1.0 jar org.springframework spring-context 6.1.11 junit junit 3.8.1 test ``` -------------------------------- ### Alien Component: Setter Injection and Value Injection Source: https://context7.com/congdeng/springdemo/llms.txt A primary business component demonstrating Spring's setter-based dependency injection for the Computer interface and value injection for the age property. The @Autowired annotation on the setter method injects the Computer dependency, and @Value injects the integer value. ```java package org.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Alien { @Value("21") private int age; private Computer com; public Alien() { System.out.println("Alien object created"); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Computer getCom() { return com; } @Autowired public void setCom(Computer com) { this.com = com; } void coding() { System.out.println("coding"); com.compile(); } } // Usage example - dependency injection in action ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Alien alien = context.getBean(Alien.class); System.out.println(alien.getAge()); // Output: 21 alien.coding(); // Output: "coding" // "Compiling in Laptop" (because Laptop is marked @Primary) ``` -------------------------------- ### Computer Interface Definition Source: https://context7.com/congdeng/springdemo/llms.txt Defines the core contract for computer implementations. This interface is used to establish a common type for different computer components, allowing for polymorphic behavior within the Spring application. ```java package org.example; public interface Computer { void compile(); } // Usage example - implementing the interface @Component public class Desktop implements Computer { @Override public void compile() { System.out.println("Compiling in Desktop"); } } ``` -------------------------------- ### Spring XML-Based Bean Configuration Source: https://context7.com/congdeng/springdemo/llms.txt Configures Spring beans using an XML file instead of Java annotations. This approach is common in older Spring applications or when externalizing configuration. It defines beans, their properties, and dependencies. ```xml ``` -------------------------------- ### Spring @Qualifier for Dependency Injection Source: https://context7.com/congdeng/springdemo/llms.txt Illustrates using @Qualifier annotation to explicitly select a specific bean when multiple beans of the same type are available. This resolves ambiguity in dependency injection, ensuring the correct implementation is injected. ```java package org.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Alien { private Computer com; @Autowired @Qualifier("laptop!") // Explicitly select laptop bean public void setCom(Computer com) { this.com = com; } void coding() { com.compile(); } } // Usage example - overriding @Primary with @Qualifier ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Alien alien = context.getBean(Alien.class); alien.coding(); // Output: "Compiling in Laptop" (explicitly injected via @Qualifier) ``` -------------------------------- ### Java Spring @Bean Method Configuration Source: https://context7.com/congdeng/springdemo/llms.txt Defines beans manually using @Bean methods within a @Configuration class. This allows for fine-grained control over bean creation and dependency injection. It also demonstrates using @Scope to define bean lifecycle. ```java package org.example.config; import org.springframework.context.annotation.*; @Configuration @ComponentScan("org.example") public class AppConfig { @Bean() public Alien alien(Computer com) { Alien alien = new Alien(); alien.setCom(com); return alien; } @Bean(name = {"com1", "otherName"}) @Scope("prototype") public Desktop desktop() { return new Desktop(); } @Bean() @Primary public Laptop laptop() { return new Laptop(); } } // Usage example - retrieving beans with custom names ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Desktop desktop = (Desktop) context.getBean("com1"); desktop.compile(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.