How to Inherit Single Property from parent bean to child bean - spring

Here?In my Spring code all properties are inherited but i need only address property in to my child class
<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.0.xsd">
<bean id="baseEmployeeBean" class="com.java.beans.BaseEmployee">
<property name="company" value="Java2novice" />
<property name="address" value="mumbai" />
</bean>
<bean id="myEmployeeBean" class="com.java.beans.Employee" parent="baseEmployeeBean">
<property name="empId" value="1016" />
<property name="name" value="Nataraja Gootooru" />
</bean>
</beans>

Related

Image not displayed in spring-boot version 1.5.3

I have been trying for more than an hour to display an image in my SpringBoot version 1.5.3 application.
I'm trying to display an image in my html page, which is the path:
And have used the image as:
<img src="images/DataServicesAdminAppLogo.png" width="40" height="40" class="d-inline-block align-top" alt="" hspace="10"/>
in my html page. But in vain. The image is not getting displayed.
Can someone please help?
UPDATE:
Network Inspector:
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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- <context:component-scan base-package="com.csaa.mdm"></context:component-scan> -->
<util:properties id="configLocProperties"
location="file:${app.config.home}/config/config.properties" />
<context:property-placeholder
location="file:${app.config.home}/config/config.properties" />
<bean id="configProperties"
class="com.csaa.mdm.adminApp.util.ApplicationPropertyConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="location">
<value>file:${app.config.home}/config/config.properties
</value>
</property>
</bean>
<bean id="applicationPropertyUtil" class="com.csaa.mdm.adminApp.util.ApplicationPropertyUtil">
<property name="propertyConfigurer" ref="configProperties" />
</bean>
<bean id="metaCryptoUtil" class="com.csaa.mdm.adminApp.util.CryptoUtil">
<property name="userId" value="${oracle.meta.username}"></property>
<property name="password" value="${oracle.meta.password}"></property>
<property name="key" value="123456789012345678901234"></property>
</bean>
<!-- Create datasource and give connection properties -->
<bean id="metaDatasource" primary="true"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${oracle.db.driver}" />
<property name="url" value="${oracle.db.dev.connection}" />
<property name="username" value="#{metaCryptoUtil.userId}" />
<property name="password" value="#{metaCryptoUtil.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="metaDatasource" />
</bean>
<!-- Create the dao object and pass the datasource to it -->
<bean id="adminDao" class="com.csaa.mdm.adminApp.dao.AdminDao">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean id="adminUtil" class="com.csaa.mdm.adminApp.util.AdminUtil">
<property name="configProperties" ref="configLocProperties"></property>
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate" />
</beans>

Update one column does not works

I want to update one column of my database. The column name is status and the type is Enum('waiting' , 'accepted' , 'rejected')
I want to update this column when I click on link:
Accept
Reject
Vacation.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.terafast.tem.model">
<class name="Vacation" table="vacations" dynamic-update="true">
<id name="id" column="REQUEST_ID">
<generator class="native" />
</id>
<property name="user" column="USER_ID" />
<property name="reason" column="REASON" />
<property name="duration" column="HOW_LONG" />
<property name="status" column="STATUS" />
<property name="start" type="date" column="START_DATE" />
<property name="created" type="date" column="CREATED_AT" />
</class>
</hibernate-mapping>
Controller
#RequestMapping(value = "/requests/action")
public String statuHandler(HttpServletRequest request, Model model) {
int id = Integer.parseInt(request.getParameter("id"));
String status = request.getParameter("a");
vacationDao.actionStatus(id, status);
return "redirect:/admin/requests";
I can successfully get these two GET values. (id , a). My VacationDAOImpl:
#Override
#Transactional
public void actionStatus(int id, String action) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("from Vacation where id = :reqid ");
q.setParameter("reqid", id);
Vacation vacation = (Vacation) q.list().get(0);
vacation.setStatus(action);
session.update(vacation);
}
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.terafast.tem" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="employeeDao" class="com.terafast.tem.dao.EmployeeDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
<bean id="vacationDoa" class="com.terafast.tem.dao.VacationDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
</beans>
I have seen this approach in this tutorial.
When I click on links, everything looks good. But the column value does not change. Could someone explain this problem?

Does lazy-init actually work as advertised?

According to the documentation http://docs.spring.io/spring-framework/docs/3.2.2.RELEASE/spring-framework-reference/html/beans.html#beans-factory-lazy-init the below should not be instatiating MyDatabase yet I can clearly see this is the case in the afterPropertiesSet method of the HibernateDatabase class when debugging. If I remove MyDatabaseEntityManagerFactory then this does not occur. However this should not matter given that MyDatabaseEntityManagerFactory is also lazy - it should not cause MyDatabase to initialise.
Either I'm misunderstanding the part of the docs that say that a lazy bean referencing another lazy bean shouldn't cause it to initialise or there is a fundamental problem in Spring here. Can anyone shed some light on this or possibly suggest an alternative to what I want to achieve?
Thanks.
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-lazy-init="false">
<bean id="HibernateDatabase" class="mypackage.database.HibernateDatabase"
abstract="true" />
<bean id="MyDatabase" parent="HibernateDatabase" lazy-init="true">
<property name="driver" value="com.sybase.jdbc3.jdbc.SybDriver" />
<property name="dialect" value="org.hibernate.dialect.SybaseDialect" />
<property name="url" value="${My.dburl}" />
<property name="user" value="${My.dbuser}" />
<property name="password" value="${My.dbpassword}" />
</bean>
<bean id="EntityManagerFactory" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
abstract="true">
<property name="targetMethod" value="getEntityManagerFactory" />
</bean>
<bean id="MyDatabaseEntityManagerFactory" parent="EntityManagerFactory" lazy-init="true">
<property name="targetObject" ref="MyDatabase" />
</bean>
</beans>

Spring 3 with Thymeleaf config issue

Hello I have the following jars in my build path -
spring-beans-3.1.2.RELEASE.jar
spring-context-3.1.2.RELEASE.jar
spring-core-3.1.2.RELEASE.jar
spring-expression-3.1.2.RELEASE.jar
spring-web-3.1.2.RELEASE.jar
spring-webmvc-3.1.2.RELEASE.jar
thymeleaf-spring3-2.0.13.jar
and my servlet
<?xml version="1.0" encoding="UTF-8"?><br>
<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"
xsi:schemaLocation="
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="web.controller" />
<!-- Enabling Spring MVC configuration through annotations -->
<mvc:annotation-driven />
<!-- Mapping Static Resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html" />
</bean>
</beans>
The error I get on launching is -
Cannot find class [org.thymeleaf.templateresolver.ServletContextTemplateResolver] for bean with name 'templateResolver' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.thymeleaf.templateresolver.ServletContextTemplateResolver
Am I missing any other library here? Any help is much appreciated.
You are missing the actual Thymeleaf jar. You included the Spring jar that provides the integration but you missed the actual implementation of it.
Download the jar from here
thymeleaf download site

spring multiple property file

I have a spring project. There are two property files. one property file is config in dbConfig.xml and I cannot change it. I have my own appConfig.xml
I have the following in it
<util:properties id="configProps" location="classpath:spring/config.properties" />
<bean id="createDummyDataTask" class="com.merc.spring.CreateDummyData" scope="step">
<property name="srcFolder" value="${configProps.srcDir}"/>
</bean>
using either srcDir or configProps.srcDir does not seem to work.
${} works only for property files loaded by the context:propertyplaceholder. If you are using spring 3.0 you can use #{} which is processed as a Spel (spring expression language). The following should work.
<util:properties id="configProps" location="classpath:spring/config.properties" />
<bean id="createDummyDataTask" class="com.merc.spring.CreateDummyData" scope="step">
<property name="srcFolder" value="#{configProps.srcDir}"/>
</bean>
Here how we resolved it
Context.xml
<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-2.0.xsd">
<import resource="Settings.xml"/>
<import resource="Database.xml"/>
</beans>
Settings.xml
In the list many property file could be added
<?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-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>db.properties</value>
</list>
</property>
</bean>
</beans>
Sample bean for processing db.properties file
Database.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- the transaction manager -->
<bean id="dstxnManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="appDataSource"/>
</bean>
<!-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) -->
<bean id="appDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- DB connection factory -->
<bean id="storageDaoFactory" class="com.dao.StorageDAOFactory">
<constructor-arg><ref bean="dstxnManager"/></constructor-arg>
</bean>
</beans>

Resources