zoukankan      html  css  js  c++  java
  • Spring注入内部的Beans

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/injecting-inner-beans.html

    如你所知,Java内部类在其他类的范围内定义,类似地,内部bean是在另一个bean的范围内定义的bean。因此,<property />或<constructor-arg />元素中的<bean />元素称为内部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-3.0.xsd">
    
       <bean id = "outerBean" class = "...">
          <property name = "target">
             <bean id = "innerBean" class = "..."/>
          </property>
       </bean>
    
    </beans>

    写法和Java内部类相似。

    例子:

    pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.jsoft.testspring</groupId>
      <artifactId>testinnerbean</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>testinnerbean</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        
      </dependencies>
    </project>

    SpellChecker.java:

    package com.jsoft.testspring.testinnerbean;
    
    public class SpellChecker {
        public SpellChecker(){
            System.out.println("SpellChecker无参数构造函数初始化");
        }
        
        public void checkSpelling(){
            System.out.println("SpellChecker检查方法");
        }
    }

    TextEditor.java:

    package com.jsoft.testspring.testinnerbean;
    
    public class TextEditor {
        private SpellChecker spellChecker;
        
        public void setSpellChecker(SpellChecker spellChecker){
            System.out.println("TextEditor通过setter初始化");
            this.spellChecker = spellChecker;
        }
        
        public void spellCheck() {
            this.spellChecker.checkSpelling();
        }
        
    }

    beans.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"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="textEditor" class="com.jsoft.testspring.testinnerbean.TextEditor">
            <property name="SpellChecker">
                <bean id="spellCheckerId" class="com.jsoft.testspring.testinnerbean.SpellChecker"></bean>
            </property>
        </bean>
       
    </beans>

    App.java:

    package com.jsoft.testspring.testinnerbean;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            TextEditor textEditor = (TextEditor)applicationContext.getBean("textEditor");
            textEditor.spellCheck();
        }
    }

    运行结果:

    测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test9/testinnerbean

  • 相关阅读:
    Linux系统中DHCP的配置
    Linux系统下第三方软件安装实例
    Linux系统在启动过程中mbr主引导程序被破坏的解决方案
    Linux系统在启动过程中启动级别发生错误的解决办法
    Linux系统下超级用户密码的修改
    Linux系统在启动过程中内核文件丢失的解决方法
    Linux系统在启动过程中grub引导文件丢失的解决方法
    Linux之FTP服务
    Linux之LVM设备的管理
    Linux下设备的基本管理
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6883719.html
Copyright © 2011-2022 走看看