ex: String sql="select distinct PRIMARY_OWNER from "+xyzSchema+"v_rptg rptg"+
" where rptg.fac_id=:facId and " +
" (:cDate >= cit_eff_asof_dt and :cDate < cit_eff_until_dt) "
+ " and (:cDate >= en_eff_asof_dt and :cDate < en_eff_until_dt) ";
So now you'll hve to bind the highlighted variables in the query.
Object[] params = { facId,cDate, cDate, cDate, cDate};
To your query, pass in sql and the parameters bound (ex: for i/p parameter like :cDate in your query, params should contain a cDate in the same order as it expects in the query)
long clietnOid=queryForLong(sql,params);
Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts
Wednesday, June 25, 2008
Thursday, June 19, 2008
Reading from Properties file into Spring Application Context.xml
Problem: How to read a value from your properties file into your spring's applicationContext.xml..?
Here is how you do it. spring's angle brackets are replaced with []
[bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"]
[property name="location" value="classpath:application.properties"/]
[/bean]
[!-- A sample bean that needs some settings. --]
[bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"]
[property name="driverClassName" value="${jdbc.driver}"/]
[property name="url" value="${jdbc.url}"/]
[property name="username" value="${jdbc.username}"/]
[property name="password" value="${jdbc.password}"/]
[/bean]
Ex: If your properties file is :
jdbc.driver=org.h2.Driver
jdbc.url=jdbc:h2:mem:example
jdbc.username=sa
jdbc.password=asdfa
you can these values directly into ur application ctxt.
More info read:
Here is how you do it. spring's angle brackets are replaced with []
[bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"]
[property name="location" value="classpath:application.properties"/]
[/bean]
[!-- A sample bean that needs some settings. --]
[bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"]
[property name="driverClassName" value="${jdbc.driver}"/]
[property name="url" value="${jdbc.url}"/]
[property name="username" value="${jdbc.username}"/]
[property name="password" value="${jdbc.password}"/]
[/bean]
Ex: If your properties file is :
jdbc.driver=org.h2.Driver
jdbc.url=jdbc:h2:mem:example
jdbc.username=sa
jdbc.password=asdfa
you can these values directly into ur application ctxt.
More info read:
Friday, June 6, 2008
Spring Quick Ref Guide
What is Spring ?
Spring is an open source framework created to address the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use while also providing a cohesive framework for J2EE application development.
Spring is a lightweight, inversion of control AOP based container framework.
What are features of Spring ?
Lightweight:
spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 1MB. And the processing overhead is also very negligible.
Inversion of control (IOC):
Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.
Aspect oriented (AOP):
Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.
Container:
Spring contains and manages the life cycle and configuration of application objects.
MVC Framework:
Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework.
Transaction Management:
Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring's transaction support is not tied to J2EE environments and it can be also used in container less environments.
JDBC Exception Handling:
The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy. Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS
What is a BeanFactory?
A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.
What is XMLBeanFactory?
BeanFactory has many implementations in Spring. But one of the most useful one is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file. To create an XmlBeanFactory, pass a java.io.InputStream to the constructor. The InputStream will provide the XML to the factory. For example, the following code snippet uses a java.io.FileInputStream to provide a bean definition XML file to XmlBeanFactory.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
To retrieve the bean from a BeanFactory, call the getBean() method by passing the name of the bean you want to retrieve.
MyBean myBean = (MyBean) factory.getBean("myBean");
Explain Bean lifecycle in Spring framework?
1. The spring container finds the bean’s definition from the XML file and instantiates the bean.
2. Using the dependency injection, spring populates all of the properties as specified in the bean definition.
3. If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
5. If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called.
6. If an init-method is specified for the bean, it will be called.
7. Finally, if there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.
What are the important beans lifecycle methods?
There are two important bean lifecycle methods. The first one is setup which is called when the bean is loaded in to the container. The second method is the teardown method which is called when the bean is unloaded from the container.
What is Auto wiring?
You can wire the beans as you wish. But spring framework also does this work for you. It can auto wire the related beans together. All you have to do is just set the autowire attribute of bean tag to an autowire type.
What is a Target?
A target is the class that is being advised. The class can be a third party class or your own class to which you want to add your own custom behavior. By using the concepts of AOP, the target class is free to center on its major concern, unaware to any advice that is being applied.
What is a Proxy?
A proxy is an object that is created after applying advice to a target object. When you think of client objects the target object and the proxy object are the same.
What are the different points where weaving can be applied?
Compile Time
Classload Time
Runtime
Spring is an open source framework created to address the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use while also providing a cohesive framework for J2EE application development.
Spring is a lightweight, inversion of control AOP based container framework.
What are features of Spring ?
Lightweight:
spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 1MB. And the processing overhead is also very negligible.
Inversion of control (IOC):
Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.
Aspect oriented (AOP):
Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.
Container:
Spring contains and manages the life cycle and configuration of application objects.
MVC Framework:
Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework.
Transaction Management:
Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring's transaction support is not tied to J2EE environments and it can be also used in container less environments.
JDBC Exception Handling:
The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy. Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS
What is a BeanFactory?
A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.
What is XMLBeanFactory?
BeanFactory has many implementations in Spring. But one of the most useful one is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file. To create an XmlBeanFactory, pass a java.io.InputStream to the constructor. The InputStream will provide the XML to the factory. For example, the following code snippet uses a java.io.FileInputStream to provide a bean definition XML file to XmlBeanFactory.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
To retrieve the bean from a BeanFactory, call the getBean() method by passing the name of the bean you want to retrieve.
MyBean myBean = (MyBean) factory.getBean("myBean");
Explain Bean lifecycle in Spring framework?
1. The spring container finds the bean’s definition from the XML file and instantiates the bean.
2. Using the dependency injection, spring populates all of the properties as specified in the bean definition.
3. If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
5. If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called.
6. If an init-method is specified for the bean, it will be called.
7. Finally, if there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.
What are the important beans lifecycle methods?
There are two important bean lifecycle methods. The first one is setup which is called when the bean is loaded in to the container. The second method is the teardown method which is called when the bean is unloaded from the container.
What is Auto wiring?
You can wire the beans as you wish. But spring framework also does this work for you. It can auto wire the related beans together. All you have to do is just set the autowire attribute of bean tag to an autowire type.
What is a Target?
A target is the class that is being advised. The class can be a third party class or your own class to which you want to add your own custom behavior. By using the concepts of AOP, the target class is free to center on its major concern, unaware to any advice that is being applied.
What is a Proxy?
A proxy is an object that is created after applying advice to a target object. When you think of client objects the target object and the proxy object are the same.
What are the different points where weaving can be applied?
Compile Time
Classload Time
Runtime
Thursday, March 13, 2008
Spring AOP Simplified
Spring AOP (Aspect Oriented Programming) Simplified:
Here is my exploration os spring aop. An interceptor which logs the method name
and the time taken for each method being invoked in the path of executing a request.
Back to Basics:
What is AOP:
AOP is a philosophy that is related to style of programming.
It addresses issues that can be solved in other approaches, but in a more elegant way.
To understand the spirit of AOP, you must understand the following issues
1) Things like logging, When you enter and exit methods and time take to execute are common across
the application (Caution: logging to debug ur app is different). It need not necessarily interfere with ur code.
2) Transaction management
3) Entitlements etc
Here is an example of how to implement AOP in spring
Lets try to see when user performs any action, see if session is active and throw
and error if its not.
public class SessionInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Suneetha in Sarc session Interceptor");
logger.info("calling... >>" + methodInvocation.getThis() + ">> "+ methodInvocation.getMethod().getName());
System.out.println("calling... >>" + methodInvocation.getThis() + ">> "+ methodInvocation.getMethod().getName());
HttpSession session = request.getSession(false);
if (session != null) {
System.out.println("Session is healthy");
}else{
System.out.println("Session expired");
throw new SessionTimeoutException("session expired");
}
//Make sure you always have these lines of code
try {
Object retVal = methodInvocation.proceed();
return retVal;
} catch (Exception e) {
logger.error(Constants.EVENT_TYPE_DIAGNOSIS + " "
+ Constants.BUSINESS_PROCESS_NAME + " "
+ this.getClass().getName() + " ", e);
throw e;
}
}
}
So now the wiring:
In your application context.xml you shud define the above bean and
also wire the aspect to it.
As always define the bean:
bean id="sessionInterceptor" class="com.jpmchase.srgtarc.interceptor.SessionInterceptor"
property name="sarcUserService"
ref bean="sarcUserService"/
/property
/bean
Now define the Aspect bean
bean name="logAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
property name="beanNames"
value /actions/smarc*,/actions/sMArc*,/actions/saMArc* /value
/property
property name="interceptorNames"
list
value sessionInterceptor /value
/list
/property
/bean
So the above aspect will be executed for all kinds of actions starting with
smarc or sMArc or saMArc etc
Other Killer AOP examples:
AOP thoroughly explained
spring wiki doc. Simple but profound
yes Knowledge is Power
Here is my exploration os spring aop. An interceptor which logs the method name
and the time taken for each method being invoked in the path of executing a request.
Back to Basics:
What is AOP:
AOP is a philosophy that is related to style of programming.
It addresses issues that can be solved in other approaches, but in a more elegant way.
To understand the spirit of AOP, you must understand the following issues
1) Things like logging, When you enter and exit methods and time take to execute are common across
the application (Caution: logging to debug ur app is different). It need not necessarily interfere with ur code.
2) Transaction management
3) Entitlements etc
Here is an example of how to implement AOP in spring
Lets try to see when user performs any action, see if session is active and throw
and error if its not.
public class SessionInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Suneetha in Sarc session Interceptor");
logger.info("calling... >>" + methodInvocation.getThis() + ">> "+ methodInvocation.getMethod().getName());
System.out.println("calling... >>" + methodInvocation.getThis() + ">> "+ methodInvocation.getMethod().getName());
HttpSession session = request.getSession(false);
if (session != null) {
System.out.println("Session is healthy");
}else{
System.out.println("Session expired");
throw new SessionTimeoutException("session expired");
}
//Make sure you always have these lines of code
try {
Object retVal = methodInvocation.proceed();
return retVal;
} catch (Exception e) {
logger.error(Constants.EVENT_TYPE_DIAGNOSIS + " "
+ Constants.BUSINESS_PROCESS_NAME + " "
+ this.getClass().getName() + " ", e);
throw e;
}
}
}
So now the wiring:
In your application context.xml you shud define the above bean and
also wire the aspect to it.
As always define the bean:
bean id="sessionInterceptor" class="com.jpmchase.srgtarc.interceptor.SessionInterceptor"
property name="sarcUserService"
ref bean="sarcUserService"/
/property
/bean
Now define the Aspect bean
bean name="logAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
property name="beanNames"
value /actions/smarc*,/actions/sMArc*,/actions/saMArc* /value
/property
property name="interceptorNames"
list
value sessionInterceptor /value
/list
/property
/bean
So the above aspect will be executed for all kinds of actions starting with
smarc or sMArc or saMArc etc
Other Killer AOP examples:
AOP thoroughly explained
spring wiki doc. Simple but profound
yes Knowledge is Power
Subscribe to:
Posts (Atom)