|
Page
1, Page
2, Page
3
|
|
How to integrate Java Server Faces
(JSF) with Spring?
|
JSF and Spring do share some of
the same features, most noticeably in the area of IOC services. By
declaring JSF managed-beans in the faces-config.xml configuration file,
you allow the FacesServlet to instantiate that bean at startup. Your
JSF pages have access to these beans and all of their properties.We can
integrate JSF and Spring in two ways:
DelegatingVariableResolver: Spring
comes with a JSF variable resolver that lets you use JSF and Spring
together.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD
BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<faces-config>
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
</faces-config>
The DelegatingVariableResolver will first delegate value lookups to the
default resolver of the underlying JSF implementation, and then to
Spring's 'business context' WebApplicationContext. This allows one to
easily inject dependencies into one's JSF-managed beans.
FacesContextUtils:custom
VariableResolver works well when mapping one's properties to beans in
faces-config.xml, but at times one may need to grab a bean explicitly.
The FacesContextUtils class makes this easy. It is similar to
WebApplicationContextUtils, except that it takes a FacesContext
parameter rather than a ServletContext parameter.
ApplicationContext ctx =
FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
|
What
is Significance of JSF- Spring integration ?
|
Spring - JSF integration is
useful when an event handler wishes to explicitly invoke the bean
factory to create beans on demand, such as a bean that encapsulates the
business logic to be performed when a submit button is pressed.
|
How
to integrate your Struts application with Spring?
|
To integrate your Struts
application with Spring, we have two options:
Configure Spring to manage your Actions as
beans, using the ContextLoaderPlugin, and set their dependencies in a
Spring context file.
Subclass Spring's ActionSupport classes and
grab your Spring-managed beans explicitly using a
getWebApplicationContext() method.
|
What
are the different types of bean injections?
|
There are two types of bean
injections.
1. By setter
2. By constructor
|
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.
<beans>
<bean id="bar"
class="com.act.Foo" Autowire="autowire type"/>
</beans>
|
What
are different types of Autowire types?
|
There are four different types
by which autowiring can be done.
- byName
- byType
- constructor
- autodetect
|
What
are the different types of events related to Listeners?
|
There are a lot of events
related to ApplicationContext of spring framework. All the events are
subclasses of org.springframework.context.Application-Event. They are
- ContextClosedEvent - This is fired when the context is
closed.
- ContextRefreshedEvent - This is fired when the context is
initialized or refreshed.
- RequestHandledEvent - This is fired when the web context
handles any request.
|
What is an Aspect?
|
An aspect is the cross-cutting
functionality that you are implementing. It is the aspect of your
application you are modularizing. An example of an aspect is logging.
Logging is something that is required throughout an application.
However, because applications tend to be broken down into layers based
on functionality, reusing a logging module through inheritance does not
make sense. However, you can create a logging aspect and apply it
throughout your application using AOP.
|
What
is a Jointpoint?
|
A joinpoint is a point in the
execution of the application where an aspect can be plugged in. This
point could be a method being called, an exception being thrown, or
even a field being modified. These are the points where your aspect's
code can be inserted into the normal flow of your application to add
new behavior.
|
What
is an Advice?
|
Advice is the implementation of
an aspect. It is something like telling your application of a new
behavior. Generally, and advice is inserted into an application at
joinpoints.
|
What
is a Pointcut?
|
A pointcut is something that
defines at what joinpoints an advice should be applied. Advices can be
applied at any joinpoint that is supported by the AOP framework. These
Pointcuts allow you to specify where the advice can be applied.
|
What
is an Introduction in AOP?
|
An introduction allows the user
to add new methods or attributes to an existing class. This can then be
introduced to an existing class without having to change the structure
of the class, but give them the new behavior and state.
|
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
is meant by Weaving?
|
The process of applying aspects
to a target object to create a new proxy object is called as Weaving.
The aspects are woven into the target object at the specified
joinpoints.
|
|
What
are the different points where weaving can be applied?
|
- Compile Time
- Classload Time
- Runtime
|
What
are the different advice types in spring?
|
- Around : Intercepts the calls to the target method
- Before : This is called before the target method is invoked
- After : This is called after the target method is returned
- Throws : This is called when the target method throws and
exception
- Around : org.aopalliance.intercept.MethodInterceptor
- Before : org.springframework.aop.BeforeAdvice
- After : org.springframework.aop.AfterReturningAdvice
- Throws : org.springframework.aop.ThrowsAdvice
|
What
are the different types of AutoProxying?
|
- BeanNameAutoProxyCreator
- DefaultAdvisorAutoProxyCreator
- Metadata autoproxying
|
What
is the Exception class related to all the exceptions that are thrown in
spring applications?
|
DataAccessException -
org.springframework.dao.DataAccessException
|
What
kind of exceptions those spring DAO classes throw?
|
The spring's DAO class does not
throw any technology related exceptions such as SQLException. They
throw exceptions which are subclasses of DataAccessException.
|
What
is DataAccessException?
|
DataAccessException is a
RuntimeException. This is an Unchecked Exception. The user is not
forced to handle these kinds of exceptions.
|
How
can you configure a bean to get DataSource from JNDI?
|
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property
name="jndiName">
<value>java:comp/env/jdbc/myDatasource</value>
</property>
</bean>
|
How
can you create a DataSource connection pool?
|
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driver">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>
|
How
JDBC can be used more efficiently in spring framework?
|
JDBC can be used more
efficiently with the help of a template class provided by spring
framework called as JdbcTemplate.
|
How
JdbcTemplate can be used?
|
With use of Spring JDBC
framework the burden of resource management and error handling is
reduced a lot. So it leaves developers to write the statements and
queries to get the data to and from the database.
JdbcTemplate template = new JdbcTemplate(myDataSource);
A simple DAO class looks like this.
public class StudentDaoJdbc implements StudentDao {
private
JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate
jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
more..
}
The configuration is shown below.
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="studentDao" class="StudentDaoJdbc">
<property
name="jdbcTemplate">
<ref
bean="jdbcTemplate"/>
</property>
</bean>
<bean id="courseDao" class="CourseDaoJdbc">
<property
name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
|
How
do you write data to backend in spring using JdbcTemplate?
|
The JdbcTemplate uses several of
these callbacks when writing data to the database. The usefulness you
will find in each of these interfaces will vary. There are two simple
interfaces. One is PreparedStatementCreator and the other interface is
BatchPreparedStatementSetter.
|
Explain
about PreparedStatementCreator?
|
PreparedStatementCreator is one
of the most common used interfaces for writing data to database. The
interface has one method createPreparedStatement().
PreparedStatement createPreparedStatement(Connection conn)
throws SQLException;
When this interface is implemented, we should create and return a
PreparedStatement from the Connection argument, and the exception
handling is automatically taken care off. When this interface is
implemented, another interface SqlProvider is also implemented which
has a method called getSql() which is used to provide sql strings to
JdbcTemplate.
|
Explain
about BatchPreparedStatementSetter?
|
If the user what to update more
than one row at a shot then he can go for BatchPreparedStatementSetter.
This interface provides two methods
setValues(PreparedStatement ps, int i) throws SQLException;
int getBatchSize();
The getBatchSize() tells the JdbcTemplate class how many statements to
create. And this also determines how many times setValues() will be
called.
|
Explain
about RowCallbackHandler and why it is used?
|
In order to navigate through the
records we generally go for ResultSet. But spring provides an interface
that handles this entire burden and leaves the user to decide what to
do with each row. The interface provided by spring is
RowCallbackHandler. There is a method processRow() which needs to be
implemented so that it is applicable for each and everyrow.
void processRow(java.sql.ResultSet rs);
|
|
Page
1, Page
2, Page
3
|
|
|
|