View RSS Feed


Spring Configuration

  1. Spring Configuration Based on Annotation

    by , 07-07-2012 at 07:25 PM
    The dependency injection can be configured using annotations in Spring 2.5 and above. The configuration of bean class can be done in class itself as compared to XML based bean wiring with the use of annotations on relevant method, class or field declaration.

    The XML injection is performed after the annotation based injection so the XML injection will override the annotation injection.

    The Spring container will not turn on the annotation wiring by default. So you will ...
  2. Resource Representation in Spring

    by , 11-28-2011 at 02:05 AM
    In my previous tips, I looked out how to make the key RESTful method calls. In all of them the controllerís handler method finishes, and a logical view name is usually returned. In the case that the method doesnít return a logical view name (a method returning void) then the logical view name is derived from the requestís URL. The DispatcherServlet passes the view name to a view resolver in order to facilitate the determination of which view should render the results of the request.


    Updated 11-30-2011 at 02:42 PM by Spring Framework

    XML , REST , Resource , Configuration , View
  3. Request Interception with Spring Security

    by , 11-27-2011 at 11:54 PM
    This is the last of a series of tips on Spring Security. From my previous tips, you should be able to configure Spring Security as well as setup login and logout. In the last tip, I will show you how to intercept requests. The <intercept-url> element is the key in the request-level security. Its pattern attribute is provide with a URL pattern that will be matched against incoming requests. If any requests match the pattern, then the <intercept-url>ís security rules will be applied. So ...
  4. Configuring Web Security in Spring

    by , 11-27-2011 at 11:49 PM
    In a previous tip, I showed how to add in the necessary servlet filters in your Spring application file. We added both the Delegating FilterProxy along with the another filter, FilterChainProxy. In general, Spring security will automatically create these beans for you when you configure the <http> element.

    Java Code:
        <form-login />
        <http-basic />
        <logout /> 
        <intercept-url pattern="/**" access="ROLE_USER"
  5. Setting up Secure Web Requests in Spring

    by , 11-27-2011 at 11:48 PM
    Continuing our investigation of Spring Security, in this tip I will look at making secure web requests. All activities that starts in a java web application is initiated via an HttpServletRequests. As such, this is where the security of your application will start. This security takes the form of request-level security. This involves declaring one or more URL patterns as requiring some level of granted authority and restricting access to those without authority from accessing the content of those ...
  6. Configuring Spring Security

    by , 11-27-2011 at 11:45 PM
    In the last tip, I introduced Spring Security and outlined the modules that are available. In this tip, we will look at configuring namespaces. Using Spring, the nice thing about Spring Security is that all the security elements are configured as beans in the application context. It is not uncommon to have a Acegi configuration containing dozens of bean declarations that span multiple pages.

    Another nice feature is that Spring Security has its own security-specific namespace to simplify ...
  7. Using Annotations for Transactions in Spring

    by , 11-27-2011 at 11:38 PM
    In the previous tip, I showed you how you could use XML to define a transaction. In this tip we will look at doing the same using annotations. It is blindly simple to use annotations for transactions. The tx namespace provides the <tx:annotation-driven> element. It appears like this :

    Java Code:
    <tx:annotation-driven transaction-manager="txManager" />
    With this you can now define transaction rules very easily. This tag tells Spring to examine all beans in ...
  8. Configuring Transactions in XML with Spring

    by , 11-27-2011 at 11:33 PM
    Continuing from my previous post, I will demonstrate how you can declare transactions in XML using Spring. Continuing from the previous tips on transaction attributes, letís dive straight in. Spring has a tx configuration namespace to simply declaring transactions in Spring. It is recommended that you include the aop namespace as well since some of the declarative transaction configuration elements use AOP configuration elements. The following shows how <tx:advice> is used for the Car service. ...

    Updated 11-27-2011 at 11:36 PM by Spring Framework

    Spring 3 , Configuration , Transaction , XML
    Attached Thumbnails Attached Images  
  9. Configuring a Hibernate SessionFactory in Spring

    by , 11-27-2011 at 11:11 PM
    In an earlier post, I showed you how to integrate Hibernate with Spring using the annotation oriented persistence. For those still interested in the standard way I outline how you can accomplish that below. The key to the standard way is to use a Hibernate org.hibernate.SessionFactory for all access to the database through the domain objects. The Java configuration for the SessionFactory is shown below.

    Java Code:
    import org.springframework.beans.factory.annotation.Value;
    Tags: hibernate Add / Edit Tags
    Hibernate , Configuration , Spring 3
  10. Configuring a Spring Data Source Continued

    by , 11-27-2011 at 11:02 PM
    There are a number of DataSource connection pool implementations provided by different vendors as well as projects like C3PO and Apache Commons DBCP providing popular open-source options. It is easy to switch between different data source implementations, because of the common DataSource interface. Spring provides a convenient data source implementations as well but it is not considered as powerful as the vendor or open source implementations. The simplest one is org.springframework.jdbc.datasource.DriverManagerD ataSource, ...
  11. Configuring Data Sources in Spring

    by , 11-27-2011 at 10:51 PM
    Spring offers a number of options for configuring a data sources via data source beans. These sources include the following:
    • Data sources that use JNDI
    • Data sources that use JDBC drivers
    • Data sources that pool connections

    There is significant differences in the features available depending on what you are trying to do. In this tip, I will outline how to configure different data sources and what they are best used for.

    If you are looking to do really robust ...
  12. Reduce your Spring XML using Java Configuration

    by , 11-27-2011 at 10:47 PM
    As Spring has grown so has the need to configure more and more components leading to the xml configuration files growing larger and larger. In order to address this issue, the Java configuration project was started as a standalone project at SpringSource and developed over the years. It was finally integrated into the core Spring framework in version 3.0. Now you are able to use a powerful ApplicationContext configuration abstraction that has the same level of isolation as its XML cousin. In this ...
  13. Writing AOP in Spring

    by , 11-16-2011 at 06:35 PM
    In this last installment of Spring AOP, we will provide an example of how to use AOP in Spring.
    In the listing below, we add a car to an order.

    Java Code:
    package com.acme.springexamples.main;
    import com.acme.springexamples.service.CarService;
    import com.acme.springexamples.service.OrderService;
    public class Main { public static void main(String[] args) {
    	ApplicationContext context = 
    		new ClassPathXmlApplicationContext("META-INF/spring/*.xml");
  14. 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(); 
    Or if you decide to use an application context, youíll need to register the ...
    Bean , Configuration , XML
  15. 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,
  16. 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
    Bean , Configuration , XML
  17. 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
  18. 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, ...
  19. 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 ...
    Bean , Configuration
  20. 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 ...
  21. 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. ...
  22. Contextualized Dependency Lookup in Spring

    by , 11-16-2011 at 01:16 PM
    At its core, Inversion of Control (IoC), and therefore Dependency Injection (DI) offers a mechanism for provisioning component dependencies and managing these dependencies throughout their lifecycles. A component that requires certain dependencies is often referred to as the dependent object or, in the case of IoC, the target. Generally, IoC can be broken down into two subtypes: Dependency Injection and Dependency Lookup. With Dependency Lookup IoC, a component must obtain a reference to a dependency. ...
  23. 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)

    Dependency Injection , Bean , Configuration