Spring三种实例化方式

Spring容器支持两种格式的配置文件.properties  .xml这两种配置文件

在面向对象的程序中,想要使用某个对象,就需要先实例化这个对象。同样,在Spring中,要想使用容器中的Bean,也需要实例化Bean。实例化Bean有三种方式,分别为构造器实例化、静态工厂方式实例化实例工厂方式实例化(其中最常用的是构造器实例化)

1.构造器实例化

public class InstanceTest1 {
       public static void main(String[] args) {
     	String xmlPath = "com/cxit/instance/constructor/beans1.xml";
               ApplicationContext applicationContext = 
			    new ClassPathXmlApplicationContext(xmlPath);
	Bean1 bean = (Bean1) applicationContext.getBean("bean1");
                System.out.println(bean);
      }
}

配置文件

    <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-4.3.xsd">
           <bean id="bean1" class="com.cxit.instance.constructor.Bean1" />
    </beans>

2.静态工厂实例化

   public class InstanceTest2 {
         public static void main(String[] args) {
	String xmlPath ="com/cxit/instance/static_factory/beans2.xml";
 	ApplicationContext applicationContext = 
 			         new ClassPathXmlApplicationContext(xmlPath);
 	System.out.println(applicationContext.getBean("bean2"));
         }
public class MyBean2Factory {	
         public static Bean2 createBean(){
              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-4.3.xsd"> 
	 
	 <bean id = "bean2" class = "com.cxit.instance.static_factory.MyBean2Factory"
	 factory-method="createBean"
	 />
	
	
</beans>

3.实例工厂实例化

 public class InstanceTest3 {
       public static void main(String[] args) {
	String xmlPath = "com/cxit/instance/factory/beans3.xml";
	ApplicationContext applicationContext = 
 			         new ClassPathXmlApplicationContext(xmlPath);
 	System.out.println(applicationContext.getBean("bean3"));
        }
 }
    public class MyBean3Factory {
          public Bean3 createBean(){
                 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-4.3.xsd"> 
	 
	 <bean id = "myBean3Factory" class = "com.cxit.instance.factory.MyBean3Factory">
	</bean>
	
	<bean id = "bean3"  factory-bean="myBean3Factory" factory-method="createBean">
	</bean>
	
</beans>

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值