JAVA工程实现
运行结果
首先,要在src目录下创建目录lib,将spring相关jar包导入\src\lib目录下
配置文件hellobean.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 name="stu1" class="com.edu.HelloBean">
<property name="name" value="张三"></property>
<property name="course" value="数学"></property>
<property name="score" value="95"></property>
</bean>
<bean name="stu2" class="com.edu.HelloBean">
<property name="name" value="李四"></property>
<property name="course" value="物理"></property>
<property name="score" value="88"></property>
</bean>
<bean name="stu3" class="com.edu.HelloBean">
<property name="name" value="王五"></property>
<property name="course" value="计算机"></property>
<property name="score" value="90"></property>
</bean>
</beans>
Main函数
package com.edu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext act=new ClassPathXmlApplicationContext("hellobean.xml");
HelloBean student;
student=(HelloBean)act.getBean("stu1");
System.out.println("name:"+student.getName()+"course:"+student.getCourse()+"score:"+student.getScore());
student=(HelloBean)act.getBean("stu2");
System.out.println("name:"+student.getName()+"course:"+student.getCourse()+"score:"+student.getScore());
student=(HelloBean)act.getBean("stu3");
System.out.println("name:"+student.getName()+"course:"+student.getCourse()+"score:"+student.getScore());
}
}
HelloBean.java
package com.edu;
public class HelloBean {
private String name;
private String course;
private double score;
public HelloBean() {
super();
}
public HelloBean(String name, String course, double score) {
super();
this.name = name;
this.course = course;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
web工程实现 运行结果 首先,记得在lib下导入spring相应jar包
实体类与配置文件均与上述java工程中的一致
web.xml 尝试了好多次都运行不起来,原因是在< param-value >标签里路径有问题识别不出来,刚开始写的是/src/hellobean.xml,一直显示error,后来改成了classpath:hellobean.xml就好了
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>spring-web-test</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hellobean.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.edu.HelloBean" %>
<%@page import="org.springframework.web.context.WebApplicationContext" %>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring应用案例</title>
</head>
<body>
<%
ServletContext sc=request.getServletContext();
WebApplicationContext wact;
wact=WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
HelloBean student;
student=(HelloBean)wact.getBean("stu1");
%>
姓名:<%=student.getName() %><br>
课程名:<%=student.getCourse() %><br>
成绩:<%=student.getScore() %><br>
------------------------------<br>
<%
student=(HelloBean)wact.getBean("stu2");
%>
姓名:<%=student.getName() %><br>
课程名:<%=student.getCourse() %><br>
成绩:<%=student.getScore() %><br>
------------------------------<br>
<%
student=(HelloBean)wact.getBean("stu3");
%>
姓名:<%=student.getName() %><br>
课程名:<%=student.getCourse() %><br>
成绩:<%=student.getScore() %><br>
------------------------------<br>
</body>
</html>
这点东西又花了我一天时间,刚开始是spring插件安装一直失败,后来是运行又出现问题,搞到了这个点也是不容易,心疼的抱住自己。
|