Dependency Injection in Spring MVC - spring

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;

Related

Read spring yml properties from xml configuration

I am trying to read properties from application.yml in spring-bean.xml like this:
<bean name="#{bean.name}" />
Is it possible ? or I should specify location of my application.yml file?
Yes It's Possible
For YAML Properties
You have to use YamlPropertiesFactoryBean
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
Then define your property in src/main/resource/application.yaml
bean:
name: foo
Now use can use the property in xml to create a bean
<bean name="${bean.name}"
class="net.asifhossain.springmvcxml.web.FooBar"/>
Here's my complete XML config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yaml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
<bean name="${bean.name}" class="net.asifhossain.springmvcxml.web.FooBar"/>
</beans>

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

Internationalize REST Spring-MVC application?

Actually the question asked on SO continue. According to my spring-servlet.xml configuration for internationalization, I can not change the language of request. When I send request including "?language=tr" it returns me messages from en file. Any help would be appreciated.
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- SPRING INTERNALIZATION CONFIGURATION -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="language" />
</mvc:interceptors>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<!-- Declare a view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
my applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- spring frameworka properties dosyasını tanıtma -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/applicationContext.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<aop:aspectj-autoproxy>
<aop:include name="myLogger"/>
</aop:aspectj-autoproxy>
<bean id="myLogger" class="sow.webservices.aop.SowLoggerAOP"/>
<bean id="personService" class="sow.webservices.services.PersonService" />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
For example #Controller and #Service. Make sure to set the correct base-package-->
<context:component-scan base-package="sow.webservices" />
<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
<!-- Loads MongoDB configuraton -->
<import resource="mongo-config.xml"/>
</beans>
Rather make sure that you added the localeChangeInterceptor bean within the interceptors properties of your handleMapping bean as follows:
<bean id="handlerMapping"vclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
Or try to register the LocaleChangeInterceptor as an mvc:interceptor:
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="language" />
</mvc:interceptors>
You need to add the mvc namespace schema (xmlns:mvc="http://www.springframework.org/schema/mvc") in the root declaration of the spring context file so it may look as:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
...
</beans>

AOP on Spring Managed JSF beans

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).

spring #inject not working in Controller

I am using context:component-scan, still the dependencies are not getting injected.
Here is my setup. ConnectionHelper property is not getting injected in the LoginController class.
WEB-INF/web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>vd</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>vd</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
WEB-INF/applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<context:component-scan base-package="com.example" />
</beans>
WEB-INF/vd-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<bean class="com.example.interceptors.SslInterceptor" />
<mvc:interceptor>
<mvc:mapping path="/login" />
<bean class="com.example.interceptors.SslInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
</bean>
</beans>
com.example.controllers.LoginController.java
#Controller
public class LoginController {
#Inject
private ConnectionHelper connectionHelper; //this is null after loading
}
com.example.connection.ConnectionHelper.java
#Component
public class ConnectionHelper {
}
Please tell me what's wrong here...after hours of troubleshooting i can't determine the problem.
Update
I changed my configuration and moved everything to vd-servlet.xml.
Quite miraculously I have found that #Autowired works (Injects Dependencies) with this configuration but #Inject does not.
And <mvc:annotation-driven /> is still required even if you use <context:component-scan />, otherwise #RequestMapping is not detected.
vd-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.example" />
<mvc:interceptors>
<bean class="com.example.interceptors.SslInterceptor" />
<mvc:interceptor>
<mvc:mapping path="/login" />
<bean class="com.example.interceptors.SslInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
</bean>
</beans>
Agree with #Alex, just that the file where you are missing <context:component-scan/> or <context:annotation-config/> is not applicationContext.xml file but vd-servlet.xml file.
Either <context:annnotation-config/> or <context:component-scan/> will register a AutowiredAnnotationBeanPostProcessor responsible for handling #Autowired, #Resource, #Inject annotations
I believe that your applicationContext.xml is missing:
<context:annotation-config>
this registers Springs post processors for annotation based configuration.
You need to add getters and setters to the LoginController. Another option is to make connectionHelper a protected variable. This is how Spring "sees" the property to be injected.
In LoginController:
#Inject
private ConnectionHelper connectionHelper;
public ConnectionHelper getConnectionHelper() {
return connectionHelper;
}
public void setConnectionHelper(ConnectionHelper connectionHelper) {
this.connectionHelper = connectionHelper;
}

Resources