javascript
【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
總結
以上是生活随笔為你收集整理的【Spring】Spring学习笔记-01-入门级实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 根据鼠标点击位置获取DataGridVi
- 下一篇: Kafka的消息格式