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

1、什么是Javabean

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

JavaBean定义了一组规则

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

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

菜鸟教程Javabeanjava

学习JavaBeanweb

JavaBean能够跨平台,能够用于多种状况下。spring

2、Spring里面怎么用JavaBean

首先定义一个JavaBean:bash

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文件,基于这个文件来配置JavaBeanapp

<?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 管理jsp

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

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());

    }

}
复制代码

运行结果:this

Eather 12345
复制代码

3、一些问题

  • 每次取出的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不相同就能够(这个几乎是废话)。

相关文章
相关标签/搜索