Java
Java
Pointcut Designator: A pointcut expression starts with a pointcut designator (PCD), which is a
keyword telling Spring AOP what to match. There are several pointcut designators, such as the
execution of a method, a type, method arguments, or annotations. execution: This is used to match
method execution (join-points). This is a primary designator, and is used most of the time while
working with Spring AOP. within: This designator has the limitation of matching of join-points
within certain types only. It's not as flexible as execution. For example, it's not allowed to specify
return types or method parameter mapping. If the patterns with within are up to the Java package, it
matches all methods of all classes within that package. If the pattern is pointing to a specific class,
then this designator will cover all. • Pointcut Annotations: Using annotations is more convenient
than using patterns. While patterns might be anything between a big cannon and a scalpel the
annotations are definitely a scalpel, by only getting the pointcut that the developer has manually
specified
The pointcuts defined in such an aspect can be referred to anywhere that you need a pointcut
expression. For example, to make the service layer transactional
<aop:config>
<aop:advisor
pointcut="com.xyz.someapp.SystemArchitecture.businessService()"
advice-ref="tx-advice"/>
</aop:config>
<tx:advice id="tx-advice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
spring AOP users are likely to use the execution pointcut designator the most often. The format of
an execution expression is:
the execution of any public method:
execution(public * *(..))
the execution of any method with a name beginning with "set":
execution(* set*(..))
the execution of any method defined by the AccountService interface:
execution(* com.xyz.service.AccountService.*(..))
the execution of any method defined in the service package:
execution(* com.xyz.service.*.*(..))
the execution of any method defined in the service package or a sub-package:
execution(* com.xyz.service..*.*(..))
any join point (method execution only in Spring AOP) within the service package:
within(com.xyz.service.*)
any join point (method execution only in Spring AOP) within the service package or a sub-
package:
within(com.xyz.service..*)
any join point (method execution only in Spring AOP) where the proxy implements
the AccountService interface:
this(com.xyz.service.AccountService)
'this' is more commonly used in a binding form :- see the following section on advice for how
to make the proxy object available in the advice body.
any join point (method execution only in Spring AOP) where the target object implements
the AccountService interface:
target(com.xyz.service.AccountService)
'target' is more commonly used in a binding form :- see the following section on advice for
how to make the target object available in the advice body.
any join point (method execution only in Spring AOP) which takes a single parameter, and
where the argument passed at runtime is Serializable:
args(java.io.Serializable)
'args' is more commonly used in a binding form :- see the following section on advice for
how to make the method arguments available in the advice body.
'@target' can also be used in a binding form :- see the following section on advice for how
to make the annotation object available in the advice body.
any join point (method execution only in Spring AOP) where the declared type of the target
object has an @Transactional annotation:
@within(org.springframework.transaction.annotation.Transactional)
'@within' can also be used in a binding form :- see the following section on advice
for how to make the annotation object available in the advice body.
any join point (method execution only in Spring AOP) where the executing method has
an @Transactional annotation:
@annotation(org.springframework.transaction.annotation.Transactional)
'@annotation' can also be used in a binding form :- see the following section on
advice for how to make the annotation object available in the advice body.
any join point (method execution only in Spring AOP) which takes a single parameter, and where the
runtime type of the argument passed has the @Classified annotation:
@args(com.xyz.security.Classified)
'@args' can also be used in a binding form :- see the following section on advice
for how to make the annotation object(s) available in the advice body.
2)- what is bean autowiring and its types of mode?
approach, we utilize the constructor arguments to inject the dependencies into the
bean.
Let’s extend our example with a constructor. In the following code,
the Shape dependency is injected through the constructor when the application starts.
import org.springframework.beans.factory.annotation.Autowired;
// Other methods...
}
Consider a ShapeService class that requires a Shape. In this example, Spring will
automatically inject a Shape bean into the shape field when creating an instance
of ShapeService.
This approach is also called the setter injection and is recommended for
the “optional“ dependencies.
3)-Explain map hierarchy explain any 2 operations with example?
The Map interface present in java.util package represents a mapping between a key
and a value. A map contains unique keys and each key can map to at most one
value. Some implementations allow null key and null value like the HashMap and
LinkedHashMap, but some do not like the TreeMap. The order of a map depends
on the specific implementations. For example, TreeMap and LinkedHashMap have
predictable order, while HashMap does not.
The Map interface is not a subtype of the Collection interface. Therefore, it behaves
a bit differently from the rest of the collection types.
The classes which implement the Map interface are HashMap, LinkedHashMap
and TreeMap.
Method Description
This method is used to clear and remove all of the
clear() elements or mappings from a specified Map
collection.
This method is used to check for equality between
two maps. It verifies whether the elements of one
equals(Object)
map passed as a parameter is equal to the elements of
this map or not.
This method is used to retrieve or fetch the value
mapped by a particular key mentioned in the
get(Object)
parameter. It returns NULL when the map contains
no such mapping for the key.
This method is used to generate a hashCode for the
hashCode()
given map containing key and values.
This method is used to check if a map is having any
isEmpty() entry for key and value pairs. If no mapping exists,
then this returns true.
This method is used to clear and remove all of the
clear() elements or mappings from a specified Map
collection.
HashMap class implements the Map interface which allows us to store key and
value pair, where keys should be unique. If you try to insert the duplicate key, it
will replace the element of the corresponding key. It is easy to perform operations
using the key index like updation, deletion, etc. HashMap class is found in the
java.util package.
HashMap in Java is like the legacy Hashtable class, but it is not synchronized. It
allows us to store the null elements as well, but there should be only one null key.
Since Java 5, it is denoted as HashMap<K,V>, where K stands for key and V for
value.
import java.util.*;
public class HashMapDemo {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Angel", 10);
map.put("Liza", 30);
map.put("Steve", 20);
for (Map.Entry<String, Integer> e : map.entrySet()) {
System.out.println(e.getKey() + " " + e.getValue());
}
}
}
Output:
vaibhav 20
vishal 10
sachin 30
LinkedHashMap is just like HashMap with an additional feature of maintaining the
order of elements inserted into it. HashMap provided the advantage of quick
insertion, search and deletion but it never maintained the track and order of
insertion which the LinkedHashMap provides where the elements can be accessed
in their insertion order.
Example: Program that demonstrates LinkedHashMap implementation
import java.util.*;
public class LinkedHashMapDemo {
public static void main(String[] args)
{
Map<String, Integer> map= new LinkedHashMap<>();
map.put("Angel", 10);
map.put("Liza", 30);
map.put("Steve", 20);
Output:
Angel 10
Liza 30
Steve 20
4)- what is constructor injection? implement with suitable example?
Constructor-based DI is accomplished by the container invoking a constructor
with a number of arguments, each representing a dependency.
The following example shows a class that can only be dependency-injected with
constructor injection.
public class SimpleMovieLister
{
// the SimpleMovieLister has a dependency on a MovieFinder
private IMovieFinder movieFinder;
// a constructor so that the Spring container can 'inject' a MovieFinder
public MovieLister(IMovieFinder movieFinder)
{
this.movieFinder = movieFinder;
}
// business logic that actually 'uses' the injected IMovieFinder is omitted...
}
Setter-based DI is accomplished by the container invoking setter properties on your
objects after invoking a no-argument constructor or no-argument static factory
method to instantiate your object.
The following eample shows a class that can only be dependency injected using
pure setter injection.
public class MovieLister
{
private IMovieFinder movieFinder;
public IMovieFinder MovieFinder
{
set
{
movieFinder = value;
}
}
// business logic that actually 'uses' the injected IMovieFinder is omitted}
5)- Vector and give an example
Before Collections, the standard way used for grouping data was using the array,
Vector and HashTable. But all of these have different methods and syntax for
accessing data and performing operations on it. For example, Arrays uses the
square brackets symbol [] to access individual data members whereas Vector uses
the method elementAt(). These differences led to a lot of discrepancies. Thus, the
“Collection Framework” was introduced in JDK 1.2 to bring a unified mechanism
to store and manipulate a group of objects.
Any group of objects which are represented as a single unit is known as the
collection of the objects. In Java Collections, individual objects are called as
elements.
The “Collection Framework” holds all the collection classes and interface in it.
These classes and interfaces define all operations that you can perform uniformly
on different types of data such as searching, sorting, insertion, manipulation, and
deletion.
The Java Collection framework has
1. Interfaces and its implementations, i.e., classes
2. Algorithm
The java.util package contains all the classes and interfaces for the Collection
framework.
package mypack;
public class students implements java.io.Serializable
{
private int RollNo;
private String name;
public students(){}
public void setRollNo(int id)
{
this.RollNo=RollNo;
}
public int getId()
{
return RollNo;
}
public void setName(String name)
{
this.name=name;
7)- EXplain session management in jsp with its types and write code on cookie management?
There are four techniques which can be used to identify a user session.
a) Cookies
b) Hidden Fields
c) URL Rewriting
d) Session Object
Cookie: -
A cookie is small information which is sent by the web server to a web client. It is save at the
client side for the given domain and path. It is basically used to identify a client when sending a
subsequent request. Thereare two types of cookies:
a) Session cookies: these are temporary cookies and its get deleted as
soon as the user closes the browser and next time whenever the client
visits the same websites, server will treat the request as a new client
as cookies are already deleted.
b) Persistent Cookie: its remains on hard drive, until we delete them or
they gets expire.
Hidden Filed:
Hidden field are similar to other input fields with the only difference is that
these fields are not get displayed on the page but the values of these fields
are sent to other input fields.
For example: <input type=”hidden” name=”sessionId” value=”unique
value”/>
URL Rewriting:
It is a process of appending or modifying the url structure when loading a
page. The request made by the client is always treated as new request and
the server cannot identify whether the request is new one or the previous
same client .so due to this property of HTTP protocol and web servers are
called stateless.
Session Object:
It is used for session management. When a user enters a website for the first
time Httpsession is obtained via request. When session is created, server
generates aunique ID and attaches that ID with every request of that user to
server with which the server identifies the client.
How to access or get a session object: By calling getSession() method and it is
an implicit object.
a) HttpSession x=request.getSession()
b) HttpSession y=new request.getSession(Boolean)
Custom tags, also known as JSP tag extensions (because they extend the set of built-in JSP tags),
provide a way of encapsulating reusable functionality on JSP pages