|
|
|
Gadbury
|
Hi all,
I am trying to implement the following example (Implementing Spring-based DAOs without callbacks), as here: https://springmodules.dev.java.net/docs/reference/0.3/html_single/#d0e1669 Within my DAO (which extends JackrabbitDAOSupport, which itself extends JcrDaoSupport), getSession(true) is throwing the following exception: java.lang.IllegalArgumentException: No sessionFactory specified. Here is my code: ProviderDAOImpl: public class ProviderDAOImpl extends JackrabbitDAOSupport { private static org.apache.log4j.Logger _lLogger = org.apache.log4j.Logger.getLogger(ProviderDAOImpl.class); public ProviderDAOImpl() { super(); } public ArrayList<ProviderBean> listProviders() { Session jcrSession = getSession(true); ArrayList<ProviderBean> providers = new ArrayList<ProviderBean>(); try { Workspace ws = jcrSession.getWorkspace(); QueryManager qm = ws.getQueryManager(); // Specify a query using the XPATH query language Query q = qm.createQuery("//provider[@isDeleted!='true']", Query.XPATH); QueryResult res = q.execute(); // Obtain a node iterator NodeIterator provs = res.getNodes(); _lLogger.debug(provs.getSize()); while (provs.hasNext()) { Node providerNode = provs.nextNode(); providers.add(new ProviderBean(providerNode.getUUID(), providerNode.getProperty("name").getString(), providerNode.getProperty("GUID") .getString(), providerNode.getProperty("isDeleted").getBoolean())); } } catch (Exception e) { _lLogger.fatal(e.getCause(), e); } finally { } return providers; } } ------------------------------------- JackrabbitDAOSupport: public class JackrabbitDAOSupport extends JcrDaoSupport { JcrSessionFactory sessionFactory; JcrTemplate template; public JackrabbitDAOSupport() { } } ------------------------------------- applicationContext-jcr.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" 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"> <!-- Register Annotation-based Post Processing Beans --> <context:annotation-config /> <!-- Creates a session factory based on the respository --> <bean id="sessionFactory" class="org.springmodules.jcr.JcrSessionFactory"> <property name="repository" ref="repository" /> <property name="credentials"> <bean class="javax.jcr.SimpleCredentials"> <constructor-arg index="0" value="userid" /> <!-- create the credentials using a bean factory --> <constructor-arg index="1"> <bean factory-bean="password" factory-method="toCharArray" /> </constructor-arg> </bean> </property> </bean> <!-- create the password to return it as a char[] --> <bean id="password" class="java.lang.String"> <constructor-arg index="0" value="" /> </bean> <!-- Creates a JcrTemplate using the session factory --> <bean id="jcrTemplate" class="org.springmodules.jcr.JcrTemplate"> <property name="sessionFactory" ref="sessionFactory" /> <property name="allowCreate" value="true" /> </bean> <!-- DAO configurations --> <bean id="jackrabbitDAOSupport" class="net.gb.mds.atl.ecommerce.dao.JackrabbitDAOSupport"> <property name="sessionFactory" ref="sessionFactory" /> <property name="template" ref="jcrTemplate" /> </bean> <!-- normal repository The first bean definition defines defines the Jackrabbit repository by specifying the configuration file to use and the location of the repository. If the repository doesn't already exist, it will be created on startup. --> <bean id="repository" class="org.springmodules.jcr.jackrabbit.RepositoryFactoryBean"> <!-- normal factory beans params --> <property name="configuration" value="classpath:repository.xml" /> <!-- use the target folder which will be cleaned --> <property name="homeDir" value="file:C:/repository1" /> </bean> </beans> ------------------------------------- I understand that I may need to inject the jcrTemplate into my DAO (as getSession() eventually calls template.getSessionFactory(), and in the debugger template == null). Please could someone help me out? I've tried with and without using the JackrabbitDAOSupport class (i.e. in the spring config file). I've read over and over the tutorial example but it seems that some information is missing (or more likely I am doing something daft). Running through the debugger I can see that the initial call to JcrDaoSupport's setSessionFactory() is correctly instantiating the member template. Thanks in advance for any help. |
||||||||||||||||
|
Gadbury
|
Is anyone able to advise a Spring newbie, please? I'm still stuck with this and have spent quite some time trying to work it out. It seems that when my web app starts, the DAO's template is correctly set but later when I instantiate and use my DAO, getSession(true) fails as the DAO's template is null.
Where / how should I set the DAO's template / template's sessionFactory? |
||||
|
Sergey Podatelev
|
How do you instantiate your ProviderDAOImpl?
You have Spring instantiating an instance of JackrabbitDaoImpl (thus properly initializing SessionFactory), but does it ever instantiates an instance of ProviderDAOImpl? On Wed, Jul 8, 2009 at 3:39 PM, Gadbury<[hidden email]> wrote: > > Is anyone able to advise a newbie, please? I'm still stuck with this and > have spent quite some time trying to work it out. It seems that as my web > app starts, template is correctly set but later when I instantiate and use > my DAO, getSession(true) fails as the DAO's template is null. > > > -- > View this message in context: http://www.nabble.com/spring-modules-jcr---Implementing-Spring-based-DAOs-without-callbacks-tp24289110p24389891.html > Sent from the Jackrabbit - Users mailing list archive at Nabble.com. > > -- sp |
||||||||||||||||
|
Gadbury
|
Hi Sergey. Thanks for your reply. I understand what you are saying but I still have some problems with "No sessionFactory specified". I have tried the following: ---------------------------------------- ProviderDAOImpl: public class ProviderDAOImpl extends JcrDaoSupport { private static Logger logger = Logger.getLogger(ProviderDAOImpl.class); public ProviderDAOImpl() { super(); } public ArrayList<ProviderBean> listProviders() { Session jcrSession = getSession(true); ArrayList<ProviderBean> providers = new ArrayList<ProviderBean>(); try { Workspace ws = jcrSession.getWorkspace(); QueryManager qm = ws.getQueryManager(); // Specify a query using the XPATH query language Query q = qm.createQuery("//provider[@isDeleted!='true']", Query.XPATH); QueryResult res = q.execute(); // Obtain a node iterator NodeIterator provs = res.getNodes(); logger.debug(provs.getSize()); while (provs.hasNext()) { Node providerNode = provs.nextNode(); providers.add( new ProviderBean(providerNode.getUUID(), providerNode.getProperty("name").getString(), providerNode.getProperty("GUID").getString(), providerNode.getProperty("isDeleted").getBoolean())); } } catch (Exception e) { logger.error(e.getCause(), e); } finally { } return providers; } } ---------------------------------------- applicationContext-jcr.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" 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"> <!-- Register Annotation-based Post Processing Beans --> <context:annotation-config /> <!-- Creates a session factory based on the respository --> <bean id="sessionFactory" class="org.springmodules.jcr.JcrSessionFactory"> <property name="repository" ref="repository" /> <property name="credentials"> <bean class="javax.jcr.SimpleCredentials"> <constructor-arg index="0" value="userid" /> <!-- create the credentials using a bean factory --> <constructor-arg index="1"> <bean factory-bean="password" factory-method="toCharArray" /> </constructor-arg> </bean> </property> </bean> <!-- Create the password to return it as a char[] --> <bean id="password" class="java.lang.String"> <constructor-arg index="0" value="" /> </bean> <!-- Create a JcrTemplate using the session factory --> <bean id="jcrTemplate" class="org.springmodules.jcr.JcrTemplate"> <property name="sessionFactory" ref="sessionFactory" /> <property name="allowCreate" value="true" /> </bean> <!-- DAO configurations --> <bean id="jcrDaoProviderImpl" class="net.gb.mds.atl.ecommerce.dao.ProviderDAOImpl"> <property name="sessionFactory" ref="sessionFactory" /> <property name="template" ref="jcrTemplate" /> </bean> <!-- Repository definition --> <bean id="repository" class="org.springmodules.jcr.jackrabbit.RepositoryFactoryBean"> <property name="configuration" value="classpath:repository.xml" /> <property name="homeDir" value="file:C:/repository1" /> </bean> </beans> ---------------------------------------- With the above code, I have removed the previous superclass and now the ProviderDAOImpl extends directly from the spring-modules-jcr class JcrDaoSupport. I am also instantiating a ProviderDAOImpl Spring bean (id = jcrDaoProviderImpl), which should inject the template (I'm guessing that I do not need the second property to inject the sessionFactory into the ProviderDAOImpl bean as sessionFactory is a member of JcrTemplate and is already injected in to the jcrTemplate bean...) ? I still have the same problem. The code falls over at ProviderDAOImpl call of getSession(true), reporting that "No sessionFactory specified" (specifically at the Assert statement in the method doGetSession() of the class org.springmodules.jcr.SessionFactoryUtils). When I debug, the first time the jcrTemplate is injected. Thereafter, it fails (template == null). I instantiate my ProviderDAOImpl from the rest of my application's classes as follows: ProviderDAOImpl provDAO = new ProviderDAOImpl(); As I am new to Spring (and really just wanted to use the spring-modules-jcr lib for simple repository configuration / creation and (Jcr) Session management), I'm sure I must have missed out something important. Can the Spring ProviderDAOImpl bean (defined above) use any name as the bean id? Should I instantiate ProviderDAOImpl in some other way? Many thanks for your time and help. |
||||||||||||||||
|
Sergey Podatelev
|
Okay, see, I'm far from being a Spring expert either.
But as far as I understand your code, the following happens: Spring instantiates an instance of ProviderDAOImpl (a ProviderDAOImpl bean) and provides it with jcrTemplate. Now if you want an working instance of ProviderDAOImpl, you have to access the bean configured in application context. What you're doing instead is creating a new instance of ProviderDAOImpl which is obviously never initialized. I' don't know whether you're using any additional frameworks which have specific hooks for accessing Spring beans (like Wicket, for instance), but here're a couple of links that describe what I believe is a universal way to fetch your beans from the application context: http://sujitpal.blogspot.com/2007/03/accessing-spring-beans-from-legacy-code.html http://blog.jdevelop.eu/2008/07/06/access-the-spring-applicationcontext-from-everywhere-in-your-application/ On Wed, Jul 8, 2009 at 11:41 PM, Gadbury<[hidden email]> wrote: > > > > Sergey Podatelev wrote: >> >> How do you instantiate your ProviderDAOImpl? >> You have Spring instantiating an instance of JackrabbitDaoImpl (thus >> properly initializing SessionFactory), but does it ever instantiates >> an instance of ProviderDAOImpl? >> > > Hi Sergey. Thanks for your reply. I understand what you are saying. I > have tried the following: > > ---------------------------------------- > > ProviderDAOImpl: > > public class ProviderDAOImpl extends JcrDaoSupport > { > private static Logger logger = Logger.getLogger(ProviderDAOImpl.class); > > public ProviderDAOImpl() > { > super(); > } > > public ArrayList<ProviderBean> listProviders() > { > Session jcrSession = getSession(true); > ArrayList<ProviderBean> providers = new ArrayList<ProviderBean>(); > > try > { > Workspace ws = jcrSession.getWorkspace(); > QueryManager qm = ws.getQueryManager(); > > // Specify a query using the XPATH query language > Query q = qm.createQuery("//provider[@isDeleted!='true']", Query.XPATH); > QueryResult res = q.execute(); > > // Obtain a node iterator > NodeIterator provs = res.getNodes(); > logger.debug(provs.getSize()); > > while (provs.hasNext()) > { > Node providerNode = provs.nextNode(); > providers.add( new ProviderBean(providerNode.getUUID(), > providerNode.getProperty("name").getString(), > providerNode.getProperty("GUID").getString(), > providerNode.getProperty("isDeleted").getBoolean())); > } > > } > catch (Exception e) > { > logger.error(e.getCause(), e); > } > finally > { > > } > > return providers; > } > } > > ---------------------------------------- > > applicationContext-jcr.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" > 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"> > > <!-- Register Annotation-based Post Processing Beans --> > <context:annotation-config /> > > <!-- Creates a session factory based on the respository --> > <bean id="sessionFactory" > class="org.springmodules.jcr.JcrSessionFactory"> > <property name="repository" ref="repository" /> > <property name="credentials"> > <bean class="javax.jcr.SimpleCredentials"> > <constructor-arg index="0" value="userid" /> > <!-- create the credentials using a bean factory --> > <constructor-arg index="1"> > <bean factory-bean="password" factory-method="toCharArray" /> > </constructor-arg> > </bean> > </property> > </bean> > > <!-- Create the password to return it as a char[] --> > <bean id="password" class="java.lang.String"> > <constructor-arg index="0" value="" /> > </bean> > > <!-- Create a JcrTemplate using the session factory --> > <bean id="jcrTemplate" class="org.springmodules.jcr.JcrTemplate"> > <property name="sessionFactory" ref="sessionFactory" /> > <property name="allowCreate" value="true" /> > </bean> > > <!-- DAO configurations --> > <bean id="jcrDaoProviderImpl" > class="net.gb.mds.atl.ecommerce.dao.ProviderDAOImpl"> > <property name="sessionFactory" ref="sessionFactory" /> > <property name="template" ref="jcrTemplate" /> > </bean> > > <!-- Repository definition --> > <bean id="repository" > class="org.springmodules.jcr.jackrabbit.RepositoryFactoryBean"> > <property name="configuration" value="classpath:repository.xml" /> > <property name="homeDir" value="file:C:/repository1" /> > </bean> > > </beans> > > ---------------------------------------- > > With the above code, I have removed the previous superclass and now the > ProviderDAOImpl extends directly from the spring-modules-jcr class > JcrDaoSupport. > > I am also instantiating a ProviderDAOImpl Spring bean (id = > jcrDaoProviderImpl), which should inject the template (I'm guessing that I > do not need the second property to inject the sessionFactory into the > ProviderDAOImpl bean as sessionFactory is a member of JcrTemplate and is > already injected in to the jcrTemplate bean...) ? > > I still have the same problem. The code falls over at ProviderDAOImpl call > of getSession(true), reporting that "No sessionFactory specified" > (specifically at the Assert statement in the method doGetSession() of the > class org.springmodules.jcr.SessionFactoryUtils). When I debug, the first > time the jcrTemplate is injected. Thereafter, it fails (template == null). > I instantiate my ProviderDAOImpl from the rest of my application's classes > as follows: > > ProviderDAOImpl provDAO = new ProviderDAOImpl(); > > As I am new to Spring (and really just wanted to use the spring-modules-jcr > lib for simple repository configuration / creation and (Jcr) Session > management), I'm sure I must have missed out something important. > > Can the Spring ProviderDAOImpl bean (defined above) use any name as the bean > id? > > Should I instantiate ProviderDAOImpl in some other way? > > Many thanks for your help. > -- > View this message in context: http://www.nabble.com/spring-modules-jcr---Implementing-Spring-based-DAOs-without-callbacks-tp24289110p24397985.html > Sent from the Jackrabbit - Users mailing list archive at Nabble.com. > > -- sp |
||||||||||||||||
|
Gadbury
|
Thank you for your advice, Sergey.
|
||||||||||||||||
|
SalvatoreIncandela
|
Hi all, if you are interested to spring-modules-jcr, you'll be surely
interested to know thaht the module jcr was migrated to Spring extensions: http://se-jcr.sourceforge.net/ 2009/7/9 Gadbury <[hidden email]> > > Thank you for your advice, Sergey. > > > Sergey Podatelev wrote: > > > > Okay, see, I'm far from being a Spring expert either. > > > > But as far as I understand your code, the following happens: > > > > Spring instantiates an instance of ProviderDAOImpl (a ProviderDAOImpl > > bean) and provides it with jcrTemplate. > > Now if you want an working instance of ProviderDAOImpl, you have to > > access the bean configured in application context. > > What you're doing instead is creating a new instance of > > ProviderDAOImpl which is obviously never initialized. > > > > I' don't know whether you're using any additional frameworks which > > have specific hooks for accessing Spring beans (like Wicket, for > > instance), but here're a couple of links that describe what I believe > > is a universal way to fetch your beans from the application context: > > > > > http://sujitpal.blogspot.com/2007/03/accessing-spring-beans-from-legacy-code.html > > > http://blog.jdevelop.eu/2008/07/06/access-the-spring-applicationcontext-from-everywhere-in-your-application/ > > > > > > > > On Wed, Jul 8, 2009 at 11:41 PM, Gadbury<[hidden email]> wrote: > >> > >> > >> > >> Sergey Podatelev wrote: > >>> > >>> How do you instantiate your ProviderDAOImpl? > >>> You have Spring instantiating an instance of JackrabbitDaoImpl (thus > >>> properly initializing SessionFactory), but does it ever instantiates > >>> an instance of ProviderDAOImpl? > >>> > >> > >> Hi Sergey. Thanks for your reply. I understand what you are saying. I > >> have tried the following: > >> > >> ---------------------------------------- > >> > >> ProviderDAOImpl: > >> > >> public class ProviderDAOImpl extends JcrDaoSupport > >> { > >> private static Logger logger = > >> Logger.getLogger(ProviderDAOImpl.class); > >> > >> public ProviderDAOImpl() > >> { > >> super(); > >> } > >> > >> public ArrayList<ProviderBean> listProviders() > >> { > >> Session jcrSession = getSession(true); > >> ArrayList<ProviderBean> providers = new > >> ArrayList<ProviderBean>(); > >> > >> try > >> { > >> Workspace ws = jcrSession.getWorkspace(); > >> QueryManager qm = ws.getQueryManager(); > >> > >> // Specify a query using the XPATH query language > >> Query q = > >> qm.createQuery("//provider[@isDeleted!='true']", Query.XPATH); > >> QueryResult res = q.execute(); > >> > >> // Obtain a node iterator > >> NodeIterator provs = res.getNodes(); > >> logger.debug(provs.getSize()); > >> > >> while (provs.hasNext()) > >> { > >> Node providerNode = > >> provs.nextNode(); > >> providers.add( new > >> ProviderBean(providerNode.getUUID(), > >> > >> providerNode.getProperty("name").getString(), > >> > >> providerNode.getProperty("GUID").getString(), > >> > >> providerNode.getProperty("isDeleted").getBoolean())); > >> } > >> > >> } > >> catch (Exception e) > >> { > >> logger.error(e.getCause(), e); > >> } > >> finally > >> { > >> > >> } > >> > >> return providers; > >> } > >> } > >> > >> ---------------------------------------- > >> > >> applicationContext-jcr.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" > >> 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"> > >> > >> <!-- Register Annotation-based Post Processing Beans --> > >> <context:annotation-config /> > >> > >> <!-- Creates a session factory based on the respository --> > >> <bean id="sessionFactory" > >> class="org.springmodules.jcr.JcrSessionFactory"> > >> <property name="repository" ref="repository" /> > >> <property name="credentials"> > >> <bean > class="javax.jcr.SimpleCredentials"> > >> <constructor-arg index="0" > >> value="userid" /> > >> <!-- create the credentials using > >> a bean factory --> > >> <constructor-arg index="1"> > >> <bean > >> factory-bean="password" factory-method="toCharArray" /> > >> </constructor-arg> > >> </bean> > >> </property> > >> </bean> > >> > >> <!-- Create the password to return it as a char[] --> > >> <bean id="password" class="java.lang.String"> > >> <constructor-arg index="0" value="" /> > >> </bean> > >> > >> <!-- Create a JcrTemplate using the session factory --> > >> <bean id="jcrTemplate" class="org.springmodules.jcr.JcrTemplate"> > >> <property name="sessionFactory" ref="sessionFactory" /> > >> <property name="allowCreate" value="true" /> > >> </bean> > >> > >> <!-- DAO configurations --> > >> <bean id="jcrDaoProviderImpl" > >> class="net.gb.mds.atl.ecommerce.dao.ProviderDAOImpl"> > >> <property name="sessionFactory" > >> ref="sessionFactory" /> > >> <property name="template" ref="jcrTemplate" /> > >> </bean> > >> > >> <!-- Repository definition --> > >> <bean id="repository" > >> class="org.springmodules.jcr.jackrabbit.RepositoryFactoryBean"> > >> <property name="configuration" > >> value="classpath:repository.xml" /> > >> <property name="homeDir" > >> value="file:C:/repository1" /> > >> </bean> > >> > >> </beans> > >> > >> ---------------------------------------- > >> > >> With the above code, I have removed the previous superclass and now the > >> ProviderDAOImpl extends directly from the spring-modules-jcr class > >> JcrDaoSupport. > >> > >> I am also instantiating a ProviderDAOImpl Spring bean (id = > >> jcrDaoProviderImpl), which should inject the template (I'm guessing that > >> I > >> do not need the second property to inject the sessionFactory into the > >> ProviderDAOImpl bean as sessionFactory is a member of JcrTemplate and is > >> already injected in to the jcrTemplate bean...) ? > >> > >> I still have the same problem. The code falls over at ProviderDAOImpl > >> call > >> of getSession(true), reporting that "No sessionFactory specified" > >> (specifically at the Assert statement in the method doGetSession() of > the > >> class org.springmodules.jcr.SessionFactoryUtils). When I debug, the > >> first > >> time the jcrTemplate is injected. Thereafter, it fails (template == > >> null). > >> I instantiate my ProviderDAOImpl from the rest of my application's > >> classes > >> as follows: > >> > >> ProviderDAOImpl provDAO = new ProviderDAOImpl(); > >> > >> As I am new to Spring (and really just wanted to use the > >> spring-modules-jcr > >> lib for simple repository configuration / creation and (Jcr) Session > >> management), I'm sure I must have missed out something important. > >> > >> Can the Spring ProviderDAOImpl bean (defined above) use any name as the > >> bean > >> id? > >> > >> Should I instantiate ProviderDAOImpl in some other way? > >> > >> Many thanks for your help. > >> -- > >> View this message in context: > >> > http://www.nabble.com/spring-modules-jcr---Implementing-Spring-based-DAOs-without-callbacks-tp24289110p24397985.html > >> Sent from the Jackrabbit - Users mailing list archive at Nabble.com. > >> > >> > > > > > > > > -- > > sp > > > > > > -- > View this message in context: > http://www.nabble.com/spring-modules-jcr---Implementing-Spring-based-DAOs-without-callbacks-tp24289110p24414004.html > Sent from the Jackrabbit - Users mailing list archive at Nabble.com. > > -- -- Salvatore Incandela Software Architect @ Pro-netics Committer/Developer @ SpringSource --------------------------------------- (Pro-netics) s.p.a. http://www.pronetics.it [hidden email] [hidden email] skype s.incandela yahoo s.incandela mobile: 349 6196615 Sede operativa: Via Mario Bianchini, 51 00142 Roma Sede legale: Via Elio Lampridio Cerva, 127 c 00143 Roma Tel. 06/51957945 Fax 06/5038035 |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |