AOP on Spring Managed JSF beans - spring

I am trying to apply AOP on Spring managed JSF Beans, but for some reason as soon as I apply AOP JSF is throwing MethodNotFoundException.
here is my code :
Web.xml
<application>
<default-render-kit-id>org.apache.myfaces.trinidad.core</default-render-kit-id>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<aop:aspectj-autoproxy/>
<bean id="loginAuditAspect" class="com.test.mobile.service.LoginAuditManagementAspect">
<constructor-arg index="0">
<list>
<bean class="com.test.mobile.service.LoginAuditableResourceResolver" />
<bean class="com.test.mobile.service.LoggedInAuditableResourceResolver" />
<bean class="com.test.mobile.service.NavigationAuditableResourceResolver" />
</list>
</constructor-arg>
</bean>
<bean id="loginService" class="com.test.mobile.service.MLoginServiceImpl" />
<bean id="memberService" class="com.test.mobile.service.MMemberServiceImpl"
scope="session">
<property name="thpContext" ref="thpContext"></property>
</bean>
<bean id="mMemberProfileBean" class="com.test.mobile.service.MMemberProfileBean"
scope="session">
<property name="memberService" ref="memberService"></property>
</bean>
<bean id="testBean" class="com.test.mobile.service.TestBean" scope="session">
</bean>
</beans>
Backing Bean:
public class TestBean extends BaseBackingBean {
private static final long serialVersionUID = 1L;
#Auditable(resourceName="LoggedIn",
resourceResolverClass=com.test.mobile.service.LoggedInAuditableResourceResolver.class)
public String getXxx() {
return null;
}
}
Can someone help me in applying AOP logic on spring managed JSF beans

You should post at least some info about the stack trace and the aspect. But if you are losing methods just only applying the aspetct try with:
<aop:aspectj-autoproxy proxy-target-class="true"/>
That uses CGLIB to proxy classes instead jdk proxies (that only proxy interfaces).

Related

Spring - Repository from other Jar is null. How to initialize it?

I have a project that does data access in one project and to use it in another project as spring jar. I created jar using gradle maven-publish plugin. I initialized this Jar in my other project test case like this
#ContextConfiguration(locations = {"classpath*:spring-config.xml"})
#Transactional
public class TenantProvisioningManagerTest extends AbstractTestNGSpringContextTests {}
Class where Repository is being used. I used like this
public class TenantProvisioningManager {
private static final Logger logger = LogManager.getLogger(TenantProvisioningManager.class);
#Autowired
TProductRepository tProductRepository;
}
Repository is null here. How to initialize repository from other jar in this seperate project?
spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">
<context:property-placeholder
location="file://#{systemEnvironment['GRADLE_USER_HOME']}/gradle.properties"/>
<!-- the base package for spring data jpa repository interfaces -->
<jpa:repositories base-package="com.asklytics.dao.repos" />
<!-- Enable the component scan (auto wiring etc) for the following package -->
<context:component-scan base-package="com.asklytics" />
<!-- Make sure the following is specified to enable transaction -->
<tx:annotation-driven />
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- This defines the entity manager factory with some custom properties -->
<bean id='entityManagerFactory' class='org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean'>
<property name="persistenceUnitName" value="persitanceUnit"/>
<property name='dataSource' ref='dataSource' />
</bean>
<bean id='dataSource' class='org.springframework.jdbc.datasource.DriverManagerDataSource'>
<property name='driverClassName' value='com.mysql.jdbc.Driver' />
<property name='url' value="${awsCamelDbUrl}" />
<property name='username' value="${awsCamelDbUsername}" />
<property name='password' value="${awsCamelDbPassword}" />
</bean>
spring-config.xml and TProductRepository are in other project while TenantProvisioningManager is in seperate project.
Make sure your TenantProvisioningManager is also managed by spring. Annotate is as #Component

Why does adding #Transactional causes exception org.springframework.beans.factory.BeanCreationException: Error creating bean with name?

I am learning Spring with Hibernate and I am playing a bit with my configuration to fully understand how it works. An answer to this will be quite helpful to understand what is going on. I have quite a perplexity about adding #Transactional to my #Repository object. When I add it I get the error:
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'soccerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.edu.spring.soccer.service.AccountService com.edu.spring.soccer.controller.SoccerController.accountService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.edu.spring.soccer.dao.AccountDaoImpl com.edu.spring.soccer.service.AccountService.accountDaoImpl; nested exception is java.lang.IllegalArgumentException: Can not set com.edu.spring.soccer.dao.AccountDaoImpl field com.edu.spring.soccer.service.AccountService.accountDaoImpl to com.sun.proxy.$Proxy63
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
However when I delete the #Transactional everything works properly. I have read in several places that #Repository should be followed by #Transactional.
Following you see my configuration. This is the #Repository object with #Transactional. Note that extends an AbstractDao class that contains SessionFactory object.
#Repository
#Transactional //It causes the exception, without it, it works.
public class AccountDaoImpl extends AbstractDao implements AccountDao {
public void saveAccount(Account account, String password) {
//Both methods come from AbstractDao
persist(account);
insertPassword(account, password);
}
}
However when I delete the #Transactional everything works properly. I have read in several places that #Repository should be followed by #Transactional.
Following you see my configuration:
beans-data.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:property-placeholder location="/WEB-INF/spring/environment.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${dataSource.driverClassName}"
p:url="${dataSource.url}" p:username="${dataSource.username}"
p:password="${dataSource.password}" />
<!-- Taken from http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annotations/ -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.edu.spring.soccer.domain</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql:false}</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
<context:component-scan base-package="com.edu.spring.soccer.dao" />
</beans>
dispatcher-servlet.xml
I am doing the component-scan to all my packages and adding annotation-driven.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<tx:annotation-driven />
<context:component-scan base-package="com.edu.spring.soccer.controller" />
<context:component-scan base-package="com.edu.spring.soccer.dao" />
<context:component-scan base-package="com.edu.spring.soccer.domain" />
<context:component-scan base-package="com.edu.spring.soccer.service" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="classpath:/messages" />
</beans>
Is it maybe related to having the component-scan and <tx:annotation-driven /> in the dispatcher-servlet.xml and then adding again #Transactional?.Maybe this causes some kind of redundancy.
The exception stack says it all :
nested exception is java.lang.IllegalArgumentException: Can not set com.edu.spring.soccer.dao.AccountDaoImpl field com.edu.spring.soccer.service.AccountService.accountDaoImpl to com.sun.proxy.$Proxy63
you are trying here to autowire AccountDaoImpl in your AccountService
But
AccountDaoImpl has that #Transactionnal annotation, so Spring has "proxied" it... You should autowire not the implementation but the interface, that is to say, in your AccountService change:
private AccountDaoImpl accountDaoImpl;
to
private AccountDao accountDao;
I believe it is not because of tx:annotation-driven & #Transactional. Whether in Hibernate or not, I m quite sure #Transactional have to be used at Service layer not #Repository Dao layer. What if you need to handle a transaction that spans across multiple Daos? Dao should be just cohesive components and transaction logic should reside in Service layer.
Pls see
http://www.byteslounge.com/tutorials/spring-with-hibernate-persistence-and-transactions-example
But I still see posts where #Transactional is used in Dao layer, and I disagree with it. But if u insist to use #Transactional and #Repository, maybe try put #Transactional on Dao method(s) saveAccount?

Spring MVC can't access #Autowired fields from #MessageMapping annotated methods

I've been setting up my Spring 4 MVC application to work with STOMP over WebSocket and so far i've succeeded, my servlet can handle and dispatch STOMP messages without problems.
However, i've encountered an annoying problem when handling these messages from #MessageMapping annotated methods inside my controllers: I can't access any of the #Autowired controller's fields from inside these methods (they all are null pointers), but i can access these fields on the same controller from #RequestMapping annotated methods without any problem.
My Dispatcher Servlet Config:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">
<mvc:annotation-driven/>
<mvc:resources location="assets" mapping="/assets/**"/>
<mvc:resources location="assets/img/favicon.ico" mapping="/favicon.ico" />
<context:component-scan base-package="com.company.web.controller"/>
<security:global-method-security pre-post-annotations="enabled" />
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManager">
<constructor-arg>
<bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
<constructor-arg>
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
</property>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language"/>
<bean class="com.hxplus.web.interceptor.aCustomAwesomeInterceptor"/>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="es"/>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2"/>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"/>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- setting maximum upload size -->
<property name="maxUploadSize" value="${upload.limit}" />
</bean>
<context:property-placeholder location="classpath*:upload_config.properties"/>
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/hello/{recipient}">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic" />
</websocket:message-broker>
</beans>
My Controller:
#Controller
public class TheController {
private static final Logger _logger = LoggerFactory.getLogger(TheController.class);
#Autowired private TheService theService;
#Autowired private SimpMessagingTemplate simpMessagingTemplate;
#PreAuthorize("hasRole('GOD')")
#RequestMapping(value = "/something/{id}", method = RequestMethod.GET)
public String show(Model model, #PathVariable("id") Long id) {
//HERE I CAN ACCESS BOTH "theService" AND
//"simpMessagingTemplate" WITHOUT PROBLEMS
}
#MessageMapping("/hello/{recipient}")
private VOID testing(StompEvent event, #DestinationVariable String recipient){
//HERE BOTH "theService" AND "simpMessagingTemplate" ARE NULL
}
}
I found my error and it had nothing to do with Spring Messaging or configuration, it was a pretty dumb error actually so i apologize:
My #MessageMapping annotated method was private and it should have been public.

Spring #Autowired is not working for #WebService annotated class

Getting null pointer while trying to autowire.
Creating a Web application and using following webservices:
WebServiceEndpoint.java
#WebService
#Component
public class ChannelMapWebServiceEndpoint {
#Autowired
ChannelMapWebService webservice;
public ChannelMapInfo4[] getMaps() throws RemoteException {
return this.webservice.getMaps();
}
}
ChannelMapsebserviceImpl.java
#Service
public class ChannelMapWebServiceImpl implements ChannelMapWebService {
public ChannelMapInfo4[] getMaps() throws RemoteException {
System.out.println("hi");
}
}
application context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="ccad" />
<context:component-scan base-package="channelmapwebservice" />
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>/WEB-INF/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
I am getting the autowired object webservice as null while trying to connect through SoapUI.
The ChannelMapWebServiceEndpoint object serving your request class is not instantiated (and managed) by Spring, which is why Spring can't autowire any dependencies.
See the accepted answer for this question:
How to make an #WebService spring aware

Dependency Injection in Spring MVC

I would like to initialize my MessageSource field with Dependency Injection in Spring. This is what have so far:
package com.ucmas.cms.view;
#Component
public class PdfRevenueReportView extends AbstractPdfView {
...
#Autowired
private MessageSource messageSource;
...
}
mvc-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="sec://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<context:component-scan base-package="com.ucmas.cms.controller,com.ucmas.cms.view" />
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
...
<beans:bean class="org.springframework.web.servlet.view.XmlViewResolver">
<beans:property name="location" value="/WEB-INF/spring-pdf-views.xml" />
<beans:property name="order" value="0" />
</beans:bean>
</beans:beans>
I have defined my messageSource in root-context.xml
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
My controller classes work fine, however I am unable to inject the messageSource field in the PdfRevenueReportView class. What should I do to make the DI works ?
UPDATED
I define the view in xml as follow
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="PdfRevenueSummary" class="com.ucmas.cms.view.PdfRevenueReportView" />
</beans>
Perhaps this is why the messageSource is always null ?
I got it working by updating my spring-pdf-views.xml into
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="PdfRevenueSummary" class="com.ucmas.cms.view.PdfRevenueReportView">
<property name="messageSource" ref="messageSource"/>
</bean>
</beans>
However this requires me to generate setter and getter for messageSource field and removing the #Autowired annotation.
In your dispatcher servlets configuration you need to create a bean for the message source.
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="directory/with/messagesource"/>
</bean>
Also ensure that the messages directory is on the classpath.
You are annotating messageSource with Autowired. This might not work when your bean messageSource whilst is an interface of MessageResource, is really the implementation ResourceBundleMessageSource.
I think your DI would have worked if you used #Resource instead as it is name based:
#Resource
private MessageSource messageSource;

Resources