当前位置: 首页 > 知识库问答 >
问题:

创建名为“User Controller”的bean时出错:通过字段“User Service”表示了不满足的依赖关系

宗政子辰
2023-03-14
   **userController code**

        @Controller
        @RequestMapping("user")
        public class UserController {

        private UserService userService;

        @Autowired
        public void setUserService(UserService userService) {
            this.userService = userService;
        }



        //======== code for user data insert =============
        @GetMapping("/createuser")
        public String showUserform(Model theModel) {
            theModel.addAttribute("user", new User());
            return "userform";
        }

        //@RequestMapping(value = "/saveUser", method = RequestMethod.POST)
        @PostMapping("/saveuser")
        public String saveUser(@ModelAttribute("user") @Valid User user,BindingResult result) {

            if (result.hasErrors()) {
                return "userform";
            }

            user.setAuthority("ROLE_USER");
            user.setEnabled(true);

        return "usercreatesuccess";

        }
   **userservice code**

        @Service("userService")
        public class UserService {

        private UserDao userDao;

        @Autowired
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;`enter code here`
        }



        //======== code for user data insert =============
        public void saveUser(User user) {
            userDao.saveUser(user);
        }



        public boolean exists(String username) {
            return userDao.exists(username);
        }

   **userDao code**


         @Component("userDao")
         public class UserDao {

        private NamedParameterJdbcTemplate jdbcTemplate;

        /*
         * @Autowired private PasswordEncoder passwordEncoder;
         */

        @Autowired
        public void setJdbcTemplate(NamedParameterJdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }





         //======== query for user data insert =============
         public boolean saveUser(User user) {

         MapSqlParameterSource params = new MapSqlParameterSource();

            params.addValue("username", user.getUsername());
            params.addValue("password", user.getPassword());
            params.addValue("firstname", user.getFirstname());
            params.addValue("lastname", user.getLastname());
            params.addValue("email", user.getEmail());
            params.addValue("enabled", user.isEnabled());
            params.addValue("authority", user.getAuthority());

            jdbcTemplate.update("insert into users ( username, firstname, lastname, password, email, enabled) values ( :username, :firstname, :lastname, :password, :email, :enabled)", params);
            return jdbcTemplate.update("insert into authorities (username, authority) values (:username, :authority)", params) == 1;

        }

        // query for Duplicate username
        public boolean exists(String username) {
            return jdbcTemplate.queryForObject("select count(*) from users where username=:username", new MapSqlParameterSource("username", username), Integer.class) > 0;
        }
   **[blog-servlet.xml][1]**
        <!-- Add support for component scanning -->
        <context:component-scan base-package="com.spring.mvc.blog.xml" />


        <!-- Add support for conversion, formatting and validation support -->
        <mvc:annotation-driven/>

        <!-- Define Spring MVC view resolver -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            id="jspViewResolver">
            <property name="prefix" value="/WEB-INF/view/" />
            <property name="suffix" value=".jsp" />
        </bean>

        <!-- Add support for reading web resources: css, images, js, etc ... -->
        <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>

        <!-- connection for datasource -->
        <jee:jndi-lookup jndi-name="jdbc/spring" id="dataSource"
            expected-type="javax.sql.DataSource">
        </jee:jndi-lookup>

   **error** 

      [1]: https://i.stack.imgur.com/WZWVj.png

共有1个答案

仲孙钊
2023-03-14

applicationcontext无法初始化userDao bean。

UserDao依赖于NamedParameterJdbcTemplate

(通过方法“set jdbctemplate”参数0表示的不满足依赖关系;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionexception:没有“org.springframework.jdbc.core.namedparam.namedparameterjdbctemplate”类型的限定bean)

@Bean 
public NamedParameterJdbcTemplate namedParameterJdbcTemplate (DataSource datasource){
     return new NamedParameterJdbcTemplate(dataSource);
}
<bean id="namedParameterJdbcTemplate"  class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg ref="dataSource"/>
</bean>
 类似资料: