Following the question Understanding Spring Autowired usage I wanted to create a complete knowledge base for the other option of spring wiring, the Configuration class. In order to import beanFromSomewhereElse we need to import it's definition. If the bean is defined in another Configuration class we can use the Import annotation:.
After we imported other XMLs or Configuration classes, we can use the beans they declare in our context by declaring a private member to the Configuration class as follows:. Or use it directly as parameter in the method which defines the bean that depends on this beanFromSomewhereElse using Qualifier as follows:. Importing properties is very similar to importing bean from another xml or Configuration class.
Instead of using Qualifier we'll use Value with properties as follows:. In order to allow spring to treat such classes as beans containers we need to mark this in our main xml by putting this tag in the context:. You can now import Configuration classes exactly the same as you would create a simple bean:. There are ways to avoid spring XMLs altogether but they are not in the scope of this answer.
You can find out one of these options in my blog post on which I'm basing my answer. Basically I find this method of declaring beans much more comfortable than using XMLs due to a few advantages I see:. Bottom line: It is perfectly fine to combine XMLs, Configuration and annotations in your application context.
Spring doesn't care about the method a bean was declared with. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Understanding spring Configuration class Ask Question. Asked 7 years, 7 months ago. Active 2 years, 11 months ago. Viewed 67k times. Does it have any affect on the code itself?
Improve this question. Avi Avi Add a comment. Active Oldest Votes. The advantages and disadvantages of using this method Basically I find this method of declaring beans much more comfortable than using XMLs due to a few advantages I see: Typos - Configuration classes are compiled and typos just won't allow compilations Fail fast compile time - If you forget to inject a bean you'll fail on compile time and not on run-time as with XMLs Easier to navigate in IDE - between constructors of beans to understand the dependency tree.
Possible to easily debug configuration startup The disadvantages are not many as I see them but there are a few which I could think of: Abuse - Code is easier to abuse than XMLs With XMLs you can define dependencies based on classes that are not available during compile time but are provided during run-time.
With Configuration classes you must have the classes available at compile time. The Bean annotation tells Spring that a method annotated with Bean will return an object that should be registered as a bean in the Spring application context.
Here, the method name is annotated with Bean works as bean ID and it creates and returns the actual bean. Your configuration class can have a declaration for more than one Bean.
Once you are done creating all the source files and adding the required additional libraries, let us run the application. You should note that there is no configuration file required. Here, the foo bean receives a reference to bar via the constructor injection. Now let us look at another working example.
The Import annotation allows for loading Bean definitions from another configuration class. Now, rather than needing to specify both ConfigA. Karthikeya T. Chaand Sheikh. Senol Atac.
0コメント