java - How to inject an object via annotation and set an attribute value on this object -


I'm using @Resource annotation to inject an object that works fine Used to be. Although I would like to set an attribute on this injection object and I'm not sure it is possible using annotation.

For example, I have a class test that has an installation using the MyClass injected @Resource annotation. There is a feature in MyClass , myAttribute , which I want to set when the MyClass example is injected.

Does anyone know if this is possible?

"post-text" itemprop = "text">

You should use the @PostConstruct annotation, from to Javax.annotation :

  public class test {@Resource Private MyClass myClass; @PostConstruct Public Zero init () {myClass.setMyAttribute ("test class"); }} Public Square Extreme {@ Resource Private Michelle Miklass; @PostConstruct Public Zero init () {myClass.setMyAttribute ("Another test class"); }}  

This method will be called after the spring has started your object (i.e. all the dependencies are injected).

I believe MyClass is not a singleton.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -