码迷,mamicode.com
首页 > 编程语言 > 详细

spring学习(02)之bean实例化的三种方式

时间:2018-02-02 22:04:41      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:support   静态方法   出现   www.   app   out   schema   instance   factory   

bean实体例化的三种方式

在spring中有三中实例化bean的方式:

一、使用构造器实例化;(通常使用的一个方法,重点)

二、使用静态工厂方法实例化;

三、使用实例化工厂方法实例化

第一种、使用构造器实例化;

这种实例化的方式可能在我们平时的开发中用到的是最多的,因为在xml文件中配置简单并且也不需要额外的工厂类来实现。

比如

<?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="user" class="cn.lmn.ioc.User"></bean>

</beans>

 id是对象的名称,class是要实例化的类。

在这个类里面还要有个无参的构造,不然会出现异常

第二种 使用静态工厂创建

(1)创建静态的方法,返回类对象

package cn.lmn.bean;

public class Bean2Factory {
    //静态方法,返回Bean2对象
    public static Bean2 getBean2(){
        return new Bean2();
    }
}
<?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="user" class="cn.lmn.ioc.User"></bean> -->
    <!-- 使用静态工厂创建对象 -->
    <bean id="bean2" class="cn.lmn.bean.Bean2Factory" factory-method="getBean2"></bean>
</beans>

最后测试一下

 1 package cn.lmn.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import cn.lmn.bean.Bean2;
 8 import cn.lmn.ioc.User;
 9 
10 public class TestIOC {
11     @Test
12     public void testUser() {
13         // 1加载spring的配置文件,创建对象
14         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
15         // 2得到配置创建的对象
16     //    User user = (User) context.getBean("user");
17         Bean2 bean2 = (Bean2) context.getBean("bean2");
18         System.out.println(bean2);
19         
20     }
21 }

第三种 使用实例工厂创建

(1)创建不是静态的方法,返回类对象

 

package cn.lmn.bean;

public class Bean3Factory {
    //普通的方法,返回的是Bean2的对象
    public Bean3 getBean3(){
        return new Bean3();
    }
}

 

<?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="user" class="cn.lmn.ioc.User"></bean> -->
    <!-- 使用静态工厂创建对象 -->
    <!-- <bean id="bean2" class="cn.lmn.bean.Bean2Factory" factory-method="getBean2"></bean> -->
    <!-- 使用实例工厂创建对象 -->
    <!-- 创建工厂对象 -->
    <bean id="bean3Factory" class="cn.lmn.bean.Bean3Factory"></bean>
    <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
</beans>

 

最后测试一下

 1 package cn.lmn.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import cn.lmn.bean.Bean3;
 8 
 9 public class TestIOC {
10     @Test
11     public void testUser() {
12         // 1加载spring的配置文件,创建对象
13         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
14         // 2得到配置创建的对象
15     //    User user = (User) context.getBean("user");
16         Bean3 bean3 = (Bean3) context.getBean("bean3");
17         System.out.println(bean3);
18         
19     }
20 }

 

spring学习(02)之bean实例化的三种方式

标签:support   静态方法   出现   www.   app   out   schema   instance   factory   

原文地址:https://www.cnblogs.com/limn/p/8406969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!