Spring入门学习手册 1:最简单的反转控制

784 阅读3分钟

一、什么是Javabean

看到的一个比较专业的解释是:

JavaBean定义了一组规则

JavaBean就是遵循此规则的平常的Java对象

JavaBean是一种特殊的Java类,提供getter 和 setter方法访问它的属性。具体的内容可以查看:

菜鸟教程Javabean

学习JavaBean

JavaBean可以跨平台,可以用于多种情况下。

二、Spring里面怎么用JavaBean

首先定义一个JavaBean:

package com.learn.springDemo;

public class Category {
    private int id;
    private String name;

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
}

然后建立一个xml文件,基于这个文件来配置JavaBean

<?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 name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>

</beans>

可以看到xml中配置了JavaBean的属性。 当然,还有基于注解来配置的方式使用 Java 配置进行 Spring bean 管理

使用时直接从容器中取出来就行:

package com.learn.springTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.springDemo.Category;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});

        Category c =  (Category) ac.getBean("c");

        System.out.println(c.getName() + " " + c.getId());

    }

}

运行结果:

Eather 12345

三、一些问题

  • 每次取出的JavaBean是一个吗?
package com.learn.springTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.springDemo.Category;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});

        Category c =  (Category) ac.getBean("c");

        System.out.println(c.getName() + " " + c.getId());

        c.setId(10086);
        c.setName("David");

/////////////////////////////////////////////////////
        Category cc = (Category) ac.getBean("c");//再取一个取出来
        System.out.println(cc.getName() + " " + cc.getId());//打印


    }

}

根据这段代码的运行结果,我认为应该是同一个,每次取出的或者说引用的都是同一个对象,在程序开始后的某一个时刻初始化好后就一直用的是一个对象。

Eather 12345
David 10086

  • JavaBean是什么时候被初始化的?

    看看下面这段代码

    package com.learn.springTest;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.learn.springDemo.Category;
    public class Test {
      public static void main(String args[]) {
          ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
    
          Category c = (Category) ac.getBean("c");
    
          System.out.println(c.getName() + " " + c.getId());
    
          c.setId(10086);
          c.setName("David");
    
          //重新初始化一个ApplicationContext
          ApplicationContext ac2 = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
          Category cc = (Category) ac2.getBean("c");//再取一个取出来
          System.out.println(cc.getName() + " " + cc.getId());//打印
    
    
          }
      }
    

    运行结果为:

    Eather 12345
    Eather 12345
    

    JavaBean是什么时候初始化就很显而易见了。

  • 依赖注入怎么搞(在一个类里面引用另一个类的实例

<?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 name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>
        <!--这里注入Category实例,已经初始化过的-->
        <property name="category" ref="c"></property>
    </bean>

</beans>

Product类的定义是这样的:

package com.learn.springDemo;

public class Product {
    private int id;
    private String name;
    private Category category;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Category getCategory() {
        return this.category;
    }
}

显然,在依赖注入时,xml文档中要用 ref,而非value

此外,依赖注入的必须是实例化好的对象。

  • xml里面可以写多个相同的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 name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="ccc" class="com.learn.springDemo.Category">
        <property name="name" value="Tom"></property>
        <property name="id" value="111111"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>
        <!--这里注入Category实例,已经初始化过的-->
        <property name="category" ref="c"></property>
    </bean>

</beans>

非常显然可以,只要name不相同就可以(这个几乎是废话)。