Issue loading application context running Junit4 and Spring - spring-mvc

I am playing with writing integration tests for our project. This is the first time I am writing tests so please pardon me if this is a simple question.
I have different modules like core, site, admin etc.,
I want to keep our unit tests separate from our integration tests. So in the core of the project, I created a folder src/it/java and a package in it - com.test.integration. This package has the test that I am trying to run.
The test application context and required test properties files are in src/it/java directly. I added src/it/java to my build path. In my application context I have the following
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="url" value="jdbc:mysql://${mysql.host}/${mysql.db_blc}?autoReconnect=true&useUnicode=true&characterEncoding=UTF8"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="${mysql.user}" />
<property name="password" value="${mysql.pass}" />
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="false"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
Now in my sample test
#RunWith(BlockJUnit4ClassRunner.class)
#ContextConfiguration("/applicationContext-test.xml")
public class SampleTest {
#Resource(name="jdbcTemplate")
private NamedParameterJdbcTemplate jdbcTemplate;
}
The jdbcTemplate bean is null and I can't seem to figure out why.
Any input is appreciated. We are using Spring 3.0.5 and JUnit4
Thanks
Mehul

try using SpringJunit4ClassRunner
I would suggest changing the context configuration to this
#ContextConfiguration("classpath:/applicationContext-test.xml")

Related

Will my application work if i don't give handlerMapping in configuration file?

This is my spring-servlet.xml file. I am new to spring MVC. Do we need to define bean for HelloWorld.Controller. If i don't define will it work?
<bean id="viewResolver" class=" org.springframework.web.servlet.view. InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean name="/welcome.htm" class="com.vaannila.HelloWorldController" >
<property name="message" value="Hello World!" />
</bean>
</beans>
If i don't give this bean definition
<bean name="/welcome.htm" class="com.vaannila.HelloWorldController" >
<property name="message" value="Hello World!" />
</bean>
MyApplication should work or not? I am new to spring MVC. In few tutorial this code is there and in few its not there. Please explain.
Yes, you will need to define the bean (way to create instance of class) for all the Controller/Service/Dao/Components class you want to use and set relevant properties.
I would recommend you to use Annotation based configuration (spring boot) as it eases all this process. You can find lot of tutorials on getting stated with spring boot. Here is one such good tutorial https://www.mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/

Alfresco: Why is there no Scripts Extension folder, when there is a Web Script Extension folder?

When I learnt about web scripts, the tutorials said to leave the Web Scripts folder alone and instead to add my web script to the Web Scripts Extensions folder. My understanding is that this is so that I do not pollute Alfresco's out-of-the-box web scripts, and upgrading will be easier.
Why is there no equivalent extensions folder for the Scripts folder?
Or is my understanding of the reason for the Web Scripts vs Web Scripts Extensions folders wrong?
Extension folders provide a way to override default behavior. You can put your code there if you like, but doing so makes it more difficult to override it. I will use the repository as an example. Similiar logic applies to share.
Have a look at alfresco/WEB-INF/classes/alfresco/web-scripts-application-context.xml. It reads:
<bean name="webscripts.store.repo.extension" parent="webscripts.repostore">
<property name="store"><value>workspace://SpacesStore</value></property>
<property name="path"><value>/${spaces.company_home.childname}/${spaces.dictionary.childname}/cm:extensionwebscripts</value></property>
</bean>
<bean name="webscripts.store.repo" parent="webscripts.repostore">
<property name="mustExist"><value>true</value></property>
<property name="store"><value>workspace://SpacesStore</value></property>
<property name="path"><value>/${spaces.company_home.childname}/${spaces.dictionary.childname}/cm:webscripts</value></property>
</bean>
<bean id="webscripts.repoclasspathstore" class="org.alfresco.repo.web.scripts.RepoClassPathStore" abstract="true" />
<bean id="webscripts.store" parent="webscripts.repoclasspathstore">
<property name="mustExist"><value>false</value></property>
<property name="classPath"><value>webscripts</value></property>
</bean>
<bean id="webscripts.store.alfresco" parent="webscripts.repoclasspathstore">
<property name="mustExist"><value>false</value></property>
<property name="classPath"><value>alfresco/webscripts</value></property>
</bean>
<bean id="webscripts.store.client" parent="webscripts.repoclasspathstore">
<property name="mustExist"><value>true</value></property>
<property name="classPath"><value>alfresco/templates/webscripts</value></property>
</bean>
<bean id="webscripts.store.client.extension" parent="webscripts.classpathstore">
<property name="classPath"><value>alfresco/extension/templates/webscripts</value></property>
</bean>
<bean id="webscripts.searchpath" class="org.springframework.extensions.webscripts.SearchPath">
<property name="searchPath">
<list>
<ref bean="webscripts.store.repo.extension" />
<ref bean="webscripts.store.repo" />
<ref bean="webscripts.store.client.extension" />
<ref bean="webscripts.store.client" />
<ref bean="webscripts.store.alfresco" />
<ref bean="webscripts.store" />
</list>
</property>
</bean>
The searchPath property of webscripts.searchpath shows the search order. Search terminates as soon as it has a match. As you can see, it is no problem to put custom code in non extension locations as long as their path is unique.
I personally only put override webscripts in extension locations.
You can create a folder under Scripts called Extension and put all your script in that. Or just create different folders under Scripts for each function or project, it doesn't matter. They get picked up by Alfresco.

How to Load lables via Properties file in FreeMarker

I am using Spring MVC + FreeMarker integration. As I am new to FreeMarker i am not able to find out a way to configure FreeMarker labels from properties file.
Kindly help me out on this.
Thanks.
You can use, 'ResourceBundleMessageSource', message source for this.
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
<property name="basename" value="classpath:messages/messages" />
<property name="defaultEncoding" value="UTF-8"/>
/>
Define the locale resolver,
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
Define localeChangeInterceptor which detects the language parameter from the user session and call locale resolve. And register the interceptor in handler Mappings.
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
Property files need to be defined like,
messages_en.properties
messages_ar.properties
inside the messages folder path.
'ReloadableResourceBundleMessageSource' can be used if the language property files frequently changes. That means you don't have to restart the application in each language file change.
You need to import spring macro,
<#import "/spring.ftl" as spring/>
and
messages can be accessed inside a .ftl as follows.
<#spring.message "customMessageKey"/>

Configuring FreeMarker In Spring Application

I am new to freemarker,but i want to fetch the data from Spring Application to my view which has extension .ftl.I am using ${message} but it displays as usual.It should display the data which is availble in message from Spring Application.
I am configured dispatcherServlet as below
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".ftl"/>
</bean>
You don't use InternalResourceViewResolver with Freemarker, you use FreemarkerViewResolver. Replace that, you should be fine.
Also, I suggest not putting your freemarker templates in /WEB-INF/jsp. They're not JSPs, and should never be treated as such.

Best place to put my .poperties files for use with springs PropertyPlaceholderConfigurer?

i have a properties folder and inside of ti contains my jdbc.properties file.
the problem is that i am unable to load it succesfuly as it always complains that it cannot locate the file.
i have the file currently sitting in the roo directopry fo WEB-INF. when i build and compile my spring mvc it does throw any exceptions but as soon as i load the beans from my code using new ClassPathXmlApplicationContext it fails and says it cannot locate my properties file??
here is how my beans look like:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"> <list> <value>/properties/jdbc.properties</value>
</list> </property> </bean>
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" /> <property
name="url" value="${database.url}" /> <property name="username" value="${database.user}"
/> <property name="password" value="${database.password}" /> </bean>
here is a pic of my project structure at present:
You can put the properties files in root of classpath. And use like below.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"> <list> <value>classpath:jdbc.properties</value>
</list> </property> </bean>

Resources