Results 1 to 5 of 5

Thread: @Inject without component scan

  1. #1
    Join Date
    Feb 2012
    Posts
    11

    Default @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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2012
    Posts
    11

    Default

    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?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Make sure you use an ApplicationContext and not a BeanFactory... Also make sure that the Inject annotation is available on the classpath.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Feb 2012
    Posts
    11

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •