Spring Transaction not working - spring

I'm having a problem with transactions, when execution reaches
taskExecutor.execute();
I get an Exception:
Caused by: org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'
I know this error is self-explanatory, but I can't get it to work properly.
This is how I get the controller and make the call:
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");
BeanFactory beanFactory = context;
FacadeControler f = beanFactory.getBean("facadeController");
f.method(reservation);
This is my FacadeController:
package xxx;
import ...
#Service
public class FacadeControllerImpl implements FacadeController {
static Logger logger = Logger.getLogger(FacadeControllerImpl.class);
#Qualifier("executor")
#Autowired
private TaskExecutor taskExecutor;
#Transactional(propagation=Propagation.REQUIRED)
public Reservation method(Reservation reservationFromEndpoint){
...
taskExecutor.execute();
...
}
Here is the Executor:
#Component("executor")
public class QueueTaskExecutor implements TaskExecutor {
final static Logger logger = LoggerFactory.getLogger(QueueTaskExecutor.class);
#Autowired
protected QueuedTaskHolderDao queuedTaskDao;
#Autowired
protected Serializer serializer;
#Override
#Transactional(propagation=Propagation.MANDATORY)
public void execute(Runnable task) {
logger.debug("Trying to enqueue: {}", task);
AbstractBaseTask abt;
try {
abt = AbstractBaseTask.class.cast(task);
} catch (ClassCastException e) {
logger.error("Only runnables that extends AbstractBaseTask are accepted.");
throw new IllegalArgumentException("Invalid task: " + task);
}
// Serialize the task
QueuedTaskHolder newTask = new QueuedTaskHolder();
byte[] serializedTask = this.serializer.serializeObject(abt);
newTask.setTriggerStamp(abt.getTriggerStamp());
logger.debug("New serialized task takes {} bytes", serializedTask.length);
newTask.setSerializedTask(serializedTask);
// Store it in the db
this.queuedTaskDao.persist(newTask);
// POST: Task has been enqueued
}
}
Here is 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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- Where to look for Spring components -->
<context:annotation-config />
<context:component-scan base-package="com.xxx"/>
<!-- #Configurable with AspectJ -->
<context:spring-configured/>
<!-- A task scheduler that will call #Scheduled methods -->
<!-- <task:scheduler id="myScheduler" pool-size="10"/> -->
<!-- <task:annotation-driven scheduler="myScheduler"/> -->
<!-- DataSource -->
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="myDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bbdd"/>
<property name="username" value="user"/>
<property name="password" value="pass"/>
</bean>
<!-- JPA Entity Manager -->
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="myEntityManagerFactory">
<property name="dataSource" ref="myDataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"></bean>
</property>
<property name="persistenceUnitName" value="comm_layer" />
<property name="jpaPropertyMap">
<map>
<entry key="eclipselink.weaving" value="false"/>
<entry key="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<entry key="eclipselink.logging.level" value="INFO"/>
</map>
</property>
</bean>
<!-- Transaction management -->
<tx:annotation-driven mode="aspectj" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
</property>
<property name="entityManagerFactory" ref="myEntityManagerFactory"/>
</bean>
<bean id="facadeController" class="xxx.FacadeControllerImpl">

Related

Unable to fix "could not obtain transaction-synchronized Session for current thread"

I am new to spring hibernate.
I am using sprin 4.3.8 and Hibernate 5.2.
I tried solutions available on the net and I have all of them in my code, yet I am getting this error please help me resolve this.
error:
Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:133)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:456)
at com.travello.daoImpl.ActivityDAOImpl.getActivity(ActivityDAOImpl.java:43)
at com.travello.model.SpringMain.main(SpringMain.java:17)
Here is the ActivityListDAOImpl:
#Transactional
public class ActivityDAOImpl implements ActivityDAO {
#Autowired
private SessionFactory sessionfactory;
public SessionFactory getSessionfactory() {
return sessionfactory;
}
public void setSessionfactory(SessionFactory sessionfactory) {
this.sessionfactory = sessionfactory;
}
#Override
public ActivityList getActivity(int activity_id) {
Session session = sessionfactory.getCurrentSession();
ActivityList activity = session.get(ActivityList.class, activity_id);
return activity;
}
}
The Main method:
public class SpringMain {
public static void main(String[] args) {
#SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext ("spring.xml");
ActivityDAOImpl dao = (ActivityDAOImpl) context.getBean("activityDao", ActivityDAOImpl.class);
ActivityList activity = dao.getActivity(1);
System.out.println(activity.getActivityName());
System.out.println("Done");
}
}
The spring.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"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="classpath:resources/database.properties" />
<context:annotation-config/>
<context:component-scan base-package="com.travello" />
<bean id="dataSource"
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>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="hibernate.cfg.xml" />
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="activityDao" class="com.travello.daoImpl.ActivityDAOImpl">
<property name="sessionfactory" ref="sessionFactory" />
</bean>
</beans>
I have #Transational annotation applied and the bean for the same in the spring.xml yet there's this error.
Add <tx:annotation-driven/> to your spring.xml. See here and here
I have had a similar error, the easiest solution is to add #Transaction with
#Transactional(propagation = Propagation.REQUIRES_NEW)
You will not get this error after that and would work perfectly.

single jpa transaction manager for multiple entitymanagers

#Repository
#Transactional
#Scope("prototype")
public class EntityManagerImpl implements EntityManagerGenerator {
#PersistenceContext(unitName = "test")
#Qualifier("transactionManager")
private EntityManager entityManager;
#PersistenceContext(unitName = "test1")
#Qualifier("transactionManager1")
private EntityManager entityManager2;
#Override
public EntityManager getEntityManager(String userType) {
if(userType.equals("test")){
return entityManager2;
}else{
return entityManager;
}
}
here how to use according entitymanager wise transaction manager dynamically.below i shared my xml configure file.
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx">
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="showSql" value="false" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="entityManagerFactory2"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="test1.ds" />
<property name="persistenceUnitName" value="test1" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapterFC" />
<property name="persistenceXmlLocation" value="classpath:jpa-persistence.xml" />
</bean>
<bean id="jpaVendorAdapterFC"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="showSql" value="false" />
</bean>
<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory2" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" />
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative
to JTA) -->
<!-- Activates various annotations to be detected in bean classes: Spring's
#Required and #Autowired, as well as JSR 250's #PostConstruct, #PreDestroy
and #Resource (if available) and JPA's #PersistenceContext and #PersistenceUnit
(if available). -->
<context:component-scan base-package="com.wmi.test" annotation-config="true"
scope-resolver="org.springframework.context.annotation.Jsr330ScopeMetadataResolver"/>
<!-- Instruct Spring to perform declarative transaction management automatically
on annotated classes. -->
<tx:annotation-driven />
<cache:annotation-driven />
so how can i use single transaction manager for multiple data source according to my configuration
and here we are used for transactional manager as #Transactional annotation

Spring+JPA+Hibernate multiple databases

I am trying to connect to two databases with Spring and JPA. I am already connected to one database(sql server 2012), and I need to connect to another without changing too much stuff. I have app-context-dao.xml file which holds my hibernate and JPA configuration:
<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:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:jee="http://www.springframework.org/schema/jee"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<aop:aspectj-autoproxy proxy-target-class="false" />
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/SQLCMS" expected-type="javax.sql.DataSource" />
<jee:jndi-lookup id="dataSourceOracle" jndi-name="jdbc/razmjenskaDBSQL" expected-type="javax.sql.DataSource" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="hr.akd.cms.*" />
<property name="persistenceUnitName" value="caPersistenceUnit" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServer2012Dialect" />
<property name="showSql" value="false" />
</bean>
</property>
<!-- ..................
Initialize additional Hibernate JPA related properties
if required
.................. -->
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="entityManagerFactoryOracle"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceOracle" />
<property name="persistenceUnitName" value="userPersistenceUnit" />
<property name="packagesToScan" value="hr.akd.cms.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServer2012Dialect" />
<property name="showSql" value="false" />
</bean>
</property>
<!-- ..................
Initialize additional Hibernate JPA related properties
if required
.................. -->
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- .................. Transaction manager setup .................. -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManagerOracle" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryOracle" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- .................. Spring data - declare base packages for scanning,
all classes extending from data repositories will be available for autowire
.................. -->
<jpa:repositories base-package="hr.akd.cms.repository"
factory-class="hr.akd.cms.repository.impl.CMSCustomRepositoryFactoryBean" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
<property name="defaultPersistenceUnitName" value="caPersistenceUnit"/>
</bean>
I added entityManagerFactoryOracle, dataSourceOracle jndi transactionManagerOracle for my second database. When I start my app I got this
Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2: entityManagerFactory,entityManagerFactoryOracle
My factory bean looks like this:
public class CMSCustomRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
extends JpaRepositoryFactoryBean<R, T, I> {
#SuppressWarnings("rawtypes")
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new CMSSearchFactoryFactory(entityManager);
}
private static class CMSSearchFactoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
private EntityManager entityManager;
public CMSSearchFactoryFactory(EntityManager entityManager) {
super(entityManager);
this.entityManager = entityManager;
}
#SuppressWarnings("unchecked")
protected Object getTargetRepository(RepositoryMetadata metadata) {
return new CMSCustomRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager);
}
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
// The RepositoryMetadata can be safely ignored, it is used by the
// JpaRepositoryFactory
// to check for QueryDslJpaRepository's which is out of scope.
return CMSCustomRepository.class;
}
}
You must make one of the dataSource and entityManagerFactory primary . primary = "true"
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
primary = "true">

#PostConstruct not called (Primefaces & Spring & Hibernate)

I'm using Hibernate, Spring and Primefaces (and Maven) and I'm trying to run
#PostConstruct
init() {}
to initialize a location list inside a bean. But the init() method is never called. The projects structure is:
com.xxx
com.xxx.hibernate.dao
com.xxx.hibernate.dao.impl
com.xxx.hibernate.data
com.xxx.prime.faces.bean
com.xxx.spring.service
com.xxx.spring.service.impl
application context:
<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:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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-3.2.xsd">
<context:property-placeholder location="classpath*:META-INF/*.properties"/>
<!-- Scan for all of Spring components such as Spring Service -->
<context:component-scan base-package="com.xxx"></context:component-scan>
<!-- Create Data Source bean -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://host:3306/db" />
<property name="username" value="user" />
<property name="password" value="password" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property value="persistenceUnit" name="persistenceUnitName" />
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Detect #Transactional Annotation -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
The LocationView Bean:
#ManagedBean
#ViewScoped
public class LocationView implements Serializable{
private static final long serialVersionUID = 1L;
#ManagedProperty("#{locationService}")
private LocationService locationService;
private Location location = new Location();
private List<Location> locations = new ArrayList<Location>();
#PostConstruct
public void init() {
this.locations = locationService.getAllLocations();
}
I run this on Glassfish4 in debug mode and the init() Method is never called. but I don't no why. It should be scanned for spring annotations, but I don't know how I could be sure about that.
Also I'm not sure what
<context:property-placeholder location="classpath*:META-INF/*.properties"/>
does.
Any ideas what I could check?

Can't use #Autowired in desktop application

i am trying to use spring in desktop application, but i am facing a problem with autowiring in action methods of my JPanel.
i am loading the applicationContext in my main method as follows:
public static void main(String[] args) {
new ClassPathXmlApplicationContext(
"classpath:/META-INF/spring/applicationContext.xml");
MainFrame frame = new MainFrame();
Signup signup = new Signup();
frame.add(signup);
frame.setResizable(false);
frame.setTitle("Please input your data");
frame.setBounds(100, 100, 450, 180);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
and i can see that it's loaded with no problems.
my panel code:
#Component
public class Signup extends JPanel {
#Autowired
private UserDao userDao;
public Signup() {
JButton btn_submit = new JButton("Submit");
btn_submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
registerUser();
}
});
}
private void registerUser() {
User newUser = new User();
newUser.setName(username);
newUser.setSalary(salary);
userDao.addUser(newUser);
}
}
the context:component-scan is configured properly, and i am using context:annotation-config too but i always gets NullPointerException in userDao.addUser(newUser);
which means that the Dependency Injection is not working as it should.
please advise how to fix this issue.
UPDATE: 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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="${project.groupId}" />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:messages/application.properties</value>
</list>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="${project.groupId}.domain" />
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.DerbyDialect
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.hbm2ddl.auto=validate
</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="url" value="jdbc:derby:test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans>
If you are going to configure Spring in a desktop environment, then you must be the one to work with the ApplicationContext.
For example, if you want to get a hold of your Signup class that you have posted here, you would do something like this in your main method:
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"classpath:/META-INF/spring/applicationContext.xml");
Signup signup = appContext.getBean(Signup.class);
//use signup here...
}
Using new Signup() to get a new instance of the Signup class, which won't work the way you want, because you want it to be a Spring managed class! (Actually, you could get it to work that way, but that is beyond my answer here)

Resources