Quick Contact

    Hibernate

    Spring – Java-based Container Configuration

    The central artifacts in Spring’s new Java-configuration support are @Configuration-annotated classes and @Bean-annotated methods.

    The @Bean annotation is used to indicate that a method instantiates, configures, and initializes a new object to be managed by the Spring IoC container. For those familiar with Spring’s XML configuration, the @Bean annotation plays the same role as the element. You can use @Bean-annotated methods with any Spring @Component. However, they are most often used with @Configuration beans.

    Annotating a class with @Configuration indicates that its primary purpose is as a source of bean definitions. Furthermore, @Configuration classes let inter-bean dependencies be defined by calling other @Bean methods in the same class. The simplest possible @Configuration class reads as follows:

    The below simple example show usage of @Bean and @Configuration annotations.

    	importorg.springframework.context.annotation.Bean;
    	importorg.springframework.context.annotation.Configuration;
    
    	importcom.companyname.projectname.customer.CustomerService;
    	importcom.companyname.projectname.order.OrderService;
    
    	@Configuration
    	public class Application {
    
    	    @Bean
    	publicCustomerServicecustomerService() {
    	return new CustomerService();
    	    }
    
    	    @Bean
    	publicOrderServiceorderService() {
    	return new OrderService();
    	    }
    	}
    

    The preceding configuration is exactly equivalent to the following Spring XML:

    	< beans>
    	< bean id="customerService" class="com.companyname.projectname.CustomerService"/>
    	< bean id="orderService" class="com.companyname.projectname.OrderService"/>
    	< /beans>
    

    Note that the method name and bean name in XML are exactly the same.

    Instantiating the Spring Container by Using AnnotationConfigApplicationContext

    Spring 3.0 introduced AnnotationConfigApplicationContext class which is implementation ApplicationContext interface. It is capable of accepting not only @Configuration classes as input but also plain @Component classes and classes annotated with JSR-330 metadata.

    When @Configuration classes are provided as input, the @Configuration class itself is registered as a bean definition and all declared @Bean methods within the class are also registered as bean definitions.

    When @Component and JSR-330 classes are provided, they are registered as bean definitions, and it is assumed that DI metadata such as @Autowired or @Inject is used within those classes where necessary.

    Create Spring IOC Container

    We can use @Configuration classes as input when instantiating an AnnotationConfigApplicationContext. This allows for completely XML-free usage of the Spring container, as the following example shows:

    importorg.springframework.context.annotation.AnnotationConfigApplicationContext;

    	publicclassApplication {
    	publicstaticvoidmain(String[] args) {
    	AnnotationConfigApplicationContext context =newAnnotationConfigApplicationContext(AppConfig.class);
    	MessageServicemessageService=context.getBean(MessageService.class);
    	messageService.setMessage("TwitterMessageService Implementation");
    	System.out.println(messageService.getMessage());
    
    	MessageService messageService1 =context.getBean(MessageService.class);
    	System.out.println(messageService1.getMessage());
    	context.close();
    	    }
    	}
    

    AnnotationConfigApplicationContext is not limited to working only with @Configuration classes. Any @Component or JSR-330 annotated class may be supplied as input to the constructor, as the following example shows:

    	publicstaticvoid main(String[] args) {
    	ApplicationContextctx=newAnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);
    	MyServicemyService=ctx.getBean(MyService.class);
    	myService.doStuff();
    	}
    

    Building the Container Programmatically by Using register(Class<?>… )

    You can instantiate an AnnotationConfigApplicationContext by using a no-arg constructor and then configure it by using the register() method. This approach is particularly useful when programmatically building an AnnotationConfigApplicationContext. The following example shows how to do so:

    	publicstaticvoid main(String[] args) {
    	AnnotationConfigApplicationContextctx=newAnnotationConfigApplicationContext();
    	ctx.register(AppConfig.class, OtherConfig.class);
    	ctx.register(AdditionalConfig.class);
    	ctx.refresh();
    	MyServicemyService=ctx.getBean(MyService.class);
    	myService.doStuff();
    	}
    

    Enabling Component Scanning with scan(String… )

    To enable component scanning, you can annotate your @Configuration class as follows:

    	packagenet.ducatindia.spring.scope;
    
    	importorg.springframework.context.annotation.ComponentScan;
    	importorg.springframework.context.annotation.Configuration;
    
    	@Configuration
    	@ComponentScan(basePackages="net.ducatindia.spring")
    	publicclassAppConfig {
    
    	 }
    

    Use @ComponentScan annotation to enable auto component scanning in java based configuration, annotate your @Configuration class as shown above code sample.

     

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.