Bean范围不起作用

时间:2013-04-25 09:04:18

标签: java spring session scope javabeans

代码显示我想创建一个以会话为范围的loginBean。我也将显示applicationContext以及service和dao,因为它们中可能还存在配置错误。

我注意到我的所有bean都是作为应用程序作用域处理的。 (跑完项目并登录并在标题上显示我的用户名。然后我打开另一个浏览器,转到我的localhost并填写了用户名..)

有人有任何想法吗?

豆:

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);
}

loginService实现:

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>

1 个答案:

答案 0 :(得分:0)

尝试

@Component
@Scope(value = "request") 

而不是@SessionScoped