Get the App
SLTechnology News&Howtos  ›  Database  › 

Null when Spring is injected into Bean

Shulou Source: shulou.com Published: 2022-06-01 06:59:48 09月27日 Update

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 {

/ / the line reported an error loginService as null

User u = loginService.checkInfo (user)

HttpSession session = request.getSession ()

Session.setAttribute ("nickname", u.getNickname ())

Return "index"

}

}

Dao interface:

Public interface LoginDao {

/ / find User according to email

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

@ Service

Public interface LoginService {

Public User checkInfo (User user) throws Exception

}

Service implementation class:

@ 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) {

/ / user name error

Throw new EmailErrorException ("user name error")

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

/ / incorrect password

Throw new PasswordErrorException ("password error")

} else {

/ / user name and password are correct

Return u

}

}

}

Tags: Error password user user name interface Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi Linux Xiaomi vpn