Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Wicket's @SpringBean annotation work?

How does Wicket's @SpringBean annotation work? Does it use reflection at run time? Does it make the compiler inject some code? Or what?

like image 323
Tarquila Avatar asked Dec 22 '09 16:12

Tarquila


People also ask

How@ Bean annotation works?

One of the most important annotations in spring is the @Bean annotation which is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. This annotation is also a part of the spring core framework.

How to define a spring Bean using annotation?

To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory .

When use@ Bean?

@Bean is used to mark a method as one that creates a bean and Spring will then add it to the context for us. The return type of the method defines the type of bean that is created, so both of the beans created in this example will be referred to by the type MyBean rather than their implementations.

What is a Bean spring?

Here's a definition of beans in the Spring Framework documentation: In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.


2 Answers

The class marked with a @SpringBean annotation has to have one of:

  1. A no-args constructor
  2. A superclass with a no-args constructor
  3. Implement an interface

An exception will be thrown if these conditions are not met as Wicket will not be able to proxy the class.

like image 115
Rod Avatar answered Sep 22 '22 16:09

Rod


@SpringBean works using Wicket's underlying Injector mechanism. When you instantiate a Wicket component, the constructor of Wicket's component base class introspects the class being instantiated, looking for the @SpringBean annotation. If the bean is found, then Wicket generates a proxy for the spring bean and injects it into the component's field. This is Wicket's equivalent of Spring's @Autowired annotation, the effect is similar.

It doesn't, however, have anything to do with Spring's own context/classpath scanning functionality (e.g. @Component), which is about auto-discovery of what is and isn't a bean, rather having anything to do with wiring.

like image 20
skaffman Avatar answered Sep 19 '22 16:09

skaffman