@Component, @Repository @Service, @Controller
Look at the literal meaning, easy but not out of three of them:
@Controller Control layer, that's our action layer.
@Service The business Logic layer, which is our service or manager layer
@Repository Persistence layer, which is what we often call the DAO layer
and @Component (literally a component), it's used when you can't decide which layer to use.
In fact, these four annotations have the same effect, and spring will load them as a bean that needs to be injected into the context;
However, in the project, it is recommended that you use the project in strict accordance with the meanings of the remaining three annotations except Componen. This is good for a layered Web architecture!!
Example:
1. Control layer
// Note for controller @RequestMapping ("/login") Publicclass loginaction {@Autowired @Qualifier ("userservice"// Note specifying the injected Bean Private Iuserservice UserService; 。。。。。。 Other slightly ... }
2. Business Logic Layer
@Service ("userservice") publicclass Userserviceimpl Implements Iuserservice {@Autowired @Qualifier ("userdao") Private Iuserdao Userdao; 。。。。。。 Other slightly ... }
3. Persistence Layer
@Repository ("Userdao") Public classUserdaoimpl implements Iuserdao {Private StaticLogger Logger = Loggerfactory.getlogger (Userdaoimpl.class); PrivateDataSource DataSource; Privatejdbctemplate template; @Autowired PublicUserdaoimpl (DataSource DataSource) { This. datasource=DataSource; Template=NewJdbcTemplate ( This. DataSource); }
Four basic annotations in SPRINGMVC