【问题标题】:No bean named 'DAOBean' available没有可用的名为“DAOBean”的 bean
【发布时间】:2020-10-14 12:21:38
【问题描述】:

我正在尝试获取信息表单数据库并将其显示在网页上 我正在使用使用注释配置的spring jdbc,它给出了一个错误没有名为“DAOBean”的bean我检查了几次但无法解决它,帮助我

这是我的 AppController

 package shrikant.spring;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    import shrikant.spring.DAO.AppDAOImpl;
    import shrikant.spring.model.Users;
    
    @Controller
    public class AppController {
    
        @RequestMapping("/")
        public ModelAndView homepage() {
            ModelAndView modelAndView = new ModelAndView("index");
            List<Users> users = new ArrayList<Users>();
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("AppConfig.class");
            AppDAOImpl DAO = context.getBean("DAOBean",AppDAOImpl.class);
            users = DAO.listUsers();
            modelAndView.addObject("users", users);
            context.close();
            return modelAndView;
        }
    } 

这是我使用 Annotation 的 AppConfig.java 配置类

package shrikant.spring.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import shrikant.spring.DAO.AppDAOImpl;

@Configuration
public class AppConfig {

    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/project1");
        dataSource.setUsername("root");
        dataSource.setUsername("shrikant@123");
        return dataSource;
    }

    @Bean(name="DAOBean")
    public AppDAOImpl AppDAO() {
        return new AppDAOImpl(getDataSource());
    }
}

这是 AppDAOImpl.java

package shrikant.spring.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import shrikant.spring.model.Users;

public class AppDAOImpl implements AppDAO {

    private DataSource dataSource;

    public AppDAOImpl(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public List<Users> listUsers() {
        String SQL = "Select * from users";
        List<Users> listUsers = new ArrayList<Users>();
        Connection conn = null;
        try {
            conn = `dataSource.getConnection`();
            PreparedStatement ps = conn.prepareStatement(SQL);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Users temp = new Users(rs.getInt("users_id"), rs.getString("name"), rs.getString("email"));
                listUsers.add(temp);
            }
            rs.close();
            ps.close();
            return listUsers;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

这是模型类

package shrikant.spring.model;

public class Users {
    private int userId;
    private String name;
    private String email;

    @Override
    public String toString() {
        return "Users [userId=" + userId + ", name=" + name + ", email=" + email + "]";
    }

    public Users(int userId, String name, String email) {
        this.userId = userId;
        this.name = name;
        this.email = email;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

【问题讨论】:

    标签: java mysql spring spring-mvc


    【解决方案1】:

    当您调用new AnnotationConfigApplicationContext("AppConfig.class"); 时,您会创建一个新的应用程序上下文,但它已经存在。您必须使用 @Autowired 注解将 bean 注入您的控制器。

    一些提示:

    • 您可以在 AppDAOImpl 类上使用@Service 注解来创建 bean
    • 注入bean时使用接口

    AppDAOImpl

    package shrikant.spring.DAO;
    
    import org.springframework.stereotype.Service;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    import javax.sql.DataSource;
    import shrikant.spring.model.Users;
    
    @Service
    public class AppDAOImpl implements AppDAO {
    
        private DataSource dataSource;
    
        public AppDAOImpl(DataSource dataSource) {
            this.dataSource = dataSource;
        }
    
        @Override
        public List<Users> listUsers() {
            String SQL = "Select * from users";
            List<Users> listUsers = new ArrayList<Users>();
            Connection conn = null;
            try {
                conn = `dataSource.getConnection`();
                PreparedStatement ps = conn.prepareStatement(SQL);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    Users temp = new Users(rs.getInt("users_id"), rs.getString("name"), rs.getString("email"));
                    listUsers.add(temp);
                }
                rs.close();
                ps.close();
                return listUsers;
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    应用控制器

    package shrikant.spring.config;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    import shrikant.spring.DAO.AppDAOImpl;
    import shrikant.spring.DAO.AppDAO;
    import shrikant.spring.model.Users;
    
    @Controller
    public class AppController {
        
        private AppDao appDAOImpl;
        
        @Autowired
        public AppController(AppDao appDAOImpl) {
            this.appDAOImpl = appDAOImpl;
        }
        
        @RequestMapping("/")
        public ModelAndView homepage() {
            ModelAndView modelAndView = new ModelAndView("index");
            List<Users> users = new ArrayList<Users>();
            users = appDaoImpl.listUsers();
            modelAndView.addObject("users", users);
            return modelAndView;
        }
    } 
    

    应用配置

    package shrikant.spring.config;
    
    import javax.sql.DataSource;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    
    import shrikant.spring.DAO.AppDAOImpl;
    
    @Configuration
    public class AppConfig {
        
        @Bean
        public DataSource getDataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3306/project1");
            dataSource.setUsername("root");
            dataSource.setUsername("shrikant@123");
            return dataSource;
        }
    }
    

    【讨论】:

      【解决方案2】:
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      

      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("shrikant.spring.config");
      

      【讨论】:

      • 网页加载没有错误,但现在在控制台中抛出“用户'shrikant@123'@'localhost'的访问被拒绝(使用密码:NO)”
      【解决方案3】:

      你的代码

      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("AppConfig.class");
      

      匹配

      /**
           * Create a new AnnotationConfigApplicationContext, scanning for components
           * in the given packages, registering bean definitions for those components,
           * and automatically refreshing the context.
           * @param basePackages the packages to scan for component classes
           */
          public AnnotationConfigApplicationContext(String... basePackages){...
          }
      

      它将尝试扫描“AppConfig.class”路径但不注册它,因此上下文找不到您的 DAOBean。

      你可以尝试使用:

      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      

      【讨论】:

        【解决方案4】:
        I have gone through the code ,reason for it is App context is not able to register your component as bean.I have made changes and it is working .Datasource is getting created at app level all you have to is autowire it
        
        @Component
        public class AppDAOImpl implements AppDAO {
            
        
            private DataSource dataSource;
            @Autowired
            public AppDAOImpl(DataSource dataSource) {
                this.dataSource = dataSource;
            }
        
            @Override
            public List<Users> listUsers() {
                String SQL = "Select * from users";
                List<Users> listUsers = new ArrayList<Users>();
                Connection conn = null;
                try {
                    conn = dataSource.getConnection();
                    PreparedStatement ps = conn.prepareStatement(SQL);
                    ResultSet rs = ps.executeQuery();
                    while (rs.next()) {
                        Users temp = new Users(rs.getInt("users_id"), rs.getString("name"), rs.getString("email"));
                        listUsers.add(temp);
                    }
                    rs.close();
                    ps.close();
                    return listUsers;
                } catch (SQLException e) {
                    e.printStackTrace();
                    return null;
                } finally {
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        
        
        @Configuration
        public class AppConfig {
        
           // @Bean
            @Bean(name="DAOBean")
            public DataSource getDataSource() {
                DriverManagerDataSource dataSource = new DriverManagerDataSource();
                dataSource.setDriverClassName("com.mysql.jdbc.Driver");
                dataSource.setUrl("jdbc:mysql://localhost:3306/project1");
                dataSource.setUsername("root");
                dataSource.setUsername("shrikant@123");
                return dataSource;
            }
            /*
             * @Bean(name="DAOBean") public AppDAOImpl AppDAO() { return new
             * AppDAOImpl(getDataSource()); }
             */
        }
        
        @Controller
        public class AppController {
            @Autowired
            AppDAOImpl DAO;
        
            @RequestMapping("/")
            public ModelAndView homepage() {
                ModelAndView modelAndView = new ModelAndView("index");
                List<Users> users = new ArrayList<Users>();
                AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("AppConfig.class");
               // AppDAOImpl DAO = context.getBean("DAOBean",AppDAOImpl.class);
                users = DAO.listUsers();
                modelAndView.addObject("users", users);
                context.close();
                return modelAndView;
            }
        } 
        

        【讨论】:

          猜你喜欢
          • 2020-03-21
          • 2018-11-16
          • 1970-01-01
          • 1970-01-01
          • 2018-11-01
          • 2017-10-06
          • 2018-11-07
          • 1970-01-01
          • 2019-10-20
          相关资源
          最近更新 更多