01.spring_helloword
来源:互联网 发布:2016淘宝店不挣钱了吧 编辑:程序博客网 时间:2024/05/22 06:30
基于spring4.0、eclipse、jdk1.8、tomcat7.0
1.建立一个springhellowrd首先导入所需的spring jar包。
commons-logging-1.1.3.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
2.建立一个需要被管理的domain类
package com.atxiaofeng.bean;public class Book { private Integer id; private String name; private String author; public Book() { super(); } public Book(Integer id, String name, String author) { super(); this.id = id; this.name = name; this.author = author; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String toString() { return "Book [id=" + id + ", name=" + name + ", author=" + author + "]"; }}
3.建立一个beans.xml也就是spring文件
<?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="book" class="com.atxiaofeng.bean.Book"> <!--property 相当于调用domain里面的 setXXX方法把这个属性值赋给变量--> <property name="id" value="1"/> <property name="name" value="水浒传"/> <property name="author" value="作者"/></beans>
4.通过springioc来获取对象
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");//beans.xml是spring文件,这个构造器可以传入多个spring配置文件,不过一般不会这么用//ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml","beans1.xml","beans2.xml");一般会如下使用匹配所有的beans开头的spring配置文件并管理。new ClassPathXmlApplicationContext("beans*.xml");Book book = (Book) ioc.getBean("book"); System.out.println(book);
5.使用构造器参数注入
<!--如果不指定符合多个构造器调用那么调用domain类里面最下面的构造器 --> <bean id="book3" class="com.atxiaofeng.bean.Book" > <constructor-arg index="0" value="1"/> <constructor-arg index="1" value="1"/><!--constructor-arg index="1" value="1" name="id" type="java.lang.Integer" ref=""/ --这个用来做展示> </bean> <!-- 构造器有这些属性:index:被调用的构造器参数位置。(跟数组一样从0开始) value:这个参数的值。 ref: 可以引用别的bean为这个参数赋值。 name: 这个参数的名称。 type: 这个参数的类型。-->
//这两个构造器都符合上面的调用规则,spring无法区分具体调用的时候就会取下面的一个,就如都匹配到第一个时发现下面还有一个构造器也符合规则,就会实际调用最下面的那个。
public Book(Integer id, String author) {
super();
System.out.println(“public Book(Integer id, String author)”);
this.id = id;
this.author = author;
}
public Book(String name,Integer id ) { super(); System.out.println("public Book(String name,Integer id )"); this.id = id; this.name = name;}
6.使用p名称空间为变量赋值、需要在namespaces里面选择p名称空间才可以用,为属性赋值,比较简洁。
<bean id="book4" class="com.atxiaofeng.bean.Book" p:id="12" p:name="p名称空间"></bean>
7.使用spring为属性赋值为null
<bean id="book5" class="com.atxiaofeng.bean.Book"></bean><!--这是一种方式Book [id=null, name=null, author=null]-->Book [id=null, name=西游记, author=吴承恩]
8.使用级联属性赋值
<!--级联属性赋值,使用 .属性名的方式 --> <bean id="student1" class="com.atxiaofeng.bean.Student"> <property name="book" ref="book1"></property> <property name="book.name" value="级联属性赋值"></property> </bean>
阅读全文
0 0
- 01.spring_helloword
- 01.EntityBase
- 01.struts2_msb
- 01.hibernate_msb
- 01.序
- 01. Introduction
- C++ EventHandler v0.01.
- 01.Eclipse开发工具
- 01.Android相关术语
- 01.struts基础
- 01.联系方式验证
- 01.J2EE 书籍列表
- 01.写在开篇
- CodeSmith----01.基本模板
- 01.C++ book
- 01.汇编语法
- 01.反汇编笔记
- 01.Shell编程总结
- Node.js 文件系统 fs
- HDU 2017 字符串统计
- 总结http服务器框架
- java实现url访问(get,post)
- 常见问题总结
- 01.spring_helloword
- codevs 1173 最优贸易
- Android第五天之ProgressBar进度条
- Caffe实战Day4-开始模型训练
- bzoj 1036(树链剖分)
- js 判断数据类型
- HttpServletWrapper 和 HttpServletResponseWrapper
- c++ functional object函数对象
- org.apache.ibatis.exceptions.PersistenceException: 的解决