spring 搭建

1 这个主要搭建spring环境

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();
}


HelloDaoImpl .java
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();
}


HelloServiceImpl.java

package spring.learn.service;

import spring.learn.dao.HelloDao;
import spring.learn.dao.HelloDaoImpl;

public class HelloServiceImpl implements HelloService {
	private HelloDao helloDao ;
	
	public HelloServiceImpl(){};
	public HelloServiceImpl(HelloDao helloDao){
		this.helloDao = helloDao;
	}
	
	public void sayHello(String username) {
		System.out.println(username);
		helloDao.sayHello();
		
	}

	public String getClassName() {
		return helloDao.getClass().getName();
	}
	public HelloDao getHelloDao() {
		return helloDao;
	}
	public void setHelloDao(HelloDao helloDao) {
		this.helloDao = helloDao;
	}

}


5  web.xml中加入spring监听  , 假设web.xml配置 classpath:applicationContext.xml  ,通过看文件目录,则用/WEB-INF /classes/ applicationContext.xml可以代替,

根据上图,最后将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监听开始 -->
  
	 <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监听开结束-->
  
 
  <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容器 就是负责创建、管理、维护Bean 并且能够依赖注入到相应组件上 -->  
   <bean id="helloDaoImpl" class="spring.learn.dao.HelloDaoImpl" scope="singleton" lazy-init="default"></bean>  
</beans>  


8 spring-service.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">  
  
   <bean id="helloServiceImpl" class="spring.learn.service.HelloServiceImpl" scope="singleton" lazy-init="false">  
      <property name="helloDao" ref="helloDaoImpl"/>  
   </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 ,结果如下图所示,说明已经成功从spring中取得bean了,配置成功.






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值