您的位置:首页 > 编程语言 > Java开发

spring 搭建

2015-11-03 19:55 543 查看
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了,配置成功.



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: