Is it possible to introduce a single class using Spring?

I have a say UserService class that implements Service and annotates Service StereoType, I use Spring AOP and want to make a temporary workaround for this ( I know this can be done more efficiently )

@Service public class UserService implements Service{ @Autowired private Service self; } 

I tried this, but got a BeanNotFoundException, did I miss something?

I know I need to go with AspectJ with @Configurable, but just looking for a little temporary workaround

+2
java spring
Feb 24 '11 at 16:30
source share
4 answers

It works great -

 @Service(value = "someService") public class UserService implements Service{ @Resource(name = "someService") private Service self; } 
0
Mar 01 2018-11-11T00
source share

Why do you need this? In any method where you need to access the current instance, i.e. self , you just use the this .

Are we missing something? If there is anything else you are trying to do, try clarifying your question and we will take it as a hit.

If you're interested, this will not work, because a bean cannot be entered until it is completely constructed -> this means that Spring must enter all the properties of the bean. Effectively, what you did is create a circular dependency because Spring tries to instantiate the bean, and when it does, it discovers that it needs Autowire another bean. When he tries to find that the bean he cannot, because the bean has not been added to the list of initialized beans (because it is currently being initialized). Does this make sense? This is why you get a BeanNotFoundException because a bean cannot be initialized.

+5
Feb 24 '11 at 16:35
source share

You can edit your class.

 @Service @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON, proxyMode = ScopedProxyMode.INTERFACES) public class UserService implements Service { @Autowired private Service self; } 

it should work

+1
Nov 08 '13 at 18:29
source share

I know that this does not quite answer the question, but I would suggest rewriting your code so that you do not have to rely on aspects that apply to self-healing. For example, if you have a transactional method, just make sure the transaction material is correctly configured in the calling method.

If you really need to, you can make your ApplicationContextAware class and get a bean - with aspects from the context

0
Feb 24 '11 at 16:56
source share



All Articles