How to autowire a bean inside a class that is not a configured bean?

If you can get hold of the Spring context, cast it to AutowireCapableBeanFactory and pass your instance of TheClass to the autowireBean(Object) method. Spring will then try to apply its autowiring rules to that object.

If you can get hold of the Spring context, cast it to AutowireCapableBeanFactory, and pass your instance of TheClass to the autowireBean(Object) method. Spring will then try to apply its autowiring rules to that object. You'd need to add @Autowired to the setTheData method, though.

1 I ended up just creating a context and then using the ACBF to create the bean I needed. There's only one bean I needed autowired, so that seemed essentially equivalent. Since TheClass is instantiated entirely outside of the scope of Spring, that seemed like the only option (and it works), even if it does tightly couple things.Thanks.

– AHungerArtist Jan 11 at 17:00.

You should be able to just use the @Autowired annotation on your instance variable that your setter is setting, without having to declare a TheClass bean in your XML. That is: public class TheClass { @Autowired private TheData theData; }.

That doesn't seem to work, as I had tried that. – AHungerArtist Jan 10 at 21:27 Can you annotate your TheClass class with @Component, @Repository, @Service, or @Controller? It should be picked up, then, I think, as long as you have in your XML configuration.

– GriffeyDog Jan 10 at 21:54 Let me try that and get back to you in a bit. I appreciate the help. – AHungerArtist Jan 11 at 15:41 That won't work.

I think it's kind of intractable. The code that's instantiating TheClass is completely outside of a Spring context. I think I'm going to have to do what skaffman suggested.

Thanks, though. – AHungerArtist Jan 11 at 16:57.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions