@Component is usually used to get a bean of a specific class.
eg:
@Component
public class Vehicle{
}
Now we can get this bean simply by
ApplicationContext context =new ClassPathXmlApplicationContext(new String[] {"Spring.xml"});
then Vehicle vehicle = (vehicle)context.getBean("vehicle");
how ever to work this
<context:component-scan base-package="mad.project.package" /> has to be put in spring.xml
Note that we can simply get the bean by just calling getBean with the parameter vehicle. That is the parameter's first letter is in lowercase where as class name should begin with capital.
Now another method is putting @Component("beanName") instead of just putting @Component. This way you can get the bean using "beanName"
How ever it is not good to use this annotation.
Instead use @Controller, @Service, @Repository annotations that are specializations of
@Component. Each of these annotations represent a certain layer of your application.
@Component – denotes just an auto scan component.
@Repository – denotes DAO component in the persistence layer.
@Service – denotes a Service component in the business layer.
@Controller – denotes a controller component in the presentation layer.
No comments:
Post a Comment