将类交给spring管理,你确定不会出问题?(求大神指导)

z1246879396 2016-10-20 02:33:46
这里是我的一个小项目。
首先给出我认为解决这个问题必须要看的代码。
Services类
@Service
public class Services {
//登陆服务
@Autowired
User user;
@SuppressWarnings({ "deprecation", "unchecked" })
public User loginAuth(String username,String password) {
Session session = ((Hibernate)GlobleParams.ctx.getBean("hibernate")).getSession();
//查询不用事务管理
//Transaction transaction = session.beginTransaction();
Criteria c = session.createCriteria(User.class);
c.add(Restrictions.eq("username",username));
c.add(Restrictions.eq("password", password));
List<User> list=c.list();
session.close();
for(User user : list){
System.out.println(user.getUsername());
System.out.println(user.getPassword());
this.user = user;
}
return user;
//transaction.commit();
}

然后给出controller。
@Controller
public class Login {
/* 这里使用注解装配这个类,下面在函数里还有一个自己创建的services类,使用注解的时候,一会给出登陆两次的截图(第一次密码正确,第二次密码错误)
@Autowired
Services services;*/
@RequestMapping(value = "/home.do",method=RequestMethod.POST)
public String loginAuth(@RequestParam("UserName") String username, @RequestParam("Password") String password, Model model,HttpSession session) {
Services services = new Services();
System.out.println(username+":"+password);
User user = services.loginAuth(username, password);
System.out.println(user);
if(user!=null){
System.out.println(user.getUsername()+":"+user.getPassword());
return "home";
}else{
System.out.println("密码错误");
model.addAttribute("pw_error", "密码错误");
return "login";
}
}

下面看输出结果(这个是不用注解的正确实现功能的):
zfp:zfp
Hibernate:
select
this_.user_id as user_id1_0_0_,
this_.authentication_time as authenti2_0_0_,
this_.password as password3_0_0_,
this_.register_time as register4_0_0_,
this_.update_time as update_t5_0_0_,
this_.username as username6_0_0_
from
User this_
where
this_.username=?
and this_.password=?
zfp
zfp
com.skyline.goods.dao.User@3445a609
zfp:zfp
/GoodsSupply
aaa:aaa
Hibernate:
select
this_.user_id as user_id1_0_0_,
this_.authentication_time as authenti2_0_0_,
this_.password as password3_0_0_,
this_.register_time as register4_0_0_,
this_.update_time as update_t5_0_0_,
this_.username as username6_0_0_
from
User this_
where
this_.username=?
and this_.password=?
null
密码错误

这个是使用注解装配services的,第一次登陆进去之后,第二次错误的用户名密码并不会刷新User:
/GoodsSupply
zfp:zfp
Hibernate:
select
this_.user_id as user_id1_0_0_,
this_.authentication_time as authenti2_0_0_,
this_.password as password3_0_0_,
this_.register_time as register4_0_0_,
this_.update_time as update_t5_0_0_,
this_.username as username6_0_0_
from
User this_
where
this_.username=?
and this_.password=?
zfp
zfp
com.skyline.goods.dao.User@319886e3
zfp:zfp
/GoodsSupply
AAAAAAAAAAAA:AAAAAAAAAAAA
Hibernate:
select
this_.user_id as user_id1_0_0_,
this_.authentication_time as authenti2_0_0_,
this_.password as password3_0_0_,
this_.register_time as register4_0_0_,
this_.update_time as update_t5_0_0_,
this_.username as username6_0_0_
from
User this_
where
this_.username=?
and this_.password=?
com.skyline.goods.dao.User@319886e3
zfp:zfp

这他妹妹的是为啥?
...全文
399 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bcsflilong 2016-10-21
  • 打赏
  • 举报
回复
你注意配置注释的注入模式 有的时候可以是单例,有的地方不可以。
Mithiu 2016-10-20
  • 打赏
  • 举报
回复
你的数据库中有AAAAAAAAAAAAAAA:AAAAAAAAAAAAA这个数据吗,没有的话看逻辑好像还是返回原来的user
piterlin 2016-10-20
  • 打赏
  • 举报
回复
原理及解决办法: spring容器默认是使用singleton模式注入依赖类。singleton模式要求对像是不可变的(即没有实例变量),你把你Service中的User变量去调就可以了。 原因分析:当错误的用户名登录时,Hibernate查不出数据,loginAuth返回的依旧是第一次登录时的User对像。 原文: singleton (Default) Scopes a single bean definition to a single object instance per Spring IoC container. 参考:http://www.coderhelper.top/doc/spring4.3.3-docs/spring-framework-reference/html/beans.html#beans-factory-scopes-singleton
z1246879396 2016-10-20
  • 打赏
  • 举报
回复
这个的确是我表达的不到位,因为有两个人这样说了。 简单点说就是我使用这个的时候,结果有bug
@Autowired
    Services services;
我解释一下我的输出都代表什么。 最上面/GoodsSupply不用管是个目录。 紧接着 zfp:zfp(这个是我在前端输入的用户名和密码) 紧接着是hibernate的sql语句。 其实主要的是最后面一行的(zfp:zfp)这个代表我从数据库查出的user的username和password。 所以正确的情况应该是:最上面的展示前端输入的(zfp:zfp)和最后查询得到的(zfp:zfp)相等。 大家看第二个错误的情况即:我前端输入(AAAAAAAAAAAA:AAAAAAAAAAAA)查询得到(zfp:zfp)。 认真看的话应该能看出来吧。
sk815 2016-10-20
  • 打赏
  • 举报
回复
引用 2 楼 z1246879396 的回复:
[quote=引用 1 楼 sk815 的回复:] 你想表达啥···看不懂···还是想证明spring管理不好 自己写?
不是他管理不好,是这个错误,我看不明白为啥。[/quote] 什么问题不明白····第一贴没表达清楚·····
NewMoons 2016-10-20
  • 打赏
  • 举报
回复
楼主的表达能力有待提高,鉴定完毕!
z1246879396 2016-10-20
  • 打赏
  • 举报
回复
引用 1 楼 sk815 的回复:
你想表达啥···看不懂···还是想证明spring管理不好 自己写?
不是他管理不好,是这个错误,我看不明白为啥。
sk815 2016-10-20
  • 打赏
  • 举报
回复
你想表达啥···看不懂···还是想证明spring管理不好 自己写?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧