听说当前Spring框架非常流行,我也准备好好学学Spring开发,并将学习的过程和大家分享,希望能对志同道合的同学有所帮助。
下面是我学习Spring的第一个例子。
我用的开发工具是MyEclipse 10,用maven管理jar包,Spring开发环境的搭建可以参考我的另一篇文章:http://blog.csdn.net/xiaoguaihai/article/details/40428485
本实例的主要目的是利用Spring的配置文件applicationContext.xml来实现bean的依赖注入,最终通过jsp的显示结果来验证程序的正确性。
一下是本实例程序的相关代码和截图
目录结构如下图所示:
主要有User类、TestUtil类这两个java类,一个jsp文件:index.jsp,一个配置文件:applicationContext.xml。
代码如下所示:
package com.iscas.entity; public class User { private String name = "Wang"; private String sex = "男"; private int age = 25; private String tel = "010-88888888"; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } }
代码如下所示:
package com.iscas.util; import com.iscas.entity.User; public class TestUtil { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public boolean getUserInfo(){ if(user != null){ return true; } else { return false; } } }
代码如下所示:
<?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-2.5.xsd"> <!-- 配置User --> <bean id="user" class="com.iscas.entity.User"></bean> <!-- 配置TestUtil,注入User --> <bean id="testUtil" class="com.iscas.util.TestUtil"> <property name="user"> <ref local="user"/> </property> </bean> </beans>
代码如下所示:
<%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%> <%@page import="org.springframework.context.ApplicationContext"%> <%@page import="com.iscas.entity.User"%> <%@page import="com.iscas.util.TestUtil"%> <%@page import="org.springframework.beans.factory.xml.XmlBeanFactory"%> <%@page import="org.springframework.beans.factory.BeanFactory"%> <%@page import="org.springframework.core.io.ClassPathResource"%> <%@page import="org.springframework.core.io.Resource"%> <%@ page language="java" import="java.util.*" 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> <base href="<%=basePath%>"> <title>应用Setter注入法实现Bean的注入</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); TestUtil testUtil = (TestUtil)context.getBean("testUtil"); if(testUtil.getUserInfo()){ User user = testUtil.getUser(); %> 姓名:<%=user.getName() %><br> 性别:<%=user.getSex() %><br> 年龄:<%=user.getAge() %><br> 电话:<%=user.getTel() %><br> <% } %> </body> </html>
最后运行结果如下图所示:
运行成功,这就是一个简单的Spring依赖注入的例子,希望对大家有所帮助。谢谢。