【Spring】Spring学习笔记-01-入门级实例


听说当前Spring框架很流行,我也准备好好学学Spring开发,并将学习的过程和大家分享,希望能对志同道合的同学有所帮助。

以下是我学习Spring的第一个样例。


1.Spring开发环境的搭建

我用的开发工具是MyEclipse 10,用maven管理jar包。Spring开发环境的搭建能够參考我的还有一篇文章:http://blog.csdn.net/xiaoguaihai/article/details/40428485


2.描写叙述

本实例的主要目的是利用Spring的配置文件applicationContext.xml来实现bean的依赖注入,终于通过jsp的显示结果来验证程序的正确性。


3.代码与截图

一下是本实例程序的相关代码和截图


3.1 project的文件夹结构

文件夹结构例如以下图所看到的:


主要有User类、TestUtil类这两个java类,一个jsp文件:index.jsp。一个配置文件:applicationContext.xml。


3.2 User类的代码

代码例如以下所看到的:

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


3.3 TestUtil类的代码

代码例如以下所看到的:

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


3.4 Spring配置文件applicationContext.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-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>


3.5 index.jsp的代码

代码例如以下所看到的:

<%@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>

4.执行结果

 最后执行结果例如以下图所看到的:


执行成功。这就是一个简单的Spring依赖注入的样例,希望对大家有所帮助。

谢谢。

原文地址:https://www.cnblogs.com/mengfanrong/p/5111151.html