2014-06-06 225 views
0

我正在通过本教程工作:https://code.google.com/p/jee6-cdi/wiki/DependencyInjectionAnIntroductoryTutorial#Step_2:[email protected]_annotation_to_annotate_a_setTransport_se为什么beanContainer.getBeanByName(...)返回null?

我到了第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> 
+0

CDI IMPL正在运行什么反对? –

+0

你有'beans.xml'文件吗? –

+0

@Templar,是的,问题现在用我的beans.xml文件更新。 – Jim

回答

0

为HaraldWellmann说 - 使用焊接。在这种情况下更容易。例如,它为我的作品:

 
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")); }