• Spring- Bean的命名


    Bean id的命名约定:

      1.遵循XML命名规范

      2.由字母数字下划线组成

      3.驼峰式,首个单词字母小写,第二个单词首字母要大写

    样例项目的结构

    xml应该放在src下面的conf中,如果不方法src下将会找不到conf文件

    conf-definition.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-3.0.xsd">
            <!-- bean的六种命名方式 -->
            <!-- 配置全限定类名,唯一 -->
            <bean class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
            <!-- 指定id,唯一 -->
            <bean id="helloWorld" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
            <!-- 指定name,唯一 -->
            <bean name="helloWorldByName" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
            <!-- 指定id和name,唯一 -->
            <bean id="helloWorldById" name="helloWorldByName01" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
            
            <!-- 指定多个name,唯一 -->
            <bean name="bean1;alias11;alias12;alias13" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
            
            <bean id="bean2" name="alias21;alias22;alias23" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
            
            
            <!-- 指定别名,唯一 -->
            <bean name="bean3" class="com.zc.spring.chapter04.definition.HelloWorldImpl" /> 
            <alias name="bean3" alias="alias31"/>
            <alias name="bean3" alias="alias32"/>
            <alias name="bean3" alias="alias33"/>
           
    </beans>
    

    HelloWorld接口

    package com.zc.spring.chapter04.definition;
    
    public class HelloWorldImpl implements HelloWorld {
    
    	@Override
    	public void sayHello() {
    		System.out.println("Hello World!");
    
    	}
    
    }  

    Main运行类

    package com.zc.spring.chapter04.definition;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
    
    	
    	public static void main(String[] args) {
    		
    		
    		
    		sayHelloWorldByAlias();
    	}
    	
    	public static void sayHelloWorldById() {
    		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
    		HelloWorld helloWorld = beanFactory.getBean("helloWorld", HelloWorld.class);
    		helloWorld.sayHello();
    		
    	}
    	
    	public static void  sayHelloWorldByClass() {
    		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
    		HelloWorld helloWorld = beanFactory.getBean(HelloWorldImpl.class);
    		helloWorld.sayHello();
    	}
    
    	public static void  sayHelloWorldByName() {
    		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
    		HelloWorld helloWorld = beanFactory.getBean("helloWorldByName", HelloWorld.class);
    		helloWorld.sayHello();
    	}
    	
    	public static void  sayHelloWorldByNameAndId() {
    		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
    		HelloWorld helloWorld = beanFactory.getBean("helloWorldById", HelloWorld.class);
    		helloWorld.sayHello();
    		HelloWorld helloWorld01 = beanFactory.getBean("helloWorldByName01", HelloWorld.class);
    		helloWorld01.sayHello();
    	}
    	
    	public static void sayHelloWorldMultiName() {
    		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
    		HelloWorld helloWorld = beanFactory.getBean("bean1", HelloWorld.class);
    		helloWorld.sayHello();
    		HelloWorld bean11 = beanFactory.getBean("alias11",HelloWorld.class);
    		bean11.sayHello();
    		HelloWorld bean12 = beanFactory.getBean("alias12",HelloWorld.class);
    		bean12.sayHello();
    		HelloWorld bean13 = beanFactory.getBean("alias13",HelloWorld.class);
    		bean13.sayHello();
    		
    		HelloWorld bean2 = beanFactory.getBean("bean2",HelloWorld.class);
    		bean2.sayHello();
    		HelloWorld bean21 = beanFactory.getBean("alias21",HelloWorld.class);
    		bean21.sayHello();
    		HelloWorld bean22 = beanFactory.getBean("alias22",HelloWorld.class);
    		bean22.sayHello();
    		HelloWorld bean23 = beanFactory.getBean("alias23",HelloWorld.class);
    		bean23.sayHello();
    	}
    	public static void  sayHelloWorldByAlias() {
    		//配置文件加载以及IOC容器启动
    		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
    		//通过别名获取bean实例
    		HelloWorld bean3 = beanFactory.getBean("bean3", HelloWorld.class);
    		//利用bean实例输出helloworld信息
    		bean3.sayHello();
    		HelloWorld bean31 = beanFactory.getBean("alias31",HelloWorld.class);
    		bean31.sayHello();
    		HelloWorld bean32 = beanFactory.getBean("alias32",HelloWorld.class);
    		bean32.sayHello();
    		HelloWorld bean33 = beanFactory.getBean("alias33",HelloWorld.class);
    		bean33.sayHello();
    	}
    }
    

      

  • 相关阅读:
    java
    java
    android-studio于java相关
    转-Cannot refer to an instance field arg while explicitly invoking a constructor
    java
    java
    hdoj 1251 统计难题(字典树)
    hdoj 3555 Bomb(DFA+dp)
    hdoj 1247 Hat’s Words(字典树)
    poj 1204 Word Puzzles(字典树)
  • 原文地址:https://www.cnblogs.com/RzCong/p/9125349.html
Copyright © 2020-2023  润新知