Reputation: 3683
I have a hello-world style java ee application using maven3 glassfish3 and javaee6.
I have a simple session bean:
package com.mytest.beans;
import com.mytest.interfaces.HelloBeanRemote;
import javax.ejb.Stateless;
@Stateless
public class HelloBean implements HelloBeanRemote{
@Override
public String sayHello() {
return "well hello there";
}
}
it exposes a remote interface. this interface is in the same module but a different package.
package com.mytest.interfaces;
import javax.ejb.Remote;
@Remote
public interface HelloBeanRemote {
public String sayHello();
}
I am using the maven-ejb-plugin to create an ejb-client artifact which my client module depends on.
I wrote this simple app-client
package com.mytest;
import com.mytest.interfaces.HelloBeanRemote;
import java.awt.Container;
import javax.ejb.EJB;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* Enterprise Application Client main class.
*
*/
public class Main {
@EJB
private static HelloBeanRemote hb;
public static void main( String[] args ) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
JButton button = new JButton("i have not been pushed");
pane.add(button);
frame.pack();
frame.setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
}
button.setText(hb.sayHello());
}
}
this worked for me as expected. however when I changed my app-client to this:
package com.mytest;
import com.mytest.interfaces.HelloBeanRemote;
import java.awt.Container;
import javax.ejb.EJB;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* Enterprise Application Client main class.
*
*/
public class Main {
public static void main( String[] args ) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
MyButton button = new MyButton("i have not been pushed");
pane.add(button);
frame.pack();
frame.setVisible(true);
}
}
...
package com.mytest;
import com.mytest.interfaces.HelloBeanRemote;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.ejb.EJB;
import javax.swing.JButton;
public class MyButton extends JButton implements ActionListener{
@EJB
private HelloBeanRemote hb;
public MyButton(String string){
super(string);
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
setText(hb.sayHello());
}
}
this will not work as expected when I click the button.
Can someone please enlighten me!
Thank you so much.
Upvotes: 1
Views: 848
Reputation: 692131
The glassfish development guide says:
Annotation is supported for the main class and the optional callback handler class in application clients.
So, either you inject the beans in the main class, and build some sort of singleton registry where all the classes will get a reference to the EJBs, or you use JNDI to get a reference to a bean, or you use some dependency injection framework (Spring, Guice, etc.) which will have its own injection rules and will use the registry or JNDI as a factory for the EJB references.
Upvotes: 3