spring快速开发搭建环境
2015-04-17 23:30
246 查看
1.加入相关的jar
2.buildpath
3.在src下建立类Hello 有一个属性 Name 有setName(作为一个bean)
new spring bean configuration file --finksh
建立一个bean
package com.my.beans;
public class HelloWord {
private String name;
public void setName1(String name) {
this.name = name;
}
public void Hello(){
System.out.println("hello"+name);
}
}
在应用配置文件中配置bean
<?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 id="hello" class="com.my.jd.Hello"> id bean的标识
<property name="name" value="zhangsan"></property>属性名字和值:就是bean的属性名和给属性赋值value
</bean> class 就是bean的全类名即Hello
</beans>
spring 就是通过class得到类名,用反射创建对象进行赋值
在测试类中写:
package com.my.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// 1.创建spring的IOC对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC中获取bean实例
HelloWord hello = (HelloWord) context.getBean("hello");
//3.调用hello方法
hello.Hello();
}
}
注意:classpath:是src下
2.buildpath
3.在src下建立类Hello 有一个属性 Name 有setName(作为一个bean)
new spring bean configuration file --finksh
建立一个bean
package com.my.beans;
public class HelloWord {
private String name;
public void setName1(String name) {
this.name = name;
}
public void Hello(){
System.out.println("hello"+name);
}
}
在应用配置文件中配置bean
<?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 id="hello" class="com.my.jd.Hello"> id bean的标识
<property name="name" value="zhangsan"></property>属性名字和值:就是bean的属性名和给属性赋值value
</bean> class 就是bean的全类名即Hello
</beans>
spring 就是通过class得到类名,用反射创建对象进行赋值
在测试类中写:
package com.my.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// 1.创建spring的IOC对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC中获取bean实例
HelloWord hello = (HelloWord) context.getBean("hello");
//3.调用hello方法
hello.Hello();
}
}
注意:classpath:是src下
相关文章推荐
- 快速搭建Spring+Mybatis开发环境
- 使用Spring boot快速搭建开发环境
- 搭建与测试Spring的开发环境
- 搭建基于SSI(struts2,spring,ibatis)的javaEE开发环境
- Spring4.0学习笔记001——搭建Spring开发环境
- 最快速的Android开发环境搭建ADT-Bundle及Hello World
- 快速搭建 Node.js 开发环境以及加速 npm
- 最快速的Android开发环境搭建ADT-Bundle及Hello World
- Spring+Maven+Dubbo+MyBatis+Linner+Handlebars—企业化Web开发环境搭建
- Android Ubuntu上NDK 开发环境快速搭建
- (1)搭建与测试 Spring 的开发环境
- 快速搭建Android开发环境ADT-Bundle及Hello World
- 快速搭建Android开发环境(支持win7,win8系统)
- Spring---搭建开发环境和第一个简单实例
- Win7环境如何快速搭建开发虚拟币交易平台
- Spring开发入门环境搭建篇
- 快速搭建 Node.js / io.js 开发环境以及加速 npm
- SpringMVC+Spring+Ibatis开发环境搭建
- Spring OSGi Eclipse开发环境的搭建
- 如何快速搭建java开发环境