java - Bean scope not working -
the code shows want create loginbean that's sessions scoped. i'll show applicationcontext , service , dao because there may configuration error in them.
i noticed beans processed application scoped. (ran project , logged in , showed username on header. opened browser, went localhost , username filled in..)
anyone got idea?
bean:
package be.neoria.swissknife.bean; import be.neoria.swissknife.model.consultant; import be.neoria.swissknife.model.klant; import be.neoria.swissknife.model.project; import be.neoria.swissknife.model.taak; import be.neoria.swissknife.service.loginservice; import be.neoria.swissknife.service.projectservice; import org.hibernate.validator.constraints.notempty; import org.springframework.beans.factory.annotation.autowired; import javax.faces.bean.managedproperty; import javax.inject.named; import java.io.serializable; import java.util.calendar; import java.util.date; import javax.enterprise.context.sessionscoped; /** * created intellij idea. user: greg date: 27/03/13 time: 15:48 change * template use file | settings | file templates. */ @named @sessionscoped public class loginbean implements serializable { private final string failure = "failure"; private final string success = "success"; @notempty(message = "enter username") private string gebruikersnaam; @notempty(message = "enter password") private string paswoord; private project project; private klant costumer; private taak taak; private consultant consultant; private taak task; private klant customer; private boolean isingelogd; private date today; @managedproperty(value = "#{loginservice}") @autowired loginservice loginservice; @managedproperty(value = "#{projectservice}") @autowired projectservice projectservice; public loginbean() { } public klant getcustomer() { return customer; } public void setcustomer(klant customer) { this.customer = customer; } public project getproject() { return project; } public void setproject(project project) { this.project = project; } public taak gettask() { return task; } public void settask(taak task) { this.task = task; } public string getgebruikersnaam() { return gebruikersnaam; } public void setgebruikersnaam(string gebruikersnaam) { this.gebruikersnaam = gebruikersnaam; } public taak gettaak() { return taak; } public void settaak(taak taak) { this.taak = taak; } public string getpaswoord() { return paswoord; } public void setpaswoord(string paswoord) { this.paswoord = paswoord; } public consultant getconsultant() { return consultant; } public void setconsultant(consultant consultant) { this.consultant = consultant; } public boolean isingelogd() { return isingelogd; } public void setingelogd(boolean ingelogd) { isingelogd = ingelogd; } public klant getcostumer() { return costumer; } public void setcostumer(klant costumer) { this.costumer = costumer; } public string loginconsultant() { consultant = loginservice.loginconsultant(gebruikersnaam, paswoord); if (consultant == null) { return failure; } else { isingelogd = true; return success; } }
loginservice:
package be.neoria.swissknife.service; import be.neoria.swissknife.model.consultant; /** * created intellij idea. * user: greg * date: 29/03/13 * time: 12:09 * change template use file | settings | file templates. */ public interface loginservice { public consultant loginconsultant(string username, string password); }
loginservice implementation:
package be.neoria.swissknife.service; import be.neoria.swissknife.dao.consultantdao; import be.neoria.swissknife.model.consultant; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; /** * created intellij idea. * user: greg * date: 29/03/13 * time: 12:10 * change template use file | settings | file templates. */ @transactional @service("loginservice") public class loginserviceimpl implements loginservice { @autowired consultantdao consultantdao; @override public consultant loginconsultant(string username, string password) { consultant consultant = consultantdao.getconsultantbyname(username); if (consultant.getpaswoord().equals(password)) { return consultant; } else { return null; } }
consultantdao:
package be.neoria.swissknife.dao; import be.neoria.swissknife.model.adres; import be.neoria.swissknife.model.consultant; import java.util.list; /** * created intellij idea. user: greg date: 29/03/13 time: 12:18 change * template use file | settings | file templates. */ public interface consultantdao { public consultant getconsultantbyname(string name); }
consultantdaoimpl:
package be.neoria.swissknife.dao; import be.neoria.swissknife.model.adres; import be.neoria.swissknife.model.consultant; import java.util.list; import org.hibernate.query; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.repository; /** * created intellij idea. * user: greg * date: 29/03/13 * time: 12:18 * change template use file | settings | file templates. */ @repository public class consultantdaoimpl implements consultantdao { @autowired private sessionfactory sessionfactory; @override public consultant getconsultantbyname(string name) { query query = sessionfactory.getcurrentsession().createquery("from consultant naam=:name"); query.setparameter("name", name); return (consultant) query.uniqueresult(); } }
applicationcontext.xml (in web-inf), reffered in web.xml file
<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:sec="http://www.springframework.org/schema/security" xsi:schemalocation=" http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <context:component-scan base-package="be.neoria.swissknife.service"/> <context:component-scan base-package="be.neoria.swissknife.dao"/> <context:component-scan base-package="be.neoria.swissknife.model"/> <context:component-scan base-package="be.neoria.swissknife.bean"/> <context:component-scan base-package="be.neoria.swissknife.util"/> <context:annotation-config/> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"> <property name="driverclassname" value="com.mysql.jdbc.driver"/> <property name="url" value="jdbc:mysql://localhost:3306/swissknife"/> <property name="username" value="swissknife"/> <property name="password" value="swissknife"/> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource"/> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="packagestoscan"> <list> <value>be.neoria.swissknife.model</value> <value>be.neoria.swissknife.service</value> <value>be.neoria.swissknife.dao</value> <value>be.neoria.swissknife.model</value> </list> </property> </bean> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory"/> </bean> <tx:annotation-driven transaction-manager="transactionmanager"/> <bean id="messagesource" class="org.springframework.context.support.reloadableresourcebundlemessagesource"> <property name="basename" value="classpath:messages"/> </bean> <bean class="org.springframework.web.servlet.i18n.cookielocaleresolver" id="localeresolver" /> </beans>
try
@component @scope(value = "request")
instead of @sessionscoped