JSP学习笔记(十六):我的第一段Spring代码
哈哈,这是一段不能再简单的代码了!
我使用的是MyEclipse6.5,可以直接使用Spring,方法:新建一Web项目,菜单MyEclipse->Project Capabilities->Add Spring Capabilities...,我使用的是Spring2.0
添加一个c1.java类,这是一个普通的类,里面包含一个函数,返回一个字符串
package com.abc;
public class C1 {
public String getContent() {
return "Hello World!";
}
}
public class C1 {
public String getContent() {
return "Hello World!";
}
}
修改applicationContext.xml文件的内容,这个文件是系统建立的Spring配置文件
<?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.0.xsd">
<bean id="bean1" class="com.abc.C1">
</bean>
</beans>
<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.0.xsd">
<bean id="bean1" class="com.abc.C1">
</bean>
</beans>
建立第二个类C2.java,这个类用来调用C1.java类,注意,这里开始使用Spring了
package com.abc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class C2 {
public String getContent()
{
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
C1 c1 = (C1) factory.getBean("bean1");
return c1.getContent();
}
}
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class C2 {
public String getContent()
{
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
C1 c1 = (C1) factory.getBean("bean1");
return c1.getContent();
}
}
factory.getBean("bean1") 这里bean1,就是配置文件applicationContext.xml里的id,返回的结果是调用C1类的一个实例
下面再建立一个helloworld.jsp页面,来使用这个类
<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="com.abc.*" %>
<!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=GB2312">
<title>Insert title here</title>
</head>
<body>
<%
C2 c2 = new C2();
out.print(c2.getContent());
%>
</body>
</html>
<%@ page import="com.abc.*" %>
<!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=GB2312">
<title>Insert title here</title>
</head>
<body>
<%
C2 c2 = new C2();
out.print(c2.getContent());
%>
</body>
</html>
浏览器打开helloworld.jsp,网页显示结果为:Hello World!
第一段Spring应用的代码完成了!
以后我会对这个例子进行改进,Spring的这个功能本来是用来实现Ioc注入的,结果我用来动态的执行一个函数?好像算是吧,这里完全是对Spring的大材小用,嘿嘿:)

浙公网安备 33010602011771号