Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Monday, April 11, 2016

Execute DDL scripts when a spring application is initialized

Normally everyone would encounter a situation where in they want to execute few of the database scripts while deploying a solution. These scripts might include anything like executing a procedure, running a batch, insert script for defaulting of few values, update to existing dataset etc., 

When such situation arises, we can follow the below mentioned approach as an initial approach to handle it.

Spring by default tries to load a script file "data.sql" located in the classpath i.e., where the application.properties, log4j.properties etc., are present. 

Hence try to consolidate all the scripts into one file and name it as "data.sql" and place it under "<<apacheTomcatFolder>>\webapps\<<app>>\WEB-INF\classes", 

Disadvantages of this approach :
  • Since it is an automated process, any failure in the execution of scripts will not be known unless we go through the log files. Hence error percentage should be minimum.
  • Using the above approach, we cannot divide all the scripts into different modules and execute with different names. By default the spring will look for a file named "data.sql" unless someone overrides this setting in application.properties which is not explained in this blogpost.
  • A restart is needed for every change in this file. Try to use only when the script is flawless and also can be executed during the deployment. If there is a provision to execute the scripts in database then try to use that as it will provide more control over the scripts that will be run.

Thursday, July 16, 2015

Serialize manually using faster jackson library in spring

I am trying to get a JSON Structure for my object Order which internally has OrderDetails as oneToMany mapping,

@Entity
public class Order {    
    /*
        All the fields associated with this class
    */      
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "orderId")
    private Set<OrderDetails> orderDetails;

    //Getters for all properties defined in this class as jackson would depend on this thing
}
In my case I am using a textWebSocket which is expecting a message only in String format so I need to serialize the objects and push to the client, I am depending on faster jackson to do it and here it goes,

public String getObjectAsString()
{

    //orderObjs : Considering this is the list of objects of class Order

    ObjectMapper objMapper = new ObjectMapper();
            returnValue = objMapper.writerWithType(
                    objMapper.getTypeFactory().constructCollectionType(
                            List.class, Order.class)).writeValueAsString(
                    orderObjs);
    return returnValue;
}

objMapper.writerWithType(objMapper.getTypeFactory().constructCollectionType(List.class,Order.class)).writeValueAsString(orderObjs);

Monday, July 6, 2015

Use a new keyword and still inject the object : Spring

Spring works on dependency injection. This means there is no need to create an object explicitly, spring will do it for you.

But there are situations where you have to create new objects (ex., Reflection). By going with this approach you are conveying string that you will have control over the current object. i.e., any Autowired fields inside the object class will be null and spring will not consider injecting it. 

But there is a way to autowire those fields as well,

appctx.getAutowireCapableBeanFactory().autowireBean(wsdlParserAPIObject);

where appctx is ApplicationContext which is autowired.

http://stackoverflow.com/questions/31237062/using-class-forname-but-want-to-autowire-members-of-the-target-class

Monday, June 29, 2015

Call a procedure from Spring

How to call a procedure using spring :

In this blog post I would take you through the approach of calling a procedure from spring using "StoredProcedure" approach.

StoredProcedure is an abstract class which has to be extended.

Define a class which extends StoredProcedure, below is a sample which is shown,

Then call a procedure which is shown as below,





Friday, June 26, 2015

Autowire datasource while using JdbcDAOSupport

public class EntityDAO extends JdbcDaoSupport{

Logger logger = LoggerFactory.getLogger(EntityDAO.class);

@Autowired
private DataSource datasource;


/*
* Setting data source as it extends jdbcDAOSupport
*/
@Autowired
private void initialize()
{
setDataSource(datasource);
}