Spring用 MyEclipse开发Spring简单实例
- 格式:doc
- 大小:306.00 KB
- 文档页数:5
1. 新建普通Java 项目MySpringT est. 这个过程无需赘述了, 建议建项目的时候将src 目录和
bin(或者classes)目录分开, 另外提示你切换透视图的时候一定要切换过去到Java 透视图, 此时默认会在Package Explorer 中选中刚才已经建好的J ava Project, 但是背景为灰色.
2. 首先单击一下左边的Package Explorer 中新建的MySpringT est 项目来使其高亮选中, 接着点击菜单项MyEclipse -> Add Spring Capabilities..., 接着会弹出对话框Add Spring Capabilities 提示你设置当前项目的Spring 属性.
对话框的第一页可以选择全部的Spring 框架, 这是最保险的做法, 不过我们的例子只需要选中Spring 2.0 Core Libraries 就可以了. 点击"Next" 继续.
第二页是Add Spring bean configuration file. 保持默认值不变就可以了. 最后点击Finish.
3. Spring 的开发没法自动生成Bean, 这里大家只好手工来写了, 也很简单. 分别复制下面的三个代码, 然后在MyEclipse src 目录上点击右键后选择菜单项 Paste 就可以生成J ava 类文件了.
public interface Action {
public String execute(String str);
}
public class UpperAction implem ents Action {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String string) {
message = string;
}
public String execute(String str) {
return (getMessage() + str).toUpperCase();
}
}
import org.springfram ework.context.ApplicationContext;
import org.springfram ework.context.support.ClassPathXmlApplicationContext;
public class T estAction {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Action bean = (Action) ctx.getBean("theAction");
System.out.println(bean.execute("Rod"));
}
}
4. 双击左侧在第2步生成的applicationContext.xml, 然后选择菜单项Window -> Show View -> Other..., 在弹出的对话框中选择MyEclipse Enterprise Workbench 节点下的Spring Beans 子节点打开视图Spring Beans. 此视图讲出现在主界面的右下侧.
5. 展开此视图中的MySpringTest 父节点, 并选中 src/applicationContext.xml 子节点, 在此节点上点击右键并选择弹出菜单项中的New Bean 来打开Create a new Spring bean 对话框, 并按照下图输入对应的内容.
Bean Id: [theAction]
Bean class: [UpperAction]
接下来请单击一下Tab 面板 Properties 并点击其中的Add... 按钮, 在接下来弹出的Property Wizard 对话框中按照下图输入/选择内容:
Name: [message]
Spring type: [value]
Type: [ng.String]
Value:[Hello_]
最后点击两次Finish 按钮关闭所有向导对话框. 然后点击菜单File -> Save. 此时可以看到applicationContext.xml 的内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-2.0.xsd">
<bean id="theAction" class="UpperAction" abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
<property name="message">
<value type="ng.String">Hello_</value>
</property>
</bean></beans>
然后双击Package Explorer 下MySpringT est/src/TestAction.java 打开源代码, 然后点击菜单Run -> Run As -> 1. Java Application, 如果没有错误的话将会出现如下的输入, 您的第一个Hello Spring 运行成功了:
log4j:WARN No appenders could be found for logger
(org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
HELLO_ROD
接着您就可以对着参考书继续创建类, 修改applicationContext.xml 做更多的练习了.
开发整合Hibernate 的关键操作点截图:
1. 在数据库浏览器中选择反向工程菜单;
2.对话框的选项说明。