JavaTechnical
JavaTechnical

Reputation: 9357

How to avoid No bean named 'something' is defined exception?

I would like to avoid the No bean named 'something' is defined exception when the related resource is not available.

I have a project, which is divided into three modules one of which is used by the other two modules. As per the requirements the shared module actually refers to one of the other two module's bean. Unfortunately, while executing the other module I am getting the No bean named 'something' is defined exception since there is no bean name defined in that module.

I would like to have null placed in that instead of raising an exception and abnormally terminating the program. How to do that?

Thanks in advance.

Upvotes: 0

Views: 260

Answers (1)

axtavt
axtavt

Reputation: 242786

For optional dependencies you can use @Autowired(required = false) instead of @Resource.

If you need to specify bean name explicitly (like in @Resource), use @Qualifier in addition to @Autowired:

@Autowired(required = false) @Qualifier("something") 
Foo something;

Upvotes: 2

Related Questions