为什么beanContainer.getBeanByName(...)返回null?

时间:2014-06-06 18:37:51

标签: java cdi

我正在完成本教程:https://code.google.com/p/jee6-cdi/wiki/DependencyInjectionAnIntroductoryTutorial#Step_2:_Use_the_@Inject_annotation_to_annotate_a_setTransport_se

我到了第6步,遇到了问题。似乎

(AutomatedTellerMachine) beanContainer.getBeanByName("atm"); 

返回null。

我正在使用JavaSE-1.6。可能是什么原因?

AutomatedTellerMachineImpl

    package src.main.java;

    import java.math.BigDecimal;

    import javax.inject.Inject;
    import javax.inject.Named;

    @Named("atm")
    public class AutomatedTellerMachineImpl implements AutomatedTellerMachine{

        private ATMTransport transport;

        public void deposit(BigDecimal bd) {
            System.out.println("deposit called");
            transport.communicateWithBank(null);

        }

        public void withdraw(BigDecimal bd) {
            System.out.println("withdraw called");
            transport.communicateWithBank(null);

        }

        @Inject
        public void setTransport(ATMTransport t){
            this.transport = t;
        }


    }

AutomatedTellerMachine

    package src.main.java;

    import java.math.BigDecimal;

    public interface AutomatedTellerMachine {

        public abstract void deposit(BigDecimal bd);

        public abstract void withdraw(BigDecimal bd);
    }

ATMMain

    package src.main.java;

    import java.lang.annotation.Annotation;
    import java.math.BigDecimal;

    import org.cdiadvocate.beancontainer.BeanContainer;
    import org.cdiadvocate.beancontainer.BeanContainerManager;


    public class ATMMain {

        public static BeanContainer beanContainer = BeanContainerManager.getInstance(); 

        static{
            beanContainer.start();
        }

        public static void main (String[] args){

            AutomatedTellerMachine atm = (AutomatedTellerMachine) beanContainer.getBeanByName("atm");

            atm.withdraw(new BigDecimal("10.00"));
            atm.deposit(new BigDecimal("100.00"));
        }
    }

beans.xml中:

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> </beans>

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>cdi</groupId>
  <artifactId>cdi</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <description>
        Source code for CDI DI tutorial. 
        See: http://code.google.com/p/jee6-cdi/wiki/DependencyInjectionAnIntroductoryTutorial
   </description>

    <dependencies>
        <dependency>
            <groupId>org.cdiadvocate</groupId>
            <artifactId>cdiadvocate-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.cdiadvocate</groupId>
            <artifactId>cdiadvocate-resin-impl</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
<!--                <scope>test</scope> -->
        </dependency>
    </dependencies>

       <repositories>
           <repository>
               <id>cdi.advocate</id>
               <name>CDI Advocacy</name>
               <url>http://jee6-cdi.googlecode.com/svn/m2/repository/</url>
           </repository>
           <repository>
               <id>caucho.maven.repo</id>
               <name>Caucho Repository</name>
               <url>http://caucho.com/m2</url>
           </repository>
       </repositories>
     <build>
       <sourceDirectory>src</sourceDirectory>
       <plugins>
         <plugin>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>3.1</version>
           <configuration>
             <source/>
             <target/>
           </configuration>
         </plugin>
         <plugin>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.3.2</version>
           <configuration>
             <source>1.6</source>
             <target>1.6</target>
           </configuration>
           </plugin>
       </plugins>
     </build>
   </project>

1 个答案:

答案 0 :(得分:0)

正如HaraldWellmann所说 - 使用Weld。在这种情况下,这更容易。例如,它适用于我:

public static void main(String[] args) {
        Weld weld = new Weld();
        WeldContainer container = weld.initialize();
        AutomatedTellerMachine atm = container
                .instance()
                .select(AutomatedTellerMachine.class)
                .get();

atm.withdraw(new BigDecimal("10.00")); atm.deposit(new BigDecimal("100.00")); }