0

As per my experience in Spring <context:component-scan base-package="<package name>"/> is enough in application context and through @Autowired beans will able to inject.

But Now I am using Spring 4.3 and configuring application context thru xml. Then bean is null (not injected).

If I remove comment in application context, bean will be available.

See the xml and my resource class here.

<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-4.0.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<context:component-scan base-package="com.mcp.rest.jersy2.service.*"/>

<!-- 
<bean id ="userService" class = "com.mcp.rest.jersy2.service.UserServiceImpl"></bean>
 -->

@Path("/service")
@Component
public class UserServiceResource {

private static final String text = "Message from Server :%s ";

@Autowired
private UserService userService;


@GET
@Path("{name}")
@Consumes(MediaType.TEXT_PLAIN)
public Response getMsg(@PathParam("name") String name) {
    String response = String.format(text, name);
    return Response.status(Response.Status.OK).entity(response).type(MediaType.TEXT_PLAIN).build();
}


public UserService getUserService() {
    return userService;
}


public void setUserService(UserService userService) {
    this.userService = userService;
}
}

what is the problem?

Service class:

package com.mcp.rest.jersy2.service;

import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService 
{

 }

IGNORE NOW

My Component scan was issue. I need to cover the package where beans are injecting.

I reduced

<context:component-scan base-package="com.mcp.rest.jersy2.service.*"/>

to

<context:component-scan base-package="com.mcp.rest.jersy2.*"/>
Chowdappa
  • 1,580
  • 1
  • 15
  • 31

0 Answers0