Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring autowiring not working

Tags:

java

spring

Hi, I am using spring 3.0 with Quartz in a scheduler class. I have created the application context by

private static final ClassPathXmlApplicationContext applicationContext;
static {
    applicationContext = new
        ClassPathXmlApplicationContext("config/applicationContext.xml");
}

The problem is that none of the @Autowired beans actually get auto-wired, so I have to manually set dependencies like this:

<bean class="com.spr.service.RegistrationServiceImpl" id="registrationService">
    <property name="userService" ref="userService" />
</bean>

Example of where I'm using @Autowired:

public class RegistrationService {
   @AutoWired private UserService userService;
   // setter for userService;
}

public class UserService{
   // methods
}

I also made sure to enable the annotation configuration in my Spring config:

<context:annotation-config/>
<bean id="registrationSevice" class="RegistrationService"/>
<bean id="userService" class="UserService"/>

Why is @Autowired not working for me?

like image 905
prassee Avatar asked Apr 18 '11 00:04

prassee


People also ask

Why Autowired is coming null?

Having no clue of the existence of this MyService object, Spring is not able to inject a MyComponent bean inside it. Thus, the MyComponent instance inside the MyService object we created will remain null, causing the NullPointerException we get when we try to call a method on this object.

Why is @autowired annotation not recommended?

If we had used Autowired, we would have been getting the following error in the test. Therefore, it is not recommended to use Autowired. Safety — Forces Spring to provide mandatory dependencies. We make sure that the created objects are valid after construction.

Can we use @autowired in Spring?

In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file. We have enabled annotation injection.


1 Answers

You haven't provided the UserService class source code so I can't be sure about your problem. Looks like the UserService class is missing a 'stereotype' annotation like @Component or @Service. You also have to configure the Spring classpath scanning using the following configuration:

    <?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">

     <!-- Add your classes base package here -->          
     <context:component-scan base-package="your.package"/>

   </beans>

Your beans must include one of the Spring stereotype annotations like:

package your.package;

@Service
public class UserService{
}
like image 161
Wilson Freitas Avatar answered Oct 24 '22 19:10

Wilson Freitas