2013-04-25 232 views
0

該代碼顯示我想創建一個會話作用域的loginBean。我將顯示applicationContext以及服務和dao,因爲它們中可能還存在配置錯誤。Bean scope not working

我注意到我的所有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 with IntelliJ IDEA. User: Greg Date: 27/03/13 Time: 15:48 To change 
* this 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; 
    } 
} 

login服務:

package be.neoria.swissknife.service; 
import be.neoria.swissknife.model.Consultant; 

/** 
* Created with IntelliJ IDEA. 
* User: Greg 
* Date: 29/03/13 
* Time: 12:09 
* To change this template use File | Settings | File Templates. 
*/ 
public interface LoginService { 
    public Consultant loginConsultant(String username, String password); 
} 

login服務實現:

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 with IntelliJ IDEA. 
* User: Greg 
* Date: 29/03/13 
* Time: 12:10 
* To change this 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 with IntelliJ IDEA. User: Greg Date: 29/03/13 Time: 12:18 To change 
* this 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 with IntelliJ IDEA. 
* User: Greg 
* Date: 29/03/13 
* Time: 12:18 
* To change this 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 where naam=:name"); 
    query.setParameter("name", name); 

    return (Consultant) query.uniqueResult(); 

    } 

} 

的applicationContext.xml(在WEB-INF),在web.xml文件下文稱到

<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> 

回答

0

嘗試

@Component 
@Scope(value = "request") 

代替@SessionScoped

+0

認爲它正在工作,但得到另一個錯誤(BeanCreationException),我必須在測試之前先解決這個錯誤。如果這是答案,我會標記它,謝謝。 – GregD 2013-04-25 09:41:45