View RSS Feed

Bean

Spring beans

  1. Auto-Wiring in Spring Beans

    by , 07-07-2012 at 07:24 PM
    The relationship can be auto wired by the Spring container between collaborating beans. It will reuce the overhead of writing large XML configuration for big Spring based applications.

    For dependency injection, following autowiring modes are used by the Spring container.

    • no: This mode is the default mode and it means that no autowiring and explicit bean referencing should be used for wiring. For this wiring, you do not need to use anything special.
    • byName: ...
    Tags: auto-wiring Add / Edit Tags
    Categories
    Bean
  2. Bean Definition Inheritance in Spring

    by , 07-07-2012 at 07:20 PM
    Following information is contained by a bean definition:

    • Configuration information
    • Constructor arguments information
    • Container specific information like method initialization
    • Property values information
    • Names of static factory methods

    Configuration data is inherited from the parent definition by the child bean definition. Some values are added or overridden by the child definition. There are no direct concerns between the java class ...
    Categories
    Bean
  3. Bean Post Processors in Spring

    by , 07-07-2012 at 07:19 PM
    The callback methods are defined by the Bean Post Processor interface and it can be implemented to provide your instatiation logic and dependency resolution logic. When the instantiating, initializating and configuring of a bean is finished by the Spring container, some custom logic implementation can be done by you by integrating Bean Post Processor implementations.

    More than one Bean Post Processor interfaces are allowed to use and configure and also it is possible to control the ...
    Categories
    Bean
  4. Spring Bean Life Cycle

    by , 07-07-2012 at 07:18 PM
    It is easy to understand the Spring Bean Life Cycle. It is required to perform some initialization on the instantiation of a bean so that it can enter into a usable state. When a bean is not required, some cleanup process is required to remove the bean from the container.

    There is a list of activities which are performed behind the scenes during the life of bean instantiation and destruction. Here we will discuss only two important methods of bean life cycle callback. These methods ...
    Categories
    Bean
  5. Spring Bean Scopes

    by , 07-07-2012 at 06:44 PM
    You can easily declare the bean scope for the bean when you define a bean in Spring. Whenever a new bean instanace is required by you, bean scope attribute is declared as prototype to force the Spring to produce the bean instance. Also whenever you are looking for the same bean instance from the Spring, the bean scope instance is declared as singleton.
    The following five scopes are supported by the Spring framework. In case of a web aware ApplicationContext, only three scopes are available. ...
    Categories
    Bean
  6. Spring Bean Definition

    by , 07-07-2012 at 06:43 PM
    The beans are the object that forms the backbone of any application and the Spring IoC container manges these beans. The Spring IoC container is responsible for instantiating, assembling and managing of a bean object. The configuration metadata is used to create these beans which are sent to container. For example, they are sent in the form of XML configurations.

    The configuration metadata information is contained by the bean definition which is required to know the following by the ...
    Categories
    Bean
  7. Introducing Behaviors to Your Spring Beans

    by , 11-27-2011 at 10:55 PM
    In Spring AOP, you can use what is called an introduction (a special type of advice) that enables your objects to implement an interface dynamically, by providing the implementation class for that interface. This behavior can be multiplied to give the impression of multiple inheritance to your beans. To define an introduction, use the @DeclareParents annotation, which gives matching types a new parent.

    Java Code:
    package com.acme.springexamples.derivautos;
    import org.aspectj.lang.annotation.Aspect;
    ...
    Categories
    Bean , Spring 3
  8. How to Automatically Discover Spring Beans on the Classpath

    by , 11-27-2011 at 10:38 PM
    Since it’s appearance in Java 5, annotations has helped to simplify the configuration of application through component scanning. This removes the need for XML bean tags in many cases. Classes annotated as component stereotypes including the following: @Service, @Controller, @Configuration, @Component, @Repository as well as the JSR-330 annotations, such as @Named are included in the auto-scan default filter. Once auto-detected, these components become Spring beans and are automatically placed ...
  9. Wiring Bean Post Processor in Spring

    by , 11-16-2011 at 05:32 PM
    In the last post, we create a class AnsiConvertor that implements the BeanPostProcessor interface. Now if we want to use this class in our application is running within a bean factory, we need to programmatically register the BeanPostProcessor using the factory’s addBeanPostProcessor() method:

    Java Code:
    BeanPostProcessor convertAnsi = new ConvertAnsi(); 
    factory.addBeanPostProcessor(convertAnsi);
    Or if you decide to use an application context, you’ll need to register the ...
    Categories
    Bean , Configuration , XML
  10. How to Write a BeanPostProcessing in Spring

    by , 11-16-2011 at 05:28 PM
    In the previous tip we talked about special beans in Spring. One of the Spring’s special beans that I had mentioned was using the BeanPostProcessor interface in order to cut into the bean’s life cycle and review or alter its configuration. In this tip, I will show you how to wire one. As mentioned in the previous tip I mentioned that the BeanPostProcessor provides two methods to alter the bean after it has been created and wired:

    Java Code:
    Object postProcessBeforeInitialization(Object bean,
    ...
  11. Spring's Special Beans

    by , 11-16-2011 at 05:25 PM
    The majority of beans in Spring are configured by the Spring container and made available for use in the application. Apart from these standard beans, Spring also has groups of beans that each serve a special purpose. The way in which Spring identifies these beans is because these beans have implemented certain interfaces that Spring considers special. The use of these interfaces allows these beans to do the following:
    • Post processing bean configuration in order to be involved in the bean
    ...
    Categories
    Bean , Configuration , XML
  12. How to Autowire a Spring Bean

    by , 11-16-2011 at 05:23 PM
    In previous tips, we have shown how to wire bean’s properties using the <property> element. There is an alternative approach that can be used. It’s called autowiring. In this approach, Spring will wire your beans automatically if you set the autowire property on each <bean> on each of the beans that you want to autowire. There are four types of autowiring that Spring uses:

    • byName—Spring will attempt to find a bean in the container whose name (or ID) is the same as the
    ...
  13. Wiring Sets, Maps and Properties in Spring

    by , 11-16-2011 at 05:19 PM
    Similarly to lists, there are times when you will need to map to other types of collections. In this tip, I will outline what you need to do to wire sets, maps and properties. The key to remember is that you want to wire your bean to the same type that is being used in the class definition. So if your bean has a java.util.Set property to guarantee uniqueness in the collection, you want to use the same definition in the xml definition.

    So for example, if you want to use a java.util.Set, ...
  14. Wiring Lists and Arrays in Spring Beans

    by , 11-16-2011 at 05:17 PM
    Often you will have situations where you will need to wire properties that are lists or arrays. If you are using Hibernate jointly with Spring, you will need to se this in order to provide the list of Hibernate mapping files. In order to do this, you will use the <list> element to wire the property in the wiring XML file for either an array property or a java.util.List property.

    For example, when you want to map Hibernate mapping files, you will wire a LocalSessionFactoryBean ...
    Categories
    Bean , Configuration
  15. Spring Inner Beans

    by , 11-16-2011 at 05:15 PM
    Rather than configure beans separately and allow for Spring’s DI container to manage the injection of these beans into other beans, another means of wiring bean references is to embed a <bean> element directly in the <property> element. For example, the carDao property of the carService bean can be wired as follows:

    Java Code:
    <bean id="carService" class="com.acme.springexample.service.CarServiceImpl">
    <property name="carDao">
    ...

    Updated 11-30-2011 at 12:51 PM by Spring Framework

    Categories
    Dependency Injection , Bean
  16. Understanding the Difference Between Singleton vs. Prototyping of Spring Beans

    by , 11-16-2011 at 05:13 PM
    When you configure a new Spring beanBy default, all Spring beans are singletons. When the container dispenses a bean, it will always give the exact same instance of the bean. But sometimes you want the application context to return a unique instance for each time you request a specific bean. In this case, you would want to do is to define a prototype bean. When you define a prototype means that you define a blueprint of the bean. Every subsequent bean for this requested bean will be based on this ...
  17. The Lifecycle of a Spring Bean

    by , 11-16-2011 at 05:11 PM
    In this tip, we will look at the lifecycle of a bean. The bean lifecycle for a Spring bean is somewhat complex. It is important to understand the life cycle of a Spring bean. Otherwise it will be difficult for you as a programmer to use Spring to its full abilities especially because you may want to take advantage of some of the opportunities that Spring offers to customize how a bean is created. The startup life cycle of a typical bean as it is loaded into a BeanFactory container is shown: ...
    Categories
    Bean
  18. Working with Spring's Bean Factories

    by , 11-16-2011 at 01:18 PM
    BeanFactory serves as the core to Spring's Dependency Injection implementation. A BeanFactory is responsible for managing components and their dependencies. Bean is the term used in Spring to refer to any component managed by the container. Typically all Spring beans adhere, at some level, to the JavaBeans specification, although it is not required. The most obvious case of Spring deviating from the JavaBean specification is with the use of Constructor Injection to wire your beans together. ...
  19. Using Setter Injection in Spring

    by , 11-15-2011 at 03:02 PM
    An object is created in the Spring IOC container by invoking the zero-argument constructor. In this Setter Dependency Injection, the IoC container injects a component's dependencies into the component via JavaBean-style setter methods. A component's setters expose the set of the dependencies the IoC container can manage The dependent object is then passed as a parameter to the setter method. The CarService object needs data access objects (DAO) to execute data- base operations. The data access ...

    Updated 11-15-2011 at 03:10 PM by Spring Framework (Error in constructor for class)

    Categories
    Dependency Injection , Bean , Configuration