专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Javaweb开发项目视频之Spring开发

Javaweb开发项目视频之Spring开发

更新时间:2020-06-23 12:17:54 来源:动力节点 浏览1729次

Web项目初始化SpringIoc容器

先新建一个Web项目,然后再导入开发项目必须的jar包,需要的jar包有:spring-java的6个jar和spring-web.jar。

导入jar包后我们要在项目的web.xml文件中配置spring-web.jar提供的监听器,该监听器可以在服务器启动时初始化Ioc容器。  

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

然后再用context-param告诉监听器Ioc容器的位置,在param-value里面通过classpath可指出要用哪些SpringIoc容器

<context-param>
 <!--监听器的父类ContextLoader中有一个属性contextConfigLocation,该属性值保存着配置文件xml的位置-->
 <param-name>contextConfigLocation</param-name>
 <param-value>
 classpath:applicationContext.xml,
 classpath:applicationContext-*
 </param-value>
 </context-param>

通过"*"可引入星号前面名称相同,但后面名称不同的所有SpringIoc容器。

<param-value>
 classpath:applicationContext-*
 </param-value>

 <!--上面代码等同于下面的代码-->
 
<param-value>
 classpath:applicationContext-Controller,
 classpath:applicationContext-Dao,
 classpath:applicationContext-Service
 </param-value>

拆分Spring配置文件

有两种拆分方法,一种是根据三层结构拆分,另一种是根据功能结构拆分。例如:

param-value>
<!--Servlet文件-->
 classpath:applicationContext-Controller,
 <!--Service文件-->
 classpath:applicationContext-Service,
 <!--Dao文件-->
 classpath:applicationContext-Dao
 </param-value>

就是根据三层结构,在一个xml配置文件中配置所有Servlet文件,在一个xml配置文件中配置所有Service文件,在一个xml配置文件中配置所有Dao文件。有时候还需要在一个xml配置文件中配置所有数据库文件。

从SpringIoc容器中获取数据

先建好各个层的文件,然后在SpringIoc容器中通过bean实例化每个层的对象。

<!--在Dao层的xml配置文件定义如下-->
<bean id="studentDao" class="dao.impl.StudentDaoImpl">
<!--在Service层的xml配置文件定义如下-->
<bean id="studentService" class="service.impl.StudentServiceImpl">
 <property name="studentDao" ref="studentDao"></property>
 </bean>
 
<!--在Controller层的xml配置文件定义如下-->
<bean id = "studentServlet" class="servlet.QueryStudentByIdServlet">
 <property name="studentService" ref="studentService"></property>
 </bean>

在web.xml配置文件中定义好运行Servlet文件所需的代码后,然后在Servlet文件通过重写init方法来获取bean,最后运行服务器即可。

@WebServlet(name = "QueryStudentByIdServlet")
public class QueryStudentByIdServlet extends HttpServlet {
 IStudentService studentService;
 //通过springioc容器的set注入将studentService 注入给Servlet
 public void setStudentService(IStudentService studentService) {
 this.studentService = studentService;
 }
 
 //servlet初始化方法:在初始化时,获取SpringIoc容器中的bean对象
 @Override
 public void init() throws ServletException {
 //在Web项目中用此方法获取Spring上下文对象,需要spring-web.jar
 ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
 //在Servlet容器中,通过getBean获取Ioc容器中的Bean
 studentService = (IStudentService) context.getBean("studentService");
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 }
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 String name = studentService.queryStudentById();
 request.setAttribute("name",name);
 request.getRequestDispatcher("result.jsp").forward(request,response);
 }
}

Javaweb项目视频下载

CRM项目:http://www.bjpowernode.com/javavideo/124.html

MVC架构:http://www.bjpowernode.com/javavideo/123.html

以上就是动力节点java培训机构的小编针对“Javaweb开发项目视频之Spring开发”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>