Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Target runtime SpringSource tc Server Developer Edition (Runtime) v2.1 is not defined.

Solutions:

1. Delete the server
2. Recreate the server: create new instance, choose template insight.
3. Right click the project, find "Targeted Runtimes", removed the old one.

JMS in Spring

If you used JMS before, you knew the drill. Before we could send/receive a message, we need create get a connection factory, then create a connection, start a connection, create a session, create a queue or topic, create a producer/consumer....and after that, we need clean up the connection and session.

Now enter Spring

All we need is inject a JmsTemplate and Destination to producer/consumer bean.

Here is how to send a message in producer bean.
jmsTemplate.send(destination,
    new MessageCreator()
    {
     public Message createMessage(Session session) throws JMSException
     {
      TextMessage message = session.createTextMessage("Hello, World!");
      return message;
     }
    });

Here is how to receive a message in consumer bean.
Message message = (Message) jmsTemplate.receive(destination);
Here is the configuration for JmsTemplate and Destination
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
     <property name="brokerURL" value="tcp://localhost:61616"/>
</bean>

<bean id="queue_destination" class="org.apache.activemq.command.ActiveMQQueue">
     <constructor-arg index="0" value="BolunQueue"/>
</bean>
You are welcome!

Inject Non-Spring Bean

Additional Jar files:

org.springframework.aspects-3.1.0.M1.jar
c:\aspectj-1.6.11\lib\aspectjweaver.jar

Also need make sure JVM AspectJ-enabled.

put -javaagent:C:/aspectj-1.6.11/lib/aspectjweaver.jar
into VM Argument section

The above are just setting environments.

Why/when we want to inject stuff into a non-spring bean?
There are many scenarios that a bean is not instantiated by Spring, but we still want to inject value/references into this bean.

In config:

xmlns:aop="http://www.springframework.org/schema/aop"
...
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
...

<aop:spring-configured/>

<bean id='noSpringBean' class="packageA.NoSpringBean" abstract="true">
<property name="beanName" value="Bolun"/>
<property name="height" value="187"/>
</bean>

NoSpringBean.java

@Configurable("noSpringBean")
public class NoSpringBean
{
...
}

When you test it, you will see a lot of Waving stuff before our codes are really executed, performance wise, this could be really bad.

Adding org.springframework.transaction-3.1.0.M1.jar into path, removed a lot of complains.

Spring Example: Wired Together


package packageA;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloApp2 
{
   public static void main(String[] args) 
   {
     ApplicationContext ctx = new ClassPathXmlApplicationContext("config.xml");
     GreetingService greetingService = (GreetingService) ctx.getBean("greetingService");
     greetingService.sayALotOfGreeting();
     greetingService.theWinnderOfOscar();
     greetingService.sayGreeting();

     Lime iLime = (Lime) ctx.getBean("lime");
     iLime.drink();
   }
}

Spring Example: Injected Method Implementation


package packageA;

import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;

public class WinnerInEnvelop implements MethodReplacer
{
   public Object reimplement(Object arg0, Method arg1, Object[] arg2) throws Throwable 
   {
      System.out.println("Samuel Bolun Liu");
      return null;
   }
}

Spring Example: Config file

Groovy in Spring

Now I could inject Groovy class into Spring, but the problem is Groovy class has to implement a Java interface, in this way, I could get the bean from application context.

I am wondering is it possible that I could get Groovy class without using interface?

Groovy in Spring

--------------------for DI---------------------
org.springframework.asm-3.1.0.M1.jar
org.springframework.beans-3.1.0.M1.jar
org.springframework.context-3.1.0.M1.jar
org.springframework.core-3.1.0.M1.jar
org.springframework.expression-3.1.0.M1
c:\springsource\spring-framework-3.1.0.M1\dist\dependencies\commons-logging-1.1.1.jar
--------------------for method injection---------------------
c:\springsource\spring-framework-3.1.0.M1\dist\dependencies\cglib-nodep-2.2.jar
--------------------for Groovy integration------------------
c:\springsource\spring-framework-3.1.0.M1\dist\dependencies\aopalliance.jar
org.springframework.aop-3.1.0.M1.jar
c:\groovy-1.7.2\lib\antlr-2.7.7.jar
c:\groovy-1.7.2\lib\asm-3.2.jar
c:\groovy-1.7.2\lib\groovy-1.7.2.jar

Spring continue (2)

I tried to implement some method injection. Easy, right?

But I keep getting some weird errors, it turns out that I missed a jar called cglib, I just don't understand why Spring doesn't include this jar? or did I miss the boat?

I also found I actually need cglib-nodep-2.2.jar (see "nodep" part? means no dependency), if you just use cglib-2.2.jar, you'll get other compilation error, because this jar need other jars as dependencies.

Aghh....

check this out

Spring continue

You can configure both simple property values (using the value attribute) and properties with references to other beans (using the ref attribute). But value and ref are only useful when your bean’s properties are singular.

What if a property is a collection of values?

list
set
map
props

Spring 3.1.0.M1

Here are the contents of \spring-framework-3.1.0.M1\dist\

org.springframework.aop-3.1.0.M1.jar
org.springframework.asm-3.1.0.M1.jar
org.springframework.aspects-3.1.0.M1.jar
org.springframework.beans-3.1.0.M1.jar
org.springframework.context.support-3.1.0.M1.jar
org.springframework.context-3.1.0.M1.jar
org.springframework.core-3.1.0.M1.jar
org.springframework.expression-3.1.0.M1.jar
org.springframework.instrument.tomcat-3.1.0.M1.jar
org.springframework.instrument-3.1.0.M1.jar
org.springframework.jdbc-3.1.0.M1.jar
org.springframework.jms-3.1.0.M1.jar
org.springframework.orm-3.1.0.M1.jar
org.springframework.oxm-3.1.0.M1.jar
org.springframework.test-3.1.0.M1.jar
org.springframework.transaction-3.1.0.M1.jar
org.springframework.web.portlet-3.1.0.M1.jar
org.springframework.web.servlet-3.1.0.M1.jar
org.springframework.web.struts-3.1.0.M1.jar
org.springframework.web-3.1.0.M1.jar

I tried an really easy example, try to use ApplicationContext and ClassPathXmlApplicationContext, guess what, it's not that easy to make a simple program to work!

First, you have to add commons-logging-1.1.1.jar into classpath, I guess Spring is using this jar, but they don't put it into one of their package???
Second, I have to import all the following jars:

org.springframework.asm-3.1.0.M1.jar
org.springframework.beans-3.1.0.M1.jar
org.springframework.context-3.1.0.M1.jar
org.springframework.core-3.1.0.M1.jar
org.springframework.expression-3.1.0.M1.jar

although in the code, I don't see anywhere I am explicitly using asm.jar and expression.jar, just weird. 

AspectJ, Spring, and Guice

Recently, I have some time to take a look at these 3 things, mainly on AOP part.

Spring has a very good support for AOP, but the main problem is, all the beans need to be managed by Spring's container, otherwise, AOP won't work. This is ideal if you have a completely Spring-powered project. But it won't do you any good if you are not.

[Not exactly: Spring could inject values into non-Spring bean]

1. define bean in config as abstract plus the configurations, the id, let's see, is "testBean1"
2. add @configurable("testBean1") before the class in .java file.
3. add in config file.

It seems behind the scene, it's still AspectJ is doing all the work.

Guice has the same problem, the Java classes have to be binded by Guice's injector to have AOP work.

That leaves only one choice, AspectJ.
With AspectJ, we could have a very powerful tool to work on big project.

Imagine your boss gives you this task: log the result whenever a method named getMyMoneyBack() gets called. Sound easy? Not so fast, the problem is getMyMoneyBack() exists in thousands of classes, and they are called by millions of times. You really don't want to go to millions of places to put your log code there, do you?

With AspectJ, life is much easier.

You define a point cut like:

pointcut logGetMyMoneyBack() : call (* *.getMyMoneyBack(..))

String around() : logGetMyMoneyBack
{
String result = proceed();
System.out.println(result );
return result;
}
return proceed();