我猜您对 Java、JEE、Spring 和 JDBC 完全陌生。正如我在评论中所说,如果你在那里所做的事情的基础是不正确的,那么很难回答你的问题。我将尝试讨论几个主题,希望也能指出您当前的问题所在。
Spring应用程序结构
您需要确保正确构建您的项目:
-
src
-
main
-
java
- directory for Java sources
-
in/mmali/springtest/controller/IndexController.java
- 你的控制器类
-
resources
- 非 Java(重新)源目录
-
webapp
- root for the web application resources
-
WEB-INF/web.xml
- JEE Web应用程序配置
-
WEB-INF/spring-servlet.xml
- 调度程序 servlet 的应用程序上下文配置
-
pom.xml
- Maven 配置(如果您使用 Maven)
我将其称为 Java 项目的通用结构,大部分由 Maven“标准化”。
正确的 JEE 配置
你需要有正确的web.xml
配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是一个基本配置(没有根上下文),它将使用您的spring-servlet.xml
作为 Spring 上下文配置。
正确的 Spring 配置
您需要有正确的 Spring 上下文配置:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**" />
<!-- With ROOT context we would restrict component scan on controllers here -->
<context:component-scan base-package="in.mmali.springtest" />
<!-- Data source configuration would normally go inside ROOT context. -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cjbranchdb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
这将加载所有带有注释的类@Component
(及其同伴@Controller
, @Service
, @Repository
)作为你的豆子。Bean在 Spring 应用程序的上下文中,是由 Spring 管理的对象 -> 即由 Spring 本身实例化的对象。当您想要使用 Spring bean 时,您需要将其注入(例如,通过使用@Autowired
注释)或者你需要将其从ApplicationContext#getBean
手动。
使用 JDBC
使用 JDBC 是一件很痛苦的事情可关闭的资源和已检查的异常。这就是 Spring-JDBC 项目包装 JDBC API 的原因,这样您就不必使用它。
为了展示如何使用 JDBC 以及如何让 Spring 注入依赖项,这里有一个简单的控制器:
@Controller // Will be detected by <context:component-scan>
@RequestMapping // Will be detected by <mvc:annotation-driven> (more specifically by one of its component - RequestMappingHandlerMapping)
public class IndexController {
@Autowired // Spring will inject JdbcTemplate here
private JdbcOperations jdbcOperations;
@RequestMapping // This method should be called for requests to "/"
@ResponseBody // Returned string will be returned to client... normally you would register view resolver and just return name of a JSP to render
public String renderIndex() {
// You don't need to worry about JDBC's DataSource, Connection, ResultSet, ... just use JdbcTemplate
long rowCount = jdbcOperations.queryForLong("SELECT COUNT(*) FROM my_test_table;");
return "Number of rows in database is: " + String.valueOf(rowCount);
}
}
请注意,在实际应用程序中,您不会允许控制器直接使用数据源,而是通过服务和数据层。
下一步
-
开始使用日志系统并且从不使用
System.out.println
再次在网络应用程序中;)。我建议slf4j其简单的启动绑定(稍后您可以将其配置为使用logback or log4j).
-
配置您的应用程序以使用事务。使用 Spring 的事务处理(
<tx:annotation-driven/>
with @Transactional
)。一开始它可能看起来很神奇,但是当您发现一些有关 AOP 和代理类的内容时,您就会开始真正欣赏 Spring 的工作原理。
-
拆分您的应用程序逻辑到服务层和数据(DAO)层。
-
检查 Spring 的示例应用程序 (http://docs.spring.io/docs/petclinic.html http://docs.spring.io/docs/petclinic.html)和参考应用(https://github.com/spring-projects/greenhouse https://github.com/spring-projects/greenhouse).