开发者

Can i use spring bean at different places or at one place

Suppose i have this in controller

@Resource(name="registrationService")
private RegistrationService registrationService;

This is working fine and i can use methods in service class.

Now suppose i have different java class and i want to use methods in registrationService class . so can i use same thing there

@Resource(name="registrationService")
private RegistrationService registrationService;

and access methods or i have to declare different bean in spring with different name

This is the function in Service class

public String test(){   return "testing"; }

Now if i call this in controller , it works fine .

But i have separate class called UserDAO for getting users. If i do like this

private List internalDatabase() {

        List<Registration> users = new ArrayList<Registration>();
        Registration user = null;

            logger.debug("Before");
    logger.debug(registrationService.test());
    logger.debug("After");

            users.add开发者_JS百科(user);
        return users;
    }

Anything after

logger.debug(registrationService.test());

is not executed . if i remove that line everything works . i don't know whats the problem


Yes, you can use Spring to inject the same bean into two different classes.


my best guess is a NullPointerException occured on

 logger.debug(registrationService.test());

that's why the succeeding lines wasn't executed. registrationService, i believe wasn't properly injected by the container. Make sure that your service class is either properly defined on your applicationContext.xml or the package is included on the component scanning.

Make sure also that UserDAO is managed by Spring or has a way (if not) to access your spring-managed service class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜