I use Spring OXM as well as Struts 1 but without using integrating Struts with Spring IOC. This is because the application is an old one and I'm just adding a module that involves the XML binding and I have no intention to change the architecture of the application.
I have an action class calls ClasspathXmlApplicationContext for bean injection for the OXM.
Here is my spring 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:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd">
<bean id="xmlMapper" class="com.st.mas.wmr.utils.xml.stifbinconv.XmlMapper">
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
</bean>
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.st.mas.wmr.utils.xml.jaxb.stifbinconv"/>
<property name="validating" value="true"/>
</bean>
</beans>
The action class:
public class StifBinConversionAction extends AnyDispatchAction {
private IProcessStifOliBinConversion svc;
public StifBinConversionAction() {
super();
svc = new ProcessStifOliBinConversion();
}
The service class:
public class ProcessStifOliBinConversion
implements
IProcessStifOliBinConversion {
private BasicDataSource ds;
private IAtomStifOliBinConversion dao;
private ApplicationContext ctx;
private XmlMapper xmlMapper;
public ProcessStifOliBinConversion() {
super();
ds = new BasicDataSource();
//TODO consts
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUrl("jdbc:oracle:thin:#sglx482:1521:wmr");
ds.setUsername("wmr_online");
ds.setPassword("wmr_online");
dao = new AtomStifOliBinConversion(ds);
ctx = new ClassPathXmlApplicationContext("com/st/mas/wmr/utils/xml/stifbinconv/oxm-context.xml");
xmlMapper = ctx.getBean(XmlMapper.class);
}
The web application gives HTTP 500 WITHOUT any error message or stack trace. However, if I change the config location of the ClasspathXmlApplicationContext to an invalid one, Spring throws an exception.
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [classes/com/st/mas/wmr/utils/xml/stifbinconv/oxm-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [classes/com/st/mas/wmr/utils/xml/stifbinconv/oxm-context.xml] cannot be opened because it does not exist
Seems that the problem is within the Spring injection.
It's irritating when there's an error but there's no error message. It makes you stuck for days.
Thanks
Will
It's irritating when there's an error
but there's no error message. It makes
you stuck for days.
??? There is an error message: your XML can't be found at this location:
classes/com/st/mas/wmr/utils/xml/stifbinconv/oxm-context.xml
I'd say you are passing bad parameters to the ApplicationContext. Take a look at the example in 4.7.1.1 Constructing ClassPathXmlApplicationContext instances - shortcuts
Consider a directory layout that
looks like this:
com/
foo/
services.xml
daos.xml
MessengerService.class
A ClassPathXmlApplicationContext
instance composed of the beans defined
in the 'services.xml' and 'daos.xml'
could be instantiated like so...
ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] {"services.xml", "daos.xml"}, MessengerService.class
Perhaps you should also use that Pattern with this Constructor:
ctx = new ClassPathXmlApplicationContext("oxm-context.xml", XmlMapper.class);
Related
I defined a classloader and added all my libraries, then I defined a ApplicationContext, I tried to getBean with ApplicationContext from a class in external jar, here are my key codes:
ApplicationContext context = new FileSystemXmlApplicationContext(beanPath) {
#Override
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
super.initBeanDefinitionReader(reader);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
reader.setBeanClassLoader(classLoader);
}
};
I tried to getBean like this:
AswanService instance = (AswanService)context.getBean("aswanService", AswanService.class);
But the question is, if I define the target class which properties have setter/getter methods, the instance object can be injected with the two properties, the class defines like this:
public class AswanServiceImpl implements AswanService {
SpringInjectFaker springInjectFaker;
ItemDO itemDO;
public AswanResponse request(AswanRequest aswanRequest) {
springInjectFaker.fake();
AswanResponse response = new AswanResponse();
response.setName("response: " + aswanRequest.getName() + "; " + itemDO.getItemId());
return response;
}
public SpringInjectFaker getSpringInjectFaker() {
return springInjectFaker;
}
public void setSpringInjectFaker(SpringInjectFaker springInjectFaker) {
this.springInjectFaker = springInjectFaker;
}
public ItemDO getItemDO() {
return itemDO;
}
public void setItemDO(ItemDO itemDO) {
this.itemDO = itemDO;
}
}
but if it is defined with autowired annotation, it doesn't work:
public class AswanServiceImpl implements AswanService {
#Autowired
SpringInjectFaker springInjectFaker;
#Autowired
ItemDO itemDO;
public AswanResponse request(AswanRequest aswanRequest) {
springInjectFaker.fake();
AswanResponse response = new AswanResponse();
response.setName("response: " + aswanRequest.getName() + "; " + itemDO.getItemId());
return response;
}
}
instance = context.getBean("aswanService", AswanService.class), instance is injected but its two properties itemDO and springInjectFaker are both null.
I tried to add config, but didn't work.
<?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/>
<bean id="aswanService" class="com.taobao.qa.perf.tr.AswanServiceImpl" autowire="byName"></bean>
<bean id="springInjectFaker" class="com.taobao.qa.perf.tr.SpringInjectFakerImp" autowire="byName"></bean>
<bean id="itemDO" class="com.taobao.qa.perf.tr.ItemDO" autowire="byName">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="2"></constructor-arg>
</bean>
</beans>
My project is java project, I run the program in main function (public static void main(String[] args)), thanks for your help ~~~
I figure it out.
Add or are both ok.
I come to a problem that when I add any the configuration above, the ide throw a exception:
org.springframework.beans.factory.BeanDefinitionStoreException: Parser configuration exception parsing XML from URL [file:xxxxxxx/beans.xml]; nested exception is javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [org.apache.xerces.jaxp.DocumentBuilderFactoryImpl#1d89c040] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
at com.alibaba.middleware.Application$2.<init>(Application.java:159)
at com.alibaba.middleware.Application.main(Application.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [org.apache.xerces.jaxp.DocumentBuilderFactoryImpl#1d89c040] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:102)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:70)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
... 20 more
Caused by: java.lang.IllegalArgumentException: No attributes are implemented
at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.setAttribute(DocumentBuilderFactoryImpl.java:93)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:99)
... 22 more
so I gave up, I just tried again, until I found that adding two args in VM options can solve this exception, and my problem is also solved. I don't know why, and I'm still studying.
-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
Thanks a lot.
Please to add <context:component-scan base-package="com.taobao.qa" /> to your config
mvc:annotation-driven is used for enabling the Spring MVC components with its default configurations.
If you dont include mvc:annotation-driven also your MVC application would work if you have used the context:component-scan for creating the beans or defined the beans in your XML file
context:component-scan element in the spring configuration file would eliminate the need for declaring all the beans in the XML files. Look at the below declaration in your spring configuration file:
<context:component-scan base-package="org.controller"/>
The above declaration in the spring application configuration file would scan the classes inside the specified package and create the beans instance. Note that it could create beans only if that class is annotated with correct annotations. The following are the annotations scanned by this element:
#Component
#Repository
#Service
#Controller
I'm learning spring dependency injection with Struts2, beased on a web project. In my example, I created a zoo having animals. Animal will talk if injection is succeed. E.g. in the console, we will see dog's talk :
Wowowo ฅ^•ﻌ•^ฅ
However, if injection failed, then we'll see :
zooService bean has not been injected.
Here's the architecture of my application :
com.zoo.controller.ZooController is the controller for receiving web actions.
com.zoo.service.ZooService is the interface for animal's talk
com.zoo.service.ZooServiceForDog is the implementation for dog's talk
Problem
Up to the step, everything is OK. And the dependency injection is handled by Spring using an XML file called applicationContext.xml. However, I've 2 types of configuration for this file, the first one Config 1 works but the second Config 2 doesn't.
Injection succeed using config 1.
<?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.xsd">
<bean id="zooService" class="com.zoo.service.ZooServiceForDog" />
</beans>
Injection failed using config 2.
<?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.xsd">
<bean id="zooController" class="com.zoo.controller.ZooController">
<property name="zooService" ref="zooServiceBean" />
</bean>
<bean id="zooServiceBean" class="com.zoo.service.ZooServiceForDog" />
</beans>
Can somebody explain why the Config 2 cannot work ?
Here're other codes that might be helpful to the issue :
Class com.zoo.controller.ZooController:
package com.zoo.controller;
import com.zoo.service.ZooService;
import com.opensymphony.xwork2.ActionSupport;
public class ZooController extends ActionSupport {
private static final long serialVersionUID = 1L;
private ZooService zooService;
public String live () {
if (zooService != null) {
zooService.talk();
} else {
System.out.println("zooService bean has not been injected.");
}
return SUCCESS;
}
public ZooService getZooService() {
return zooService;
}
public void setZooService(ZooService zooService) {
this.zooService = zooService;
}
}
It cannot work because the scope of the zooController is singleton. You should make the scope prototype.
<bean id="zooController" class="com.zoo.controller.ZooController" scope="prototype" >
<property name="zooService" ref="zooServiceBean" />
</bean>
The dependency management is defined by the container:
If your actions managed by Struts container, then Struts is creating them in the default scope. If your actions is managed by Spring container then you need to define the scope of the action beans, because Spring by default uses singleton scope and if you don't want to share your action beans between user's requests you should define the corresponding scope. You can use prototype scope, which means a new instance is returned by the Spring each time Struts is being built an action instance.
The Struts integrates to Spring via plugin. Make sure it has
<constant name="struts.objectFactory" value="spring" />
then you can delegate actions to Spring
References:
Struts2 and Spring
Spring plugin
EDIT:
In your first config you declared a bean zooService that will be injected by Struts using spring object factory.
In your second config you declared two beans zooController and zooServiceBean, but you changed the name of the second bean. Then you tried to build the action bean using spring object factory like in the first case. And because there's no bean with name zooService the autowiring has been failed. Because by default Struts is configured to autowire beans from the application context by name.
Then you changed struts.xml and used a bean reference in the action class attribute. It means that Struts will use app context to get a bean from Spring. And because it has an explicit dependency on the second bean, it would be wired before the bean is returned.
I am new to Spring.
Currently, I am trying to achieve following:
A method in class A calls a method in class B with a String parameter. Class B then creates a bean after processing the string (just doing some basic string operations) as:
GenericApplicationContext context = new GenericApplicationContext();
DefaultListableBeanFactory factory = (DefaultListableBeanFactory) context.getBeanFactory();
BeanDefinitionBuilder bean1 = BeanDefinitionBuilder.rootBeanDefinition("className");
bean1.addPropertyReference("sampleString", "sampleBean");
bean1.addPropertyValue("sampleString", "processed string here");
factory.registerBeanDefinition("sampleBean", bean1.getBeanDefinition());
context.refresh();
(I am not sure if my approach to creating bean here is correct. If not, please suggest the appropriate way).
I want this bean to be passed to class A (ofcourse the Spring Way). The name of variable is sampleString.
So far I had been using something like:
<?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.0.xsd">
<bean id="proc" class="A">
</bean>
<bean id="result" class="B">
<property name="sampleString" value="#{proc.sampleString}"/>
</bean>
</beans>
But this copies null value to the variable in class A (ofcourse because the method executes only when a button is pressed).
So is there a cleaner way to do this?
I'm working with Spring, and I'm getting the injected instance as null when I create the instance with new operator, I can elobrate the scenario.
For example,
Let the class A and class B are injected into the class Main
class Main
{
#autowired
A a;
#autowired
B b;
//getter and setter
}
class MainExecute
{
public static void main()
{
// loading the spring config xml
Main main = new Main();
A a=main.getA();
// whether a will get the instance ( I'm getting a as null)
}
what could be the reason for this scenario
Please guide me on the same
Thanks in advance.
No, it won't get dependency injection.
Only objects created/managed by the spring container will get the facilities offered by spring dependency injection.
To elaborate on the above answers:
You need to let the spring IOC container create your objects for you, you don't explicitly create them. You can do this by creating a spring config xml file, here is a quick example:
META-INF\spring\my-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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="myMainClass" class="org.foo.Main" />
<bean id="myA" class="org.foo.A" />
<bean id="myB" class="org.foo.B" />
</beans>
org.foo.MainExecute:
class MainExecute
{
public static void main()
{
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("META-INF\\spring\\my-spring-config.xml");
Main main = (Main) appContext.getBean("myMainClass");
}
}
In this example the Spring IOC container will instantiate an "A" bean and a "B" bean. It will then autowire these into the "Main" bean.
I have started learning hibernate and spring through an assignment in which i am trying to use session factory instance through spring. I understood the hibernate part but just cant go on with spring. I have tried numerous tutorials and examples but just cant get my spring working. Although it works when i instantiate it directly.
Here are the problem related details of my project...
applicationContext.xml (inside WEB-INF)
<?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:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<context:component-scan
base-package="com.nagarro.training.assignment6.dao.entity.Student" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven />
<bean id="studentDAO"
class="com.nagarro.training.assignment6.dao.impl.StudentDAOImplementation">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Edit: including changes as suggested
StudentDAOImplementaion.java (the file where it is being used )
public class StudentDAOImplementation implements StudentDAO {
/**
* single instance of hibernate session
*/
#Autowired
private SessionFactory sessionFactory;
// HibernateUtil.getSessionFactory().openSession()
/**
* #param sessionFactory
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* #return the sessionFactory
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
private Session getSession() {
return this.sessionFactory.getCurrentSession();
}
/**
* #return list of all the students
*/
public List<Student> getStudentList() {
System.out.println(this.getSession().getStatistics());
return (List<Student>) this.getSEssion.createQuery("from Student")
.list();
}
}
And here is the snip of my jar files in lib folder:
I think that i dont need to include hibernate files and beans as it is working fine without spring. Its the spring i cant get to work.I have tried many different implementation from the web but i just cannot get it working. It just says null pointer exception on ** System.out.println(sessionFactory.getStatistics());** line in StudentDAOImplementation.
Test class that calls StudentDAO
public class Test {
public static void main(String[] args) {
StudentDAOImplementation sd = new StudentDAOImplementation();
List<Student> list = sd.getStudentList();
for(Student s : list) {
System.out.println(s.getName());
}
}
}
Stack trace
Exception in thread "main" java.lang.NullPointerException
at StudentDAOImplementation.getStudentList(StudentDAOImplementation.java:116)
at Test.main(Test.java:13)
Your sessionFactory variable is misleadingly named, since the type is actually Session. Session != SessionFactory. You're getting a NPE on sessionFactory.getStatistics() because there's no way that Spring can autowire a Session into a DAO like that. If you're not seeing an error before the NPE, then you're not actually instantiating the DAO with Spring, or else you'd get an error about not being able to find a dependency of type Session. The appropriate way to use a Hibernate-based DAO is to inject it with a SessionFactory and call getCurrentSession() in your methods where you need a Session. See "Implementing DAOs based on plain Hibernate 3 API" and following for details about this approach and about setting up appropriate transaction management.
Update: On a second glance, I see that the package for your component-scan is set to com.nagarro.training.assignment6.dao.entity.Student, which looks exactly like a class, not a package. It's also not even close to anything you'd actually want to component-scan. Maybe you don't understand what component-scan is for. It's covered under "Annotation-based container configuration" in the reference guide.
Update 2: About your "test" code: you're not using Spring at all, so you might as well remove the XML and save yourself the trouble. On the other hand, if you'd like to actually use Spring, you'd need to create a context in your main method based on said XML file, such as:
ApplicationContext context = new FileSystemXmlApplicationContext(locationOfXmlFile);
Then if you want a Spring-managed DAO, you can't just create one with new. Spring isn't magic. It doesn't grab control away from you just because you have it loaded somewhere in the same JVM.* You have to ask Spring for the DAO that it created, like:
StudentDAO dao = context.getBean(StudentDAO.class);
Note that I used the interface type, not the concrete type. That's always an advisable practice for numerous reasons.
This (not starting Spring) is your first problem. As soon as you do this, you're going to run into other problems with your configuration. You should post a new question if you need help solving one of them.
*Unless you're using AspectJ weaving to inject arbitrary objects.
You are injecting a Session instead of SessionFactory with #Autowired private Session sessionFactory; in the DAO class. It needs to be a SessionFactory , like this
#Autowired SessionFactory sessionFactory;
And then use it like this to do a DAO operations like save
Session session = sessionFactory.getCurrentSession()
session.persist(entity);
EDIT
Your testcase should be something like this
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:<path_to_your_appcontext.xml>" })
public class StudentDAOTest {
#Autowired
private StudentDAO studentDAO
#Test
public void test() {
List<Student> list = studentDAO.getStudentList();
assertNotNull(list)
}
}