Issue with Spring Method interceptor - spring

I'm having an issue with using Spring interceptor. I've a CXF service endpoint method which I'm trying a wrap with an interceptor to do some initialization. For some reason, the interceptor is not being invoked. Here's my spring entry:
<jaxrs:server id="acadConnectServer" address="/rest/acadconnect3">
<jaxrs:serviceBeans>
<ref bean="acadConnectResource" />
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="acadConnectResource"
class="com.test.connectchannel.service.AcadConnectChannelResource" />
<bean id="connectResource" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="acadConnectResource" />
<property name="interceptorNames">
<list>
<value>methodPointCut</value>
</list>
</property>
</bean>
<bean id="methodPointCut"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<ref local="methodInterceptor" />
</property>
<property name="mappedNames">
<list>
<value>search</value>
<value>searchJSONP</value>
</list>
</property>
</bean>
<bean id="methodInterceptor"
class="com.test.connectchannel.util.ConnectChannelInterceptor">
</bean>
As you can see, I've a CXF endpoint class AcadConnectChannelResource which has couple of methods search and searchJSONp. I've created the Named Method Cut interceptor to intercept these two method calls and so some initialization using the custom intercetor class.But, everytime the methods are invoked, the interceptor is not being called.
Not sure what I'm missing here, any pointer will be appreciated.
-Thanks

I might be entirely wrong, but don't you want
<ref bean="connectResource" />
instead of
<ref bean="acadConnectResource" />
in your <jaxrs:server>? You're proxying the resource, but using the raw resource instead of the proxy.

Related

Access spring property in jsp or javascript

I have a pageSize variable configured in spring properties. I need to access this pageSize property in almost all the jsps. What is the best way to get hold of this spring property.
src\main\resources\web.properties contains default.page.items.size=10
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:web.properties</value>
<value>classpath:core.properties</value>
</list>
</property>
</bean>
I know how to access the property in the controller but as this property is been accessed my mutliple pages hence i want some way to access it javascript or jsp directly
I used this in a old project (not sure if still works) in your dispatcher context declare a bean like:
<bean id="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:web.properties</value>
<value>classpath:core.properties</value>
</list>
</property>
</bean>
and then, if using standard JSTL view resolver:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2"></property>
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="exposedContextBeanNames">
<list>
<value>myProps</value>
</list>
</property>
</bean>
You should be able to access the properties inside JSP using ${myProps.XXX}

Creating multiple SchedulerFactoryBean in Quartz

I have run into a problem where I have two classes extending QuartzJobBean. The problem I am facing is to create two SchedulerFactoryBean. I did my research and found that setting the property schedulerName solves the problem. This did not work for me. I used #Qualifier also. If I create the two beans of SchedulerFactoryBean , Spring doesn't know which scheduler to refer to. I have two CronTriggers.
Code:
<!--
<bean name="quartzSchedulerR" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.task.QuartzScheduler" />
<property name="jobDataAsMap">
<map>
<entry key="rRSImpl" value-ref="rRSService" />
<entry key="SRObject" value-ref="SRObject"/>
</map>
</property>
</bean>
<bean id="cronTriggerR"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="quartzSchedulerR" />
<property name="cronExpression" value="0 30 12 ? * MON *" />
</bean>
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<property name="schedulerName" value="scheduleOne"/>
<property name="schedulerContextAsMap">
<map>
<entry key="rSchedulerServiceImpl" value-ref="rSchedulerServiceImpl"></entry>
</map>
</property>
<property name="jobDetails">
<list>
<ref bean="quartzSchedulerR" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTriggerR" />
</list>
</property>
</bean> -->
<bean id ="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<property name="schedulerContextAsMap">
<map>
<entry key="rSSImpl" value-ref="rSSImpl"></entry>
</map>
</property>
</bean>
<bean id="jobDetailFactory" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
<property name="targetBeanName">
<idref local="jobDetail" />
</property>
</bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean" scope="prototype">
<property name="jobClass" value="com.scheduler.SMTPMailJob " />
<property name="jobDataAsMap">
<map>
<entry key="rSSeImpl" value-ref="rSSImpl" />
<entry key="fUtil" value-ref="fUtil" />
<entry key="rService" value-ref="rService" />
<entry key="fusion" value-ref="fusion"/>
<entry key="fcproperties" value-ref="fcproperties"/>
</map>
</property>
</bean>
<bean id="jobTriggerFactory"
class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
<property name="targetBeanName">
<idref local="jobTrigger" />
</property>
</bean>
<bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"
scope="prototype">
</bean>
Currently I comment out the first scheduler and the application works as expected. But if I uncomment it, the second scheduler stops working. Any workaround for this issue.. ? Any help is appreciated.
EDIT: There is no error but I know that the job isn't scheduled. The error is basically that spring doesn't find a unique bean for com.quartz.Scheduler when both the SchedulerFactoryBean are defined. Basically how to configure multiple SchedulerFactoryBean for totally isolated classes.. ?
Please can you explain why do you need two scheduarfactorybeans.
As per spring doc, FactoryBean that creates and configures a Quartz Scheduler, manages its lifecycle as part of the Spring application context, and exposes the Scheduler as bean reference for dependency injection.
This means you cant have multiple instances of the bean.
Also as per your code you are trying to schedule multiple jobs which can be done using one schedularFactoryBean. Create Multiple job beans and their corresponding triggers and add them as list to the schedular factoryBean. All the triggers will be invoked as per configuration irrespective of whether it is a cron trigger or simple trigger.

Spring+ Hibernate :How to map single session factory in multiple modules (deployed as jar)

I am working on Spring + Struts2 and Hibernate currently, My requirement is :
I have Master module which will make DB connection, shared by all other modules (deployed as Jar) in the system.
All Module specific .hbm and persistence classes will exists in Module itself
So for example My master module will have Hibernate connection file (through spring) will all .hbm mapping files, Below is the sample of Hibernate connection made through Spring.
<bean id="dataSourceErik" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="testWhileIdle" value="true" />
<property name="minEvictableIdleTimeMillis" value="120000" />
<property name="timeBetweenEvictionRunsMillis" value="30000" />
</bean>
<bean id="sessionFactoryErik"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceErik" />
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.generate_statistics=true
hibernate.show_sql=false
hibernate.jdbc.batch_size=10
hibernate.bytecode.use_reflection_optimizer=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
net.sf.ehcache.configurationResourceName=configuration/ehcache.xml
</value>
</property>
<property name="mappingLocations">
<list>
<value>classpath:configuration/hibernate/Abc.hbm.xml</value>
<value>classpath:configuration/hibernate/Xyz.hbm.xml</value>
</list>
</property>
</bean>
<bean id="AbcActionDAO" class="au.com.master.persistance.dao.AbcDbSession">
<constructor-arg ref="sessionFactoryErik" />
</bean>
<bean id="XyzActionTypeDAO"class="au.com.master.persistance.dao.XyzDbSession">
<constructor-arg ref="sessionFactoryErik" />
</bean>
if i add below code in above xml i can access '''sessionFactory''' and can connect with the DB. as i am giving the path of deployed subModule jar file, see below code :
<property name="mappingJarLocations">
<list>
<value>WEB-INF/lib/subModule.jar</value>
</list>
</property>
All above .hbm and DAO/DbSession classes exists in Master module. Now i want to use this '''sessionFactoryErik''' in my Sub modules deployed as jar. so for that i created another xml file in Sub module which will take reference of above '''sessionFactoryErik''' and will have mappings of this module specific .hbm and DAO/DbSession. Refer below code:
<bean id="sessionFactoryMonitor" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="sessionFactoryErik" ref="sessionFactoryErik" />
<property name="mappingLocations">
<list>
<value>classpath:configuration/hibernate/DDDType.hbm.xml</value>
</list>
</property>
</bean>
<bean id="testActionDAO" class="au.com.java.subModule.persistance.dao.DddActionDbSession">
<constructor-arg ref="sessionFactoryMonitor" />
</bean>
if i deployed the project and reboot the server i am getting below error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactoryMonitor' defined in URL [jar:file:/home/developer/Project/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/webapps/erik/WEB-INF/lib/erik-monitor-1.0-SNAPSHOT.jar!/configuration/spring-monitor-dao.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactoryErik' of bean class [org.springframework.orm.hibernate4.LocalSessionFactoryBean]: Bean property 'sessionFactoryErik' is not writable or has an invalid setter method.
Can anyone help me for this. How can i get the same session object in sub module as only Master module can make connection as it will not have any knowledge of its deployed (deployed as jar) module.
Thanks.
Tapan
the class org.springframework.orm.hibernate4.LocalSessionFactoryBean doesn't have a property called sessionFactoryErik. I am guessing that you are injecting your datasource to LocalSessionFactoryBean. if so Change this
<bean id="sessionFactoryMonitor" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="sessionFactoryErik" ref="sessionFactoryErik" />
<property name="mappingLocations">
<list>
<value>classpath:configuration/hibernate/DDDType.hbm.xml</value>
</list>
</property>
</bean>
to this
<bean id="sessionFactoryMonitor" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="sessionFactoryErik" />
<property name="mappingLocations">
<list>
<value>classpath:configuration/hibernate/DDDType.hbm.xml</value>
</list>
</property>
</bean>
sessionFactoryErik bean returns org.hibernate.internal.SessionFactoryImpl object,
so you should specify org.hibernate.internal.SessionFactoryImpl or its super class
(SessionFactory) type in au.com.master.persistance.dao.XyzDbSession constructor
reference.

How to clear methodcacheinterceptor cache

I am developing a web application where I wrote a method, in that method I called a json url and parsing that and store in a pojo class.
I just cache this method using methodcacheinterceptor concept, My xml configuration below:
<bean id="methodCacheInterceptor"
class="web.app.services.MethodCacheInterceptor">
<property name="cache">
<ref local="methodCache" />
</property>
</bean>
<bean id="methodCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager" />
</property>
<property name="cacheName">
<value>videoItemsCache</value>
</property>
</bean>
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>/WEB-INF/ehcache.xml</value>
</property>
</bean>
<bean id="methodCachePointCut"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheInterceptor" />
</property>
<property name="patterns">
<list>
<value>.*web.app.services.ItemCollectionImpl.getCollection</value>
<value>.*web.app.services.ItemCollectionImpl.buildCollection</value>
</list>
</property>
</bean>
I set a time of 20 mins caching & it works perfectly!
All I want is for a particular activity I want to override & clear those cache, For the next time I visit page I want that whole method to be executed and data should not be loaded from cache (I want to clear the cache) for that particular activity.
FYI
CacheManager.getInstance().getCacheNames() - this prints my cachename which I have given in the xml configuration.
I have tried CacheManager.getInstance().clearAll(), removeall methods but it doesn't works!!
Anyone help me here!
Thanks in advance.

jackson jaxb annotations support in Spring

i'm looking for the simplest way of adding jaxb annotations support to jackson.
Jackson is added now to Spring by <mvc:annotation-driven/>. I need that by #ResponseBody annotation the Object is converted to xml or json dependently to the media type.
I'm new in spring-mvc so doesn't understand well yet how things work. Thanks.
Okay, I assume you want to be able to return both XML and JSON. To do this you need to create MessageConverters for both formats.
The XML message converter:
<bean id="xmlConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg>
<oxm:jaxb2-marshaller id="jaxb2Marshaller">
<!-- you must either bind your JAXB annotated classes here -->
<!-- OR provide a jaxb.index and use contextPath -->
<oxm:class-to-be-bound name="com.mycompany.MyClass"/>
</oxm:jaxb2-marshaller>
</constructor-arg>
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="xml"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property>
</bean>
The JSON message converter, which uses the JAXB annotations:
<bean id="jaxbAnnotationInspector"
class="org.codehaus.jackson.xc.JaxbAnnotationIntrospector"/>
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper">
<property name="annotationIntrospector" ref="jaxbAnnotationInspector"/>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper">
<bean ref="jacksonObjectMapper"/>
</property>
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="json"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property>
</bean>
And finally, the AnnotationMethodHandlerAdapter, which will convert the responses to the appropriate content type, depending upon the accept headers:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="xmlConverter"/>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>
Note that the JAXB support in jackson isn't 100% complete or correct all the time, but the developers are really good at fixing bugs and responding to error reports.

Resources