spring-bean命名方式-以及实例化--配置文件整合


Spring 配置文件的一般结构如下:

<beans>
    <import  resource = resource1.xml ” />
    <import  resource =“resource2.xml” />
    <bean id=“bean1” class=“ *** ”></bean>
    <bean name= bean2” class= *** ”></bean>
    <alias alias= bean3 name= bean2 />
</beans>
其中import是导入其他配置。







以上实例代码为:

1、命名

首先是接口类:

public interface HelloWorld {
	public void sayHello();

}
然后是实现类:

public class HelloWorldImpl implements HelloWorld {

	@Override
	public void sayHello() {
		System.out.println("Hello World!");

	}

}
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-3.0.xsd">
	
	<bean class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
	<bean id="helloWorld" class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
	<bean name="helloWorldByName" class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
	<bean id="helloWorldById" name="helloWorldByName01" 
	class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
	
	<bean name="bean1;alias11;alias12;alias13"
	class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
	
	<bean id="bean2" name="alias21,alias22,alias23"
	class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
	
	
	<bean name="bean3" 
	class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
	<alias alias="alias31" name="bean3" />
	<alias alias="alias32" name="bean3" />
	
</beans>

2、实例化示例代码

首先是接口类:

public interface HelloWorld {
	public void sayHello();

}
实现类:

public class HelloWorldImpl implements HelloWorld {

    private String message;

    /**
     * 空构造器
     */
    public HelloWorldImpl() {
        this.message = "Hello World!";
    }

    /**
     * 带参构造器
     * 
     * @param message
     */
    public HelloWorldImpl(String message) {
        this.message = message;
    }

    public void sayHello() {
        System.out.println(message);
    }

}
构造器实例化不需要类。
下面是静态工厂实例化类:

public class HelloWorldStaticFactory {
	 /**
     * 工厂方法 
     * 
     * @param message
     * @return
     */
    public static HelloWorld newInstance(String message) {
        // 返回需要的Bean实例
        return new HelloWorldImpl(message);
    }

}
下面是实例工厂实例化类:

public class HelloWorldInstanceFactory {
     
	 /**
     * 工厂方法 
     * 
     * @param message
     * @return
     */
    public HelloWorld newInstance(String message) {
        // 返回需要的Bean实例
        return new HelloWorldImpl(message);
    }

}
最后是实例化的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-3.0.xsd">

	  <!--使用默认构造参数--> 
    <bean id="helloWorldWithNoArgs" 
    class="com.jike.spring.chapter04.instance.HelloWorldImpl" />

    <!--使用有参数构造参数-->  
    <bean id="helloWorldWithArgs" 
    class="com.jike.spring.chapter04.instance.HelloWorldImpl" >
        <!-- 指定构造器参数 -->  
        <constructor-arg index="0" value="Hello Args!"/>  
    </bean>
    
    
    <!--静态工厂方式-->  
    <bean id="helloWorldStaticFactory" 
     class="com.jike.spring.chapter04.instance.HelloWorldStaticFactory" 
     factory-method="newInstance" >
        <!-- 指定构造器参数 -->  
        <constructor-arg index="0" value="Hello Static Factory!"/>  
    </bean>
    
    <!-- 1、定义实例工厂Bean -->
    <bean id="helloWorldInstanceFactory" 
    class="com.jike.spring.chapter04.instance.HelloWorldInstanceFactory" />
    <!-- 2、使用实例工厂Bean创建Bean -->
    <bean id="helloWorldInstance" factory-bean="helloWorldInstanceFactory" factory-method="newInstance">
         <constructor-arg index="0" value="Hello Instance Factory!"></constructor-arg>
    </bean>
    
	
</beans>

最后是测试代码:

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		helloWorldInstanceFactory();
	}
	
	//使用无参数构造器来实例化Bean
	public static void sayHelloWithNoArgs() {
		BeanFactory beanFactory = 
				new ClassPathXmlApplicationContext("conf/conf-instance.xml");
		HelloWorld helloWorld = beanFactory.getBean("helloWorldWithNoArgs", HelloWorld.class);
		helloWorld.sayHello();
	}
	//使用有参数构造器来实例化Bean
	public static void sayHelloWithArgs() {
		BeanFactory beanFactory = 
				new ClassPathXmlApplicationContext("conf/conf-instance.xml");
		HelloWorld helloWorld = beanFactory.getBean("helloWorldWithArgs", HelloWorld.class);
		helloWorld.sayHello();
	}
	

	//使用静态工厂方法来实例化Bean
    public static void helloWorldStaticFactory() {
        // 1、读取配置文件实例化一个IOC容器
    	BeanFactory beanFactory = 
        		new ClassPathXmlApplicationContext("conf/conf-instance.xml");
        // 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现”
    	HelloWorld helloWorld 
    	= beanFactory.getBean("helloWorldStaticFactory", HelloWorld.class);
        // 3、执行业务逻辑
    	helloWorld.sayHello();
    }

    //使用实例化工厂方法来实例化Bean
    public static void helloWorldInstanceFactory() {
        // 1、读取配置文件实例化一个IOC容器
    	BeanFactory beanFactory = 
        		new ClassPathXmlApplicationContext("conf/conf-instance.xml");
        // 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现编程”
    	HelloWorld helloWorld 
    	= beanFactory.getBean("helloWorldInstance", HelloWorld.class);
        // 3、执行业务逻辑
    	helloWorld.sayHello();
    }
}















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值