Spring-MVC 中的 Bean 创建异常
Bean Creation Exception in Spring-MVC
这是我的代码和我得到的异常,早些时候这个项目工作正常但是当我在其中添加 MySQL 连接和 Bean class 时,我得到了异常。
Jul 03, 2017 12:54:40 AM org.springframework.web.servlet.DispatcherServlet initServletBean
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: metalist.yashdeep.scrap.service.IStockDetailsService metalist.yashdeep.scrap.controller.DefaultController.iStockDetailsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iStockDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: metalist.yashdeep.scrap.dao.IStockDetailsDAO metalist.yashdeep.scrap.service.StockDetailsService.iStockDetailsDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stockDetailsDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: metalist.yashdeep.scrap.resource.DatabaseConnection metalist.yashdeep.scrap.dao.StockDetailsDAO.databaseConnection; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [metalist.yashdeep.scrap.resource.DatabaseConnection] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1269)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1182)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1072)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5368)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5660)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MetalistEnterprises</groupId>
<artifactId>MetalistEnterprises</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring.version>4.0.3.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MetalistEnterprises</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
控制器
@Controller
public class DefaultController {
@Autowired
IStockDetailsService iStockDetailsService;
@RequestMapping(value = "/stock")
public ModelAndView stockMethod(Model model) {
ArrayList<StockBean> stocksList = iStockDetailsService.getAllStocks();
model.addAttribute("stocksList", stocksList);
return new ModelAndView("stock");
}
服务
@Service("iStockDetailsService")
public class StockDetailsService implements IStockDetailsService {
@Autowired
IStockDetailsDAO iStockDetailsDAO;
@Override
public ArrayList<StockBean> getAllStocks() {
System.out.println("In Service, getAllStocks methods");
ArrayList<StockBean> stocksList = iStockDetailsDAO.getAllStocks();
return stocksList;
}
豆子
public class StockBean {
private Integer idStock;
private String invoiceNumber;
private Date invoiceDate;
private String companyName;
private String materialDescription;
private float rate;
private float amount;
private float tax;
Getter and Settters
}
mvc-调度程序-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:component-scan base-package="metalist.yashdeep.scrap" />
<context:annotation-config />
<mvc:resources mapping="/resource/**" location="/resource/" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
IStockDetailsDAO
import java.util.ArrayList;
import metalist.yashdeep.scrap.model.StockBean;
public interface IStockDetailsDAO {
public ArrayList<StockBean> getAllStocks();
}
StockDetailsDAO
@Repository
public class StockDetailsDAO implements IStockDetailsDAO {
@Autowired
DatabaseConnection databaseConnection;
@Autowired
DatabaseQuery databaseQuery;
@Override
public ArrayList<StockBean> getAllStocks() {
System.out.println("In DAO, getAllStocks methods");
Connection conn = databaseConnection.getConnection();
try {
ArrayList<StockBean> arrayListStockBean = new ArrayList<StockBean>();
String getAllQueries = databaseQuery.queryGetAllStocks;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(getAllQueries);
while (rs.next()) {
StockBean stockBean = new StockBean();
stockBean.setIdStock(rs.getInt("idStock"));
stockBean.setInvoiceNumber(rs.getString("invoiceNumber"));
stockBean.setInvoiceDate(rs.getDate("invoiceDate"));
stockBean.setCompanyName(rs.getString("CompanyName"));
stockBean.setMaterialDescription(rs.getString("materialDescription"));
stockBean.setRate(rs.getFloat("rate"));
stockBean.setAmount(rs.getFloat("amount"));
stockBean.setTax(rs.getFloat("tax"));
arrayListStockBean.add(stockBean);
}
return arrayListStockBean;
} catch (Exception ex) {
return null;
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
数据库连接
package metalist.yashdeep.scrap.resource;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
public Connection getConnection() {
System.out.println("In getConnection");
try {
System.out.println("Trying for Connection For NEOs");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/metalistenterpriseschema",
"root", "root");
System.out.println("Connection Successful For NEOs");
return conn;
} catch (Exception e) {
System.out.println("Exception in getConnection. Exception is : " + e);
return null;
}
}
}
您可能没有用 @Component
注释 metalist.yashdeep.scrap.resource.DatabaseConnection
class,所以 spring 容器没有识别您的依赖 class
这是我的代码和我得到的异常,早些时候这个项目工作正常但是当我在其中添加 MySQL 连接和 Bean class 时,我得到了异常。
Jul 03, 2017 12:54:40 AM org.springframework.web.servlet.DispatcherServlet initServletBean
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: metalist.yashdeep.scrap.service.IStockDetailsService metalist.yashdeep.scrap.controller.DefaultController.iStockDetailsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iStockDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: metalist.yashdeep.scrap.dao.IStockDetailsDAO metalist.yashdeep.scrap.service.StockDetailsService.iStockDetailsDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stockDetailsDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: metalist.yashdeep.scrap.resource.DatabaseConnection metalist.yashdeep.scrap.dao.StockDetailsDAO.databaseConnection; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [metalist.yashdeep.scrap.resource.DatabaseConnection] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1269)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1182)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1072)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5368)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5660)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MetalistEnterprises</groupId>
<artifactId>MetalistEnterprises</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring.version>4.0.3.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MetalistEnterprises</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
控制器
@Controller
public class DefaultController {
@Autowired
IStockDetailsService iStockDetailsService;
@RequestMapping(value = "/stock")
public ModelAndView stockMethod(Model model) {
ArrayList<StockBean> stocksList = iStockDetailsService.getAllStocks();
model.addAttribute("stocksList", stocksList);
return new ModelAndView("stock");
}
服务
@Service("iStockDetailsService")
public class StockDetailsService implements IStockDetailsService {
@Autowired
IStockDetailsDAO iStockDetailsDAO;
@Override
public ArrayList<StockBean> getAllStocks() {
System.out.println("In Service, getAllStocks methods");
ArrayList<StockBean> stocksList = iStockDetailsDAO.getAllStocks();
return stocksList;
}
豆子
public class StockBean {
private Integer idStock;
private String invoiceNumber;
private Date invoiceDate;
private String companyName;
private String materialDescription;
private float rate;
private float amount;
private float tax;
Getter and Settters
}
mvc-调度程序-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:component-scan base-package="metalist.yashdeep.scrap" />
<context:annotation-config />
<mvc:resources mapping="/resource/**" location="/resource/" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
IStockDetailsDAO
import java.util.ArrayList;
import metalist.yashdeep.scrap.model.StockBean;
public interface IStockDetailsDAO {
public ArrayList<StockBean> getAllStocks();
}
StockDetailsDAO
@Repository
public class StockDetailsDAO implements IStockDetailsDAO {
@Autowired
DatabaseConnection databaseConnection;
@Autowired
DatabaseQuery databaseQuery;
@Override
public ArrayList<StockBean> getAllStocks() {
System.out.println("In DAO, getAllStocks methods");
Connection conn = databaseConnection.getConnection();
try {
ArrayList<StockBean> arrayListStockBean = new ArrayList<StockBean>();
String getAllQueries = databaseQuery.queryGetAllStocks;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(getAllQueries);
while (rs.next()) {
StockBean stockBean = new StockBean();
stockBean.setIdStock(rs.getInt("idStock"));
stockBean.setInvoiceNumber(rs.getString("invoiceNumber"));
stockBean.setInvoiceDate(rs.getDate("invoiceDate"));
stockBean.setCompanyName(rs.getString("CompanyName"));
stockBean.setMaterialDescription(rs.getString("materialDescription"));
stockBean.setRate(rs.getFloat("rate"));
stockBean.setAmount(rs.getFloat("amount"));
stockBean.setTax(rs.getFloat("tax"));
arrayListStockBean.add(stockBean);
}
return arrayListStockBean;
} catch (Exception ex) {
return null;
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
数据库连接
package metalist.yashdeep.scrap.resource;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
public Connection getConnection() {
System.out.println("In getConnection");
try {
System.out.println("Trying for Connection For NEOs");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/metalistenterpriseschema",
"root", "root");
System.out.println("Connection Successful For NEOs");
return conn;
} catch (Exception e) {
System.out.println("Exception in getConnection. Exception is : " + e);
return null;
}
}
}
您可能没有用 @Component
注释 metalist.yashdeep.scrap.resource.DatabaseConnection
class,所以 spring 容器没有识别您的依赖 class