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
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了,配置成功.
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了,配置成功.
相关文章推荐
- 关于进制转换(java)
- 使用Java来实现String字符串和Byte[]数组的转换
- java异常总结
- Java插入MySQL中文出现乱码
- Spring控制反转(IOC)和依赖注入(DI),再记不住就去出家!
- Java API ——Scanner类
- java 对象引用与赋值
- Cucumber java + Webdriver (6) Cucumber的数据驱动
- Java substring()用法
- javaBean类的一些操作
- 关于spring4.0和hibernate4.3整合的时annotation的小常识
- java.lang.IllegalStateException: Fragement no longer exists for key f0: index 0
- SpringData JPA的学习笔记之环境搭建
- Spring随笔6
- Java Iterator与ListIterator的主要区别
- java连接mysql删除操作
- Java常用的设计模式13:常用设计模式之桥接模式(结构型模式)
- LT【深入Java虚拟机】之八:Java垃圾收集机制
- java 多线程 总结
- Java常用的设计模式12:常用设计模式之外观模式(结构型模式)