Uploaded by abdenaji chiba

Java IOC - Doc

advertisement
JAVA IOC
Certainly! Here's a more detailed example that combines various ways to use and configure IoC using Spring:
1. XML Configuration:
```xml
<!-- beans.xml -->
<beans>
<bean id="messageService" class="com.example.MessageService"/>
<bean id="notificationService" class="com.example.NotificationService">
<property name="messageService" ref="messageService"/>
</bean>
</beans>
```
In this example, we define two beans, `messageService` and `notificationService`, in an XML configuration file. The
`messageService` bean of class `com.example.MessageService` is created and injected into the `notificationService`
bean using the `ref` attribute.
2. Annotation-based Configuration:
```java
@Component
public class MessageService {
public void sendMessage(String message) {
System.out.println("Sending message: " + message);
}
}
```
```java
@Component
public class NotificationService {
private final MessageService messageService;
@Autowired
public NotificationService(MessageService messageService) {
this.messageService = messageService;
}
public void sendNotification(String message) {
messageService.sendMessage(message);
}
}
```
In this example, the `@Component` annotation is used to mark the `MessageService` and `NotificationService`
classes as Spring-managed beans. The `NotificationService` class has a dependency on the `MessageService`
interface, which is automatically injected using the `@Autowired` annotation.
3. Java-based Configuration:
```java
@Configuration
public class AppConfig {
@Bean
public MessageService messageService() {
return new MessageService();
}
@Bean
public NotificationService notificationService() {
return new NotificationService(messageService());
}
}
```
In this example, we define bean definitions using Java-based configuration. The `messageService` bean is created
and injected into the `notificationService` bean using the `messageService()` method.
4. Component Scanning:
```java
@Component
public class MessageService {
public void sendMessage(String message) {
System.out.println("Sending message: " + message);
}
}
```
```java
@Component
public class NotificationService {
private final MessageService messageService;
@Autowired
public NotificationService(MessageService messageService) {
this.messageService = messageService;
}
public void sendNotification(String message) {
messageService.sendMessage(message);
}
}
```
In this example, the `@Component` annotation is used to mark the `MessageService` and `NotificationService`
classes as Spring-managed beans. During component scanning, Spring will automatically detect and create instances
of these classes.
5. XML and Annotation Combination:
```xml
<!-- beans.xml -->
<beans>
<context:component-scan base-package="com.example"/>
</beans>
```
In this example, the XML configuration file specifies component scanning using the `context:component-scan`
element. It tells Spring to scan the `com.example` package for annotated beans.
6. Java-based and Annotation Combination:
```java
@Configuration
@ComponentScan("com.example")
public class AppConfig {
// Bean definitions and other configurations
}
```
In this example, the `@Configuration` annotation is used to mark the class as a configuration class. The
`@ComponentScan` annotation is used to specify the package to scan for annotated beans.
To run the application, you can create
a main class that loads the Spring application context:
```java
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);
NotificationService notificationService =
context.getBean(NotificationService.class);
notificationService.sendNotification("Hello, Spring!");
}
}
```
In this example, we load the Spring application context using the `AnnotationConfigApplicationContext` class and
pass the `AppConfig` class as an argument. We then retrieve the `NotificationService` bean from the context and call
the `sendNotification()` method.
When you run the application, it will print "Sending message: Hello, Spring!" as the output, demonstrating the
successful wiring and invocation of the beans.
Please note that for the examples to work, you'll need to have the necessary dependencies in your Maven project,
such as `spring-context`, `spring-beans`, and `spring-core`.
Download