简体   繁体   中英

Dependency Injection in simple java program using CDI

Do we always require a beans.xml for a dependency injection in a simple java program using only CDI,and do we need to construct a bean to get the objects injected???

Below are the codes for simple java project with dependency Injection::

Interface

 public interface Hello
 {
      public void sayHello(String str);
 }

class

 public class HelloImpl1 implements Hello{
               public void sayHello(String str){
 System.out.println("Hello from 1st block")
               }
 }

class

          public class HelloImpl2 implements hello{
                public void sayHello(String str){
                    System.out.println("Hello from 2nd block")
                            }
                   }

Class

      public CallingHello(){
          @Inject
          Hello hello;

         public void callHello(){
            hello.sayHello("Hey");
            }
       }

Class

      public Test(){

    public static void main(String[] args){
       CallingHello hello=new CallingHello();
       hello.callHello();
         }

Thats all i am doing and while running the test class its throwing nullpointerexception,and i am making simple classes no bean in eclipse,mi going right??

You can use autowiring , in order to inject dependencies (and possibly apply post processors) to an object that is not part of the application context.

You can use java config class with factory methods to configure ApplicationContext :

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean(name="HelloImpl1")
    public HelloImpl1 helloImpl1() {
        return new HelloImpl1();
    }

    @Bean(name="HelloImpl2")
    public HelloImpl1 helloImpl2() {
        return new HelloImpl2();
    }
}

And then to instantiate:

ApplicationContext ctx = 
    new AnnotationConfigApplicationContext(AppConfig.class);
HelloImpl1 helloImpl1= (HelloImpl1)ctx.getBean("HelloImpl1");
helloImpl1.sayHello("#?*")

If you are using Spring for the dependency injection, you always need a configuration file. But it does not need to be named beans.xml.

Yes the beans are injected in other beans .

Technically no you don't have use an XML file define your beans and inject dependencies. It depends on the type of ApplicationContext you use. But, for most practical applications you'll use one of the AbstractXMLContextApplication instances which will load an XML file to figure out the beans your app has declared. The name of that file doesn't have to be beans.xml. That's a convention used, but it you don't have to name it that. You can override which file it looks for when you constructor the implementation of AbstractXMLApplicationContext object.

All you need to do is construct the bean container, and it will load the beans, resolve the dependencies, and startup the program (if you are using init-method). Typically my Spring Container Java programs look like this:

ApplicationContext context = null;
try {
    context = new FileSystemXmlApplicationContext( new String[] { file } );
    // pause until we want to shutdown or after something happens.
    System.in.readLine();
} finally {
    context.close(); // remember to clean up!
}

And I let the init methods on the bean startup my application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM