Cues-Answer for my Last q's

Does spring framework also needs container to work out with spring beans i.e; Does spring container needs a server to work out with?

EJB3.0 provides Dependency Injection for JNDI Objects.In other words,EJB3 dependency injection capabilities are limited in making the JNDI lookups more convinent,but do not go beyond that.
And there seems there is no point in making every internal object of the application to be managed by the application server.

In contrast to the above Spring Framework does it in a different way.Spring works in such a way that it lets the server does not know about each and every object of the application. You can link in objects from JNDI if you need to, but most of your application objects are not managed by the server.

Spring can manage application objects in all sorts of environments ,while EJB3 allows you to run them only in an EJB container.

Cues-Spring Sample Application for Aspect Oriented Programming

IDE:Eclipse GanyMede
This Aspect i took to work out is logging.
Jars:spring-context-2.5.6.jar
spring-aop-2.5.6.jar
commons-logging-1.1.1.jar
aspectjweaver-1.6.2.jar
spring-2.5.6.jar
Steps:

  1. Create an interface Foo.java
  2. Create an java class that implements this interface.
  3. Create one class which include the methods that are used for Aspect Oriented Programming.
  4. Create one more class which contains the public static void main method.
  5. Create spring's application context.xml (here it is spring.xml)where the spring beans are configured and AOP is configured.(Will be explained in detail in the coming lines)
Foo.java(Interface)
package org.check;
public interface Foo {
void getAfter();
//void getBefore(String myName,String change);
//public void getBefore(SetFields fields,String myChange);
public void getBefore(SetFields fields,SetFields fields1,String myChange);
}



Default Foo Service.java(Class Implementing Above Interface)
package org.check;
public class DefaultFooService implements Foo {
public void getAfter() {
}
}



Main Class
package org.check;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Boo {
public static void main(final String[] args) throws Exception {
BeanFactory ctx = new FileSystemXmlApplicationContext("E:/SECH_WS/TestLog/spring.xml");
Foo foo = (Foo) ctx.getBean("fooService");
foo.getAfter();
}
}
The line BeanFactory ctx = new FileSystemXmlApplicationContext"E:/SECH_WS/TestLog/spring.xml")
  • Bean factory class here we used is the spring container.
  • After loading the spring.xml into the container an instance of the bean is created.
  • The instance of the required class is obtained from getBean Method and referred there after.

SimpleProfiler.class

package org.check;
import org.check.BeanLogger;
public class SimpleProfiler {

public void afterMethod() throws Throwable {
System.out.println("afterMethod");}
public void beforeMethod(){
System.out.println("beforeMethod"); }

Now coming into the core part of the spring framework spring.xml.

spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="fooService" class="org.check.DefaultFooService"/>
<bean id="profiler" class="org.check.SimpleProfiler"/>
<bean id="myprofiler" class="org.check.MyProfiler"/>
<aop:config>
<aop:aspect ref="profiler">
<aop:pointcut id="aopafterMethod" expression="execution(* org.check.DefaultFooService.*(..))"/>
<aop:pointcut id="aopBefore" expression="execution(* org.check.DefaultFooService.*(..))"/>
<aop:after pointcut-ref="aopafterMethod" method="afterMethod"/>
<aop:before pointcut-ref="aopBefore" method="beforeMethod"/>
<aop:aspect/>

<aop:aspect ref="myprofiler">
<aop:pointcut id="aopBeforeOne" expression="execution(* org.check.DefaultFooService.*(..))"/>
<aop:pointcut id="aopBeforeTwo" expression="execution(* org.check.DefaultFooService.*(..))"/>
<aop:aspect/>
<aop:config/>
beans>

Things configured in the spring.xml and their explanation.

  • Three Beans are configured i.e.Default Foo Service,Simple Profiler,My Profiler.
  • Simple Profiler and My Profiler are the classes which i have used for writing the Logging logic.
  • These classes have many methods and these methods are executed before/after execution of any other classes methods or the entire class.
  • refers to beginning and ending of an aspect.
  • Now the profiler what is defined as the bean is made an aspect.
  • Next we need to define Point-cuts.Point Cuts are nothing but declarative expressions stating on what classes methods we need to execute this Aspect(here my aspect is Logging) logic.
  • gives the information that the method afterMethod inside the SimpleProfiler.class must be executed on all methods which comes under the format (* org.check.DefaultFooService.*(..))" after the execution of the method
  • gives the information that the method beforeMethod inside the SimpleProfiler.class must be executed on all methods which comes under the format (* org.check.DefaultFooService.*(..))" before the execution of the method.

Now Run the application as Java Application.

Output:
beforeMethod
afterMethod

So the logging methods are executed everytime when the corresponding methods are executed.



Cues--T.I.E

Look into the things this way..
  • How much knowledge we have about design patterns?
  • Tel me any of the design patterns followed for EJB'S?
  • Key performance issues faced with JSF?
  • What is Cartesian Product,Aggregate functions in Java?
  • How much we know about Comparable,Comparator Interfaces,Hashtable and Hash Set?
  • Do we really need Design Patterns?
  • How to refer EJB'S earlier to EJB3.0?

Cues--Interesting??

These are some of the questions for which no answers are found for me .Let me put the questions and ll update with answers in the coming posts.
  • How to test EJB's?
  • Does Hibernate need any performance Tuning?If so is it preferrable to do DBFine Tuning or Hibernate Fine Tuning??
  • Does transactions propagate for EJB's during the remote calls?
  • Does spring framework also needs container to work out with spring beans i.e; Does spring container needs a server to work out with?
  • Read that EJB's are heavy weight components.What does it mean?
  • How useful are Hudson and Maven build for the project..
  • What is the procedure to initalize lo4j.
  • What is the difference between the component and Service??
  • Can the entity manager be Cached using the TOPLINK ORM?
  • HPROF Profiler,Wily IntroScope Profiler,Optimizeit Profiler,JProbe profiler wat are all these..?

My Space

Hang over--Wrath of Grapes