Java Spring的配置以及第一个HelloWorld
2014-10-23 19:24
387 查看
第一个spring程序的配置以及过程中遇到的问题:
1.首先你的Eclipse已经配置好
2. 首先是Spring-tool-suite的安装(我的版本是springsource-tool-suite-
3.6.2.RELEASE-e3.8.2-updatesite.zip稍后会给出网盘链接)
2.1 首先 help--->Install New Software--->Add添加你的spring-tool-suite(此时
会在中间的列表框中进行选择, 你只需要选择Spring-IDE结尾的所有选项)--->在下面
的单选列表中将Contact all那个去掉,防止在线更新---->点击下一步,下一步即可
最后在Perferences中看见你的Spring, 基本上你的就安装完成了
3.此时导入Spring所以依赖的包
commons-logging-1.1.3.jar
spring-beans-4.0.5.RELEASE.jar
spring-context-4.0.5.RELEASE.jar
spring-core-4.0.5.RELEASE.jar
spring-expression-4.0.5.RELEASE.jar
下载链接后面会给出来
4.现在我们写一个简单的HelloWorld程序
HelloWorld.java
public class HelloWorld {
private String name;
public HelloWorld() {
System.out.println("This is a constructor function");
}
public String getName(){
return "Hello: "+name;
}
public void setName(String name){ //这里定义成public方法
this.name = name;
}
}
Main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//HelloWorld he = new HelloWorld();
//he.setString("Jack");
//System.out.println(he.getString());
//调用spring方法
//1. 创建spring的IOC 容器对象
ApplicationContext act = new ClassPathXmlApplicationContext
("Application.xml");
//2. 从IOC容器中获取Bean实例
HelloWorld he = (HelloWorld)act.getBean("helloWorld");
//3. 调用方法
System.out.println(he.getName());
}
}
这是在当前文件夹下建立的xml配置文件
Application.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的配置 -->
<bean id="helloWorld" class="SMI.HelloWorld">
<!-- <property name="name" value="Jack"></property> -->
<property name="name" value="Jack"></property> <!-- 这里的
name代表的setName方法 切记 -->
</bean>
</beans>
好了,一个简单的基于Spring的HelloWorld程序就建成了
1.首先你的Eclipse已经配置好
2. 首先是Spring-tool-suite的安装(我的版本是springsource-tool-suite-
3.6.2.RELEASE-e3.8.2-updatesite.zip稍后会给出网盘链接)
2.1 首先 help--->Install New Software--->Add添加你的spring-tool-suite(此时
会在中间的列表框中进行选择, 你只需要选择Spring-IDE结尾的所有选项)--->在下面
的单选列表中将Contact all那个去掉,防止在线更新---->点击下一步,下一步即可
最后在Perferences中看见你的Spring, 基本上你的就安装完成了
3.此时导入Spring所以依赖的包
commons-logging-1.1.3.jar
spring-beans-4.0.5.RELEASE.jar
spring-context-4.0.5.RELEASE.jar
spring-core-4.0.5.RELEASE.jar
spring-expression-4.0.5.RELEASE.jar
下载链接后面会给出来
4.现在我们写一个简单的HelloWorld程序
HelloWorld.java
public class HelloWorld {
private String name;
public HelloWorld() {
System.out.println("This is a constructor function");
}
public String getName(){
return "Hello: "+name;
}
public void setName(String name){ //这里定义成public方法
this.name = name;
}
}
Main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//HelloWorld he = new HelloWorld();
//he.setString("Jack");
//System.out.println(he.getString());
//调用spring方法
//1. 创建spring的IOC 容器对象
ApplicationContext act = new ClassPathXmlApplicationContext
("Application.xml");
//2. 从IOC容器中获取Bean实例
HelloWorld he = (HelloWorld)act.getBean("helloWorld");
//3. 调用方法
System.out.println(he.getName());
}
}
这是在当前文件夹下建立的xml配置文件
Application.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的配置 -->
<bean id="helloWorld" class="SMI.HelloWorld">
<!-- <property name="name" value="Jack"></property> -->
<property name="name" value="Jack"></property> <!-- 这里的
name代表的setName方法 切记 -->
</bean>
</beans>
好了,一个简单的基于Spring的HelloWorld程序就建成了
相关文章推荐
- Java环境配置以及第一个小程序HelloWorld
- 【JAVA初学者】配置JDK的详细过程,以及第一个JAVA程序的编写。
- 关于NDK的配置以及第一个应用程序(android中java语言调用c/c++)
- java 使用memcached以及spring 配置memcached完整实例代码
- Java Servlet + Spring 配置中所遇问题以及处理
- springboot打包jar包找不到jsp文件以及在linux永久执行java -jar以及springboot的多环境配置
- java Android SDK安装与环境变量配置以及开发第一个Android程序
- Java Android SDK安装与环境变量配置以及开发第一个Android程序
- scala对redis集群操作工具类 附带java版本的工具类以及spring配置
- Eclipse中Spring环境配置以及跑个Helloworld
- idea+Spring Boot的第一个应用,以及热部署的配置
- JDK安装、配置以及编译运行我第一个JAVA程序
- Linux下Java开发环境搭建以及第一个HelloWorld
- 在tomcat里面配置数据库地址,以及在Spring和Java中的使用
- Eclipse3.5下的struts2的配置,以及建立第一个struts项目HelloWorld
- 配置Java连接池的两种方式:tomcat方式以及spring方式
- Java maven项目的搭建,配置web框架以及Spring
- java Android SDK安装与环境变量配置以及开发第一个Android程序
- eclipse 中创建 整合springmvc+mybatis+maven 的java web项目步骤,以及maven仓库配置
- 第一个Spring JavaConfig注解配置bean