AOP with Spring MVC :1.2.5 - spring

Mine is a old legacy project and I am using Spring 1.2.5 . I am trying below code to apply aop over one of my business classes :
<bean id="hijackBeforeMethodBean" class="com.mycom.myapp.common.logging.LoggingAdvice" />
<bean id="demoServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref local="demoDelegate"></ref>
</property>
<property name="interceptorNames">
<list>
<value>hijackBeforeMethodBean</value>
</list>
</property>
</bean>
</beans>
In addition to this :
I firstly tried this over controllers ,but it didnt work.
I followed this link
http://www.javatpoint.com/spring-aop-example
but this asks me to inject or use the proxyfactory bean. A sper my current code I am injecting delegate beans in the controllers ,so in tht case I might have to change all my bean ids.
Please suggest some solution to this.

Related

How to use AfterAdvice Interface in java

I am new to spring.I know that AfterAdvice will cause the after method to execute whether target method completes or exits with exception, but i am not able to find out any example for it.
As AfterAdvice is a marker interface, I don't know which method i need to define in it's implementation class.
Thanks,
You do not have to implement those interfaces directly. Instead, you use either
Use #After annotation to mark the method you want to it to be called.
Use spring xml bean configuration aop:advice to declare an after advice method
However, if you choose to use ProxyFactoryBean
As you indicate that you want to use ProxyFactoryBean, you can declare the xml like this
<bean id="interceptor"
class="yourimplementation">
</bean>
<bean id="setterAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="interceptor"/>
</property>
<property name="patterns">
<list>
<value>.*set.*</value>
</list>
</property>
</bean>
<bean id="person"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.mycompany.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>setterAdvisor</value>
</list>
</property>
</bean>
For the java implementation, there is no use to implement Advice interface. You should either implement ThrowingAdvice or AfterReturningAdvice. Refer to this for more info.
For more information, you can refer to a couple tutorial guides on Spring AOP and play around with it to get a sense.

spring-batch : load and use a property file

I am new on Spring batch so am here to ask some basic advice.
What is the best approach to load a config file in memory (or bean) and use its content while the spring Job/step are running ?
I am not sure but based on some google search I found the below scenario even if I dont quite understand why I should define a writer even if i dont need it :
step1 : load config file (the content is two field delimited by =)
step2 : perform some java code and use the previous config file
so for the step 1 :
<bean id="inputFile" class="org.springframework.core.io.FileSystemResource" scope="step">
<constructor-arg value="path_config_file"/>
</bean>
<bean id="readerConfigFile" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" ref="inputFile"/>
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="field,value"/>
<property name="delimiter" value="="/>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="configProperties"/>
</bean>
</property>
</bean>
</property>
</bean>
<bean id="outputConfig" class="outputConfig"></bean>
<bean id="configProperties" class="configProperties" scope="prototype"/>
so my question are :
How can I use the information gathered in the file ? Should I put them in the Java bean ?
How can I pas this info between different step or make them persistent in the whole application life-cycle ?
Would you recommend to use a itemProcessor to achieve the above ?
Any advice are most than welcome
I'm a bit confused about your questions because I think you only need to load a properties file in spring context using a PropertiesFactoryBean:
<bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>file:path_config_file</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="propertiesArray">
<list>
<ref bean="config"/>
</list>
</property>
</bean>
and you can refer to property values using ${} placeholder; but this solution is unrelated to spring-batch; I don't need it!
About your questions:
Using a POJO is a good way because spring-batch offers in-box mapping strategies (BeanWrapperFieldSetMapper in your case)
Objects used in a job are accessible only in job context, not in application context (this is why I think you need a PropertiesFactoryBean).To pass object between steps read How can we share data between the different steps of a Job in Spring Batch?
ItemProcessor is requested if you need to convert an object T read from a ItemReader<T> to an object of type S written by an ItemWriter<S>. So no, you don't need an ItemProcessor.
I hope I was clear, English is not my native language

OpenEntityManagerInViewInterceptor for CXF

I have a Web service application based on JPA (Hibernate), Spring and CXF.
I am facing some lazy-load exceptions after transactional business methods (because I need some extra beans to be rendered in the rpesentation layer), and I wanted to give a try to the OpenSession/EntityManagerInView pattern.
Please do not argue this choice, we are just giving it a try.
The issue is that, since I am using CXFServlet, instead of standard Spring Servlet, I cannot use OpenEntityManagerInViewFilter in web.xml.
I cannot use either OpenEntityManagerInViewInterceptor which applies as a WebRequest Interceptor (and does not work with CXF interceptor/filters).
Finally I am aware of the HibernateInterceptor, an AOP proxy which wraps any method into a session. But still : this one is for the Hibernate API, not JPA API (I am using EntityManagerFactory, rather than SessionFactory).
So, are you aware of either :
A HibernateInterceptor for the JPA API (EntityManagerInterceptor ?)
A way to adapt a Spring WebRequestInterceptor into a JAX-RSfilter ?
Any other solution ?
Thanks in advance for your help.
Once again, I finally found what I wanted ...
There is actually a JpaInterceptor that does what I want (it seems deprecated though. I do not really understand why).
Here is the resulting configuration that works like a charm, with a bit of auto proxy by name :
<bean id="jpaInterceptor" class="org.springframework.orm.jpa.JpaInterceptor">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="jpaAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
<property name="beanNames">
<list>
<value>myService1</value>
<value>myService2</value>
<value>...</value>
</list>
</property>
<property name="interceptorNames">
<list><value>jpaInterceptor</value></list>
</property>
</bean>
<jaxrs:server id="services" address="/">
<jaxrs:serviceBeans>
<ref bean="myService1" />
<ref bean="myService2" />
<ref bean="..." />
</jaxrs:serviceBeans>
</jaxrs:server>

best approach for setting hibernate/spring project

I have a project with spring and hibernate in GWT,
I am using below applicationcontext.xml,
I was just looking for some best approach of making this file
like all the annotated classes below i.e entity.user, entity.secretQuestion and many more , they all get called when my application runs even if i don't need them , which i guess makes my application quite slow,
so is it possible that only the class which i am calling is getting load in applicationcontext.xml and if yes then would it be a better approach as well ?
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.cricsite.persistence.entity.User</value>
<value>com.cricsite.persistence.entity.SecretQuestion</value>
</list>
</property>
</bean>
<bean id ="ManagerAdmin" class= "com.persistence.MySQLRdbHelper">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
You might be looking for something called "lazy loading"
Please take a look at these threads;
Help needed with Spring/Hibernate Lazy-loading
What is lazy loading in Hibernate?
how does spring allow for lazy-loading?

Spring MVC from 2.5 to 3.0

We are migrating our web application from Spring 2.5 to Spring 3.0.5. Looks like all the Controller classes (the classes in org.springframework.web.servlet.mvc package: BaseCommandController, AbstractCommandController, SimpleFormController, etc) have been deprecated. We used those classes heavily in our application. These classes have been replaced by #Controller annotation instead.
I have a few questions regarding Spring 3.0.x Controller configuration...
We used the following XML to create a Controller bean in Spring 2.5. If <context:component-scan> is used instead to create the Controller beans, then how do I wire-in the dependencies? If possible I would like to avoid Auto-Wiring.
<bean id="photosetViewController" class="com.xyz.web.PhotosetViewController"
p:photosetManager-ref="photosetManager"
p:photoManager-ref="photoManager" />
We have created 3 Interceptors. We use SimpleUrlHandlerMapping to map these Interceptors to different request URLs and also to map URLs to Controllers. How do we achieve the same in Spring 3.0.x?
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
<property name="interceptors">
<list>
<ref bean="httpRedirectInterceptor"/>
<ref bean="loginInterceptor"/>
</list>
</property>
<property name="mappings">
<value>
/abc.html=abcLookupController
/photoupload.html=photoUploadController
</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
<property name="interceptors">
<list>
<ref bean="httpRedirectInterceptor"/>
<ref bean="loginInterceptor"/>
<ref bean="userMenuInterceptor" />
</list>
</property>
<property name="mappings">
<value>
/index.html=homepageController
/success.html=simpleController
</value>
</property>
</bean>
In case of SimpleFormControllers we used different methods like initBinder, referenceData, formBackingObject. We also used command objects and validation classes. How do we achieve the same in Spring 3.0.x?
<bean id="photosetAddController" class="com.xyz.web.PhotosetAddController"
p:photosetManager-ref="photosetManager"
p:photosetTypeManager-ref="photosetTypeManager"
p:stateManager-ref="stateManager" >
<property name="validator">
<bean class="com.xyz.form.PhotosetAddValidator" />
</property>
<property name="formView" value="photosetadd" />
<property name="successView" value="redirect:/photoset" />
</bean>
Any pointers are greatly appreciated.
As skaffman noted - your code will work fine without any modifications. I'll answer your questions in short:
You can use #Resource(name="beanName") for name-based injection. But autowiring is also a good option. Either #Autowired, or #javax.inject.Inject. Note that you can use #Qualifier("name") to differentiate between beans with the same interface/base class. You can even use the javax.inject qualifiers (read about all these in the docs)
interceptor mappings can stay the same
There is #InitBinder, which denotes the initBinder method. I can't tell about the other.

Resources