spring 搭建
1 這個主要搭建spring環(huán)境
2 需要的jar包
3 整個項目結構圖,新建configResouce , 將配置文件和java代碼徹底分離
4 如上圖所示 ?java代碼有分為 dao , service ,junit (dao,service) 采用分層思想
dao層的代碼如下
HelloDao.java
package spring.learn.dao;public interface HelloDao {public void sayHello(); }
package spring.learn.dao;public class HelloDaoImpl implements HelloDao {public HelloDaoImpl(){};public void sayHello() {System.out.println("dao say hello");}}service層代碼如下
HelloService.java
package spring.learn.service;import spring.learn.dao.HelloDao;public interface HelloService {public void sayHello(String username);public String getClassName(); }
5 ?web.xml中加入spring監(jiān)聽 ?, 假設web.xml配置 classpath:applicationContext.xml ?,通過看文件目錄,則用/WEB-INF?/classes/?applicationContext.xml可以代替,
根據(jù)上圖,最后將spring.xml在web.xml中配置為classpath:xml/spring/spring.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"><display-name></display-name> <!-- 配置spring監(jiān)聽開始 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:xml/spring/spring.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置spring監(jiān)聽開結束--><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app>6 spring.xml的代碼如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <import resource="spring-dao.xml"/> <import resource="spring-service.xml"/> </beans>
7 ?spring-dao.xml的代碼如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!-- spring容器 就是負責創(chuàng)建、管理、維護Bean 并且能夠依賴注入到相應組件上 --> <bean id="helloDaoImpl" class="spring.learn.dao.HelloDaoImpl" scope="singleton" lazy-init="default"></bean> </beans>
9 index.jsp代碼如下 ,因為bean交給spring管理,所有必須得啟動web瀏覽器 <%@ page language="java" import="java.util.*,org.springframework.context.ApplicationContext,org.springframework.context.support.ClassPathXmlApplicationContext,spring.learn.service.HelloServiceImpl" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>My JSP 'index.jsp' starting page</title></head><body><%ApplicationContext context = new ClassPathXmlApplicationContext("classpath:xml/spring/spring.xml"); HelloServiceImpl helloServiceImpl = (HelloServiceImpl)context.getBean("helloServiceImpl");%>sayHello method test = <%helloServiceImpl.sayHello("zhangsan");%></br>getClassName = <%=helloServiceImpl.getClassName()%></body> </html>
10 訪問http://localhost:8080/Spring/index.jsp ,結果如下圖所示,說明已經(jīng)成功從spring中取得bean了,配置成功.
總結
- 上一篇: UML各种线的含义
- 下一篇: 从程序员到CTO的Java技术路线图 (