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>
原创粉丝点击