Spring注入Bean时为null

Spring.xml

创新互联是一家专注于成都网站设计、网站制作和成都移动服务器托管的网络公司,有着丰富的建站经验和案例。

jdbc.properties

driver=com.MySQL.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/xmscode?useUnicode=true&characterEncoding=utf8

user=root

password=1234

controller:

@Controller

@RequestMapping("login")

public class LoginController {

private LoginService loginService;

public LoginService getLoginService() {

return loginService;

}

public void setLoginService(LoginService loginService) {

this.loginService = loginService;

}

@RequestMapping("tologin")

public String toLogin(){

return "login";

}

@RequestMapping("login")

public String login(User user, HttpServletRequest request) throws Exception{

//该行报错   loginService为null

                User u = loginService.checkInfo(user);

HttpSession session = request.getSession();

session.setAttribute("nickname", u.getNickname());

return "index";

}

}

dao接口:

public interface LoginDao {

//根据email查找User

public User findUserByEmail(User user) throws Exception;

}

daoImpl:

@Repository

public class LoginDaoImpl implements LoginDao {

private JdbcTemplate jdbcTemplate;

public JdbcTemplate getJdbcTemplate() {

return jdbcTemplate;

}


public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

this.jdbcTemplate = jdbcTemplate;

}


public User findUserByEmail(User user) throws Exception {

User u = null;

String sql = "select * from xc_user where email=?";

Object[] params = new Object[]{user.getEmail()};

u = jdbcTemplate.queryForObject(sql, (RowMapper) new User(), params);

return u;

}

}

service接口:

@Service

public interface LoginService {

public User checkInfo(User user) throws Exception;

}

service实现类:

@Service

public class loginServiceImpl implements LoginService{


private LoginDao loginDao;

public LoginDao getLoginDao() {

return loginDao;

}


public void setLoginDao(LoginDao loginDao) {

this.loginDao = loginDao;

}


public User checkInfo(User user) throws Exception{

System.out.println("loginDao"+loginDao);

User u = null;

u = loginDao.findUserByEmail(user);

if(u==null){

//用户名错误

throw new EmailErrorException("用户名错误");

}else if(!u.getPassword().equals(user.getPassword())){

//密码错误

throw new PasswordErrorException("密码错误");

}else{

//用户名和密码正确

return u;

}

}

}


本文题目:Spring注入Bean时为null
本文来源:http://bzwzjz.com/article/jgcjjc.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: LED网站设计方案 企业网站制作 重庆网站设计 网站制作 教育网站设计方案 企业网站建设 高端网站设计推广 网站建设方案 成都网站设计 响应式网站设计 品牌网站建设 成都网站制作 网站建设方案 手机网站建设套餐 成都企业网站建设 成都网站建设 四川成都网站建设 成都网站制作 成都网站设计 成都网站制作 响应式网站建设 网站制作公司