Java 没有名为“DAOBean”的bean可用

Java 没有名为“DAOBean”的bean可用,java,mysql,spring,spring-mvc,Java,Mysql,Spring,Spring Mvc,我正在尝试从数据库中获取信息并将其显示在网页上 我正在使用SpringJDBC使用注释配置它,它给出了一个错误,没有名为DAOBean的bean可用。我检查了好几次,但无法解决它,请帮助我 这是我的AppController package shrikant.spring; import java.util.ArrayList; import java.util.List; import org.springframework.context.ann

我正在尝试从数据库中获取信息并将其显示在网页上 我正在使用SpringJDBC使用注释配置它,它给出了一个错误,没有名为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;
        }
    } 
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;
    }
} 
这是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();
                }
            }
        }
    }
}
调用新的AnnotationConfigApplicationContextAppConfig.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();
                }
            }
        }
    }
}

你的代码

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

您可以尝试使用:


网页加载时没有错误,但现在在控制台中,它正在抛出拒绝用户访问的请求shrikant@123“@'localhost'正在使用密码:否
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;
    }
}
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("shrikant.spring.config");
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){...
    }
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
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;
    }
}