Announcement Announcement Module
Collapse
No announcement yet.
@Inject without component scan Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Inject without component scan

    I tried to use @Inject with a bean definition like

    Code:
    	<bean class="spring.test.Bean1">
    	<bean class="spring.test.Bean2">
    Obtaining the beans with (for example)
    Code:
    getBean(Bean1.class)
    works fine. However, one of my beans contains a field

    Code:
        @Inject Bean1 bean1;
    which doesn't get injected by the container. Do I need component-scan, or something similar? I'd like to avoid that, as I am creating a somewhat generic library and wouldn't want to bind the library user to particular package names.

    Thanks,

    Jochen

  • #2
    Annotations by themselves are useless, they are only meta data (i.e. additional information) you need something to process those annotations. To have xml definitions and still use annotations you have to specify this by adding a context:annotation-config...

    All this is also explained in the reference guide.

    Comment


    • #3
      Hi, Marten,

      thanks very much for your hint. I changed my bean definition accordingly:

      Code:
      <?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:context="http://www.springframework.org/schema/context"
      	xsi:schemaLocation="http://www.springframework.org/schema/beans
      		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      		http://www.springframework.org/schema/context
      		http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
      	<bean class="spring.test.Bean1"/>
      	<bean class="spring.test.Bean2"/>
      
      	<!-- more bean definitions go here -->
      	<context:annotation-config />
      
      </beans>
      But the result remains unchanged?

      Comment


      • #4
        Make sure you use an ApplicationContext and not a BeanFactory... Also make sure that the Inject annotation is available on the classpath.

        Comment


        • #5
          Thanks again, Marten! I was, indeed, using a bean factory, and not an application context.

          Comment

          Working...
          X