spring的HelloWorld<01>
来源:互联网 发布:淘宝能买到客户资料呢 编辑:程序博客网 时间:2024/06/07 14:19
首先我们先来写个最简单的HelloWorld:
1.加入JAR 包。
2.创建一个简单的实体类。
3.在spring 的配置文件中配置实体类的bean.
4.单元测试。
c3p0-0.9.1.2.jarcommons-logging-1.1.1.jarmysql-connector-java-5.1.7-bin.jarspring-beans-4.0.0.RELEASE.jarspring-context-4.0.0.RELEASE.jarspring-core-4.0.0.RELEASE.jarspring-expression-4.0.0.RELEASE.jar
package com.o1;public class Book {private String id;private String name;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Book(String id, String name) {super();this.id = id;this.name = name;}public Book() {System.out.println("ioc执行之前先进行创建对象");}@Overridepublic String toString() {return "Book [id=" + id + ", name=" + name + "]";}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 配置一个bean : id属性:用来唯一的标识一个bean 的实例。 class 属性:用于指定bean 的实现类的全类名 --> <bean id="book" class="com.o1.Book"> <!-- 使用property子标签为bean的属性赋值 --><!-- name属性:用于指定目标属性名 --><!-- value属性:用于指定属性值 --> <property name="id" value="01"/> <property name="name" value="哈利波特"/> </bean></beans>
package com.o1;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test01 {@Testpublic void test01() {//创建IOC 容器ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");Book book = (Book) ioc.getBean("book");System.out.println(book);/** * ioc执行之前先进行创建对象 * Book [id=01, name=哈利波特] */}}
接下来我们来分析IOC (控制反转)?
1 0
- spring的HelloWorld<01>
- Spring的HelloWorld
- Spring 的Helloworld
- spring的HelloWorld
- Spring的HelloWorld
- spring的helloworld
- Spring-01-HelloWorld
- Spring-01-HelloWorld
- Spring-01--HelloWorld
- Spring.Net 的简单HelloWorld
- 最简单的Spring HelloWorld
- Spring MVC版本的helloworld
- spring的入门之helloworld
- Spring学习(二):Spring的HelloWorld
- spring mvc 5.0 HelloWorld 01
- Spring helloworld
- spring helloworld
- spring helloworld
- HDU 5775 Bubble Sort (多校4)
- iOS实战演练之——UINavigationBar滑动问题
- 准备写:创建对象时的内存分配
- IOS Dev Intro - Third Party Library Process
- GetLastError的返回值意思
- spring的HelloWorld<01>
- 题目275 队花的烦恼一
- vim命令集合
- Swift基础(三十二)获取相册中的图片
- 华为OJ 初级:百钱买百鸡问题
- hdu3592 差分约束问题
- iOS 延时操作
- 防止表单重复提交专题3-1:外刊IT评论:防止表单重复提交的几种策略
- ios屏幕尺寸