i would like to initialize (in Struts2) a property(data loading from a file) only once and make available that property for entire struts 2 application.
how can i achieve that? do i need override struts 2 dispatcher?
Regards
Raju
You could create a ServletContextListener defined in web.xml that opens your property file and sets the desired value to the ServletContext via:
getServletContext().setAttribute("dataKey", dataValue);
The ServletContext has application-wide scope.
Update:
You can create a new class that implements ServletContextListener (here is its JavaDoc: ServletContextListener), which requires that you define contextInitialized() and contextDestroyed() methods.
The method contextInitialized() is called right before your servlet begins accepting requests. In your contextInitialized() method, you would include the getServletContext().setAttribute("dataKey", dataValue) call.
In order to register your listener, you will need to add a listener definition in your web.xml file:
<listener>
<listener-class>CLASS_PATH.CLASS_NAME</listener-class>
</listener>
You'll need to replace CLASS_PATH.CLASS_NAME in the above XML with the class path and name of the context listener class you just created.
Related
I am trying to Inject ServletContext in my dependant scope class but it always gives the failure.
java.lang.IllegalStateException: No CXF message usable for JAX-RS #Context injections in that thread so can't use interface javax.servlet.ServletContext
I am not able to undestand the reason that why can't I inject it here. I am using a producer method and when inside producer method,I try to access ServletContext obj then it gives above exception. I have also checked if the Injected servlet context is null but it is not null. But When I call any method using it for example sc.getContextPath() it gives the above exception. Below is the code snippet:
#Dependent
public class AuthContexthandler {
#Context
ServletContext sc;
#Produces
JWTAuthContextInfo getInfo() {
try{
System.out.println(sc.getContextPath()); //here I get the above mentioned error
//rest of the code
}catch(Exception e){
e.printStackTrace();
}
}
}
you must use #inject instead of #Context.
you can use #Context to inject object instances related to the context of HTTP requests into to JAX-RS source class and as AuthContextHandler(as #Christoph Böhme said) is not a JAX-RS source class so you cannot use #Context
but as http://docs.jboss.org/weld/reference/latest/en-US/html_single/ says:
An object bound to a lifecycle context is called a bean. CDI includes
built-in support for several different kinds of bean, including the
following Java EE component types:
managed beans, and EJB session beans. Both managed beans and EJB
session beans may inject other beans. But some other objects, which
are not themselves beans in the sense used here, may also have beans
injected via CDI. In the Java EE platform, the following kinds of
component may have beans injected:
message-driven beans,
interceptors,
servlets,
servlet filters and
servlet event listeners,
JAX-WS service endpoints and handlers,
JAX-RS resources,
providers and javax.ws.rs.core.Application subclasses, and
JSP tag handlers and tag library event listeners.
it means you also can use #Inject annotation in your JAX-RS source class.
there are also some predefined Beans in CDI such as ServletContext that you can use #inject annotation to inject them.
https://docs.jboss.org/seam/3/servlet/latest/reference/en-US/html/injectablerefs.html
https://docs.oracle.com/javaee/7/tutorial/cdi-adv004.htm
In my application in some places we are using #ManagedBean annoation for Person bean and for the same Person bean we defining in the faces-confing.xml like below at the same time.
#ManagedBean("name=person")
#SessionScoped
Public class Person{
}
faces-config.xml
<managed-bean>
<managed-bean-name>person</managed-bean-name>
<managed-bean-class>com.test.sample.Person</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
my question is does this approach create two instances for the Person bean or it does matter if I do this? Does this have any effect on performance of my application If I do this for every Bean in my application?
There's a priority defined for this case. #ManagedBean annotation avoids having to configure an entry in faces-config.xml but, if you have both, the <managed-bean> entry overrides the annotation.
In your case, there'll be only one instance configured like your faces-config.xml entry. In your case, both approaches are configured the same way but, should you change your faces-config.xml entry to something like
<managed-bean>
<managed-bean-name>personBean</managed-bean-name>
<managed-bean-class>com.test.sample.Person</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Your bean will be registered under personBean rather than person (which is the name defined by the annotation).
I want to subclass AbstractPersistenceEventListener so I can register custom event listeners in Grails 2.5.4. However, where should I place these subclasses?
Most importantly I want these event listeners to use autowired beans, especially service beans. If I put these classes in src/groovy, it seems I'll have to manually register the beans in resources.groovy, which is an extra step I'd like to avoid.
This post shows how you can register a custom event listener in grails if you are not doing it using a plugin where you can register it inside doWithApplicationContext closure instead of doing it inside Bootstrap.groovy.
And you should put these classes under src/groovy. And no, you don't need to register the listener as a bean or any other beans again inside resources.groovy. However if your listener need to use any bean then you can declare a field for that and initialize it when you are registering the listener. For eg if you need to inject grailsApplication bean in your listener then do this while registering your listener:
def grailsApplication
def init = { servletContext ->
def applicationContext = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT)
applicationContext.eventTriggeringInterceptor.datastores.each { k, datastore ->
GormEventListener listener = new GormEventListener(datastore)
listener.with{
application = grailsApplication
}
applicationContext.addApplicationListener(listener)
}
I use custom implementation for Spring Security Plugin to override a default loadUser function. I read this manual https://grails-plugins.github.io/grails-spring-security-core/guide/userDetailsService.html and this is my resources.groovy file:
package spring
import security.ExtendedUserDetails
beans = {
UserDetailsService(ExtendedUserDetails)
}
but it was didn't loaded. App still use a default puligin implementation GormUserDetailsService. I use debugger and I see that ExtendedUserDetails never run.
So what's wrong?
This question is unanswered yet
Custom UserDetailsService Not Being Called - Grails & Spring Security Core
Looks like there is a typo in your bean name in resources.groovy. The name must be userDetailsService not UserDetailsService (capital U) and ExtendedUserDetails must implement GrailsUserDetailsService.
I need to inject a singleton bean into the session bean. Below are the corresponding classes. The problem is that the injected object is always null. I tried all of the JNDI lookup strings which my JBoss 7.0.1 server showed me during startup (i.e. JNDI bindings for session bean named GlobalBean in deployment unit subdeployment .. of deployment .. are as follows: ..). I also tried commenting out the #EJB annotation in GlobalBean.java and also tried to use the "ejb/GlobalBean" during injection. However, no luck. What could be the reason? Thx.
GlobalBean.java:
#Startup
#Singleton
#Remote(GlobalBeanRemote.class)
#EJB(name="ejb/GlobalBean", beanName="GlobalBean", beanInterface=GlobalBeanRemote.class)
#ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class GlobalBean implements GlobalBeanRemote
{
// CODE
}
SessionBean.java:
#Stateful
public class SessionBean extends ParentBean
{
#EJB(name="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
private GlobalBeanRemote globalBeanAPI3;
// CODE
}
In your SessionBean class try changing name attribute of #EJB to mappedName.
#EJB(mappedName="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
This will, of course, only work if your two beans are in the same module.
Update
Given that your beans are in separate modules, try using the java:app namespace:
#EJB(mappedName="java:app ...")
The java:app namespace is used to look up local enterprise beans packaged within the same application. That is, the enterprise bean is packaged within an EAR file containing multiple Java EE modules. JNDI addresses using the java:app namespace are of the following form:
java:app[/module name]/enterprise bean name[/interface name]
Also try removing GlobalBean's #EJB annotation. #EJB is used to define a dependency.