2

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,m i going right??

Charles Stevens
  • 1,568
  • 15
  • 30

3 Answers3

2

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("#?*")
user987339
  • 10,519
  • 8
  • 40
  • 45
1

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.

Community
  • 1
  • 1
Tom Jonckheere
  • 1,630
  • 3
  • 20
  • 37
  • i am not developing a spring project only a simple java project,so do i need a .xml file ,because when i am running the main it eclipse is throwing nullpointerexception – Charles Stevens Oct 16 '13 at 12:55
  • You used the Spring tag, so i thought you used Spring. mayby you can checkout: http://www.vogella.com/articles/DependencyInjection/article.html or http://code.google.com/p/jee6-cdi/wiki/DependencyInjectionAnIntroductoryTutorial_Part1 – Tom Jonckheere Oct 16 '13 at 13:00
1

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.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138