Spring4学习(二):创建项目 Hello Spring
2016-04-07 22:11
507 查看
一、创建eclipse项目,引入jar包
1、eclipse创建java project项目 HelloSpring
2、创建lib目录,加入spring必须的5个jar包
3、选中5个文件,右键 -> Build Path -> add to build path
二、编写spring的hello spring代码
1、创建包io.spring.beans,并编写HelloWorld.java
package io.spring.beans;
/**
* @author 胖胖のALEX E-mail:zanbin168@qq.com
* @date 2016年4月7日 下午10:26:12
* @version 1.0
*/
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello() {
System.out.println("hello " + name);
}
}
2、src右键 -> 创建spring bean configuration文件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.xsd">
<!-- 配置bean -->
<bean id="helloWorld" class="io.spring.beans.HelloWorld">
<property name="name" value="大红"></property>
</bean>
</beans>
3、编写Main.java
package io.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 胖胖のALEX E-mail:zanbin168@qq.com
* @date 2016年4月7日 下午10:29:45
* @version 1.0
*/
public class Main {
public static void main(String[] args) {
//1、创建Spring的IOC容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、从IOC容器中获取Bean实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//3、调用hello方法
helloWorld.hello();
}
}输出结果
当console内打印出红色spring日志,表示spring应用成功
----------------------------------------------------------------------------------------
下一节 spring注解配置bean
1、eclipse创建java project项目 HelloSpring
2、创建lib目录,加入spring必须的5个jar包
3、选中5个文件,右键 -> Build Path -> add to build path
二、编写spring的hello spring代码
1、创建包io.spring.beans,并编写HelloWorld.java
package io.spring.beans;
/**
* @author 胖胖のALEX E-mail:zanbin168@qq.com
* @date 2016年4月7日 下午10:26:12
* @version 1.0
*/
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello() {
System.out.println("hello " + name);
}
}
2、src右键 -> 创建spring bean configuration文件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.xsd">
<!-- 配置bean -->
<bean id="helloWorld" class="io.spring.beans.HelloWorld">
<property name="name" value="大红"></property>
</bean>
</beans>
3、编写Main.java
package io.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 胖胖のALEX E-mail:zanbin168@qq.com
* @date 2016年4月7日 下午10:29:45
* @version 1.0
*/
public class Main {
public static void main(String[] args) {
//1、创建Spring的IOC容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、从IOC容器中获取Bean实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//3、调用hello方法
helloWorld.hello();
}
}输出结果
当console内打印出红色spring日志,表示spring应用成功
----------------------------------------------------------------------------------------
下一节 spring注解配置bean
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 详解Java的Spring框架中的事务管理方式
- 解析Java的Spring框架的BeanPostProcessor发布处理器
- Java开发框架spring实现自定义缓存标签