Four basic annotations in SPRINGMVC:
@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!!
This is about the role of these four annotations in Springmvc, in fact srping the role of these four annotations and springmvc the same.
Example:
1. Control layer
@Controller//Comment as Controller
@RequestMapping ("/login")
public class Loginaction {
@Autowired
@Qualifier ("UserService")//Note Specify the injected Bean
Private Iuserservice UserService;
。。。。。。 Other slightly ...
}
2. Business Logic Layer
@Service ("UserService")
public class Userserviceimpl implements Iuserservice {
@Autowired
@Qualifier ("Userdao")
Private Iuserdao Userdao;
。。。。。。 Other slightly ...
}
3. Persistence Layer
@Repository ("Userdao")
public class Userdaoimpl implements Iuserdao {
private static Logger Logger = Loggerfactory.getlogger (Userdaoimpl.class);
Private DataSource DataSource;
private JdbcTemplate template;
@Autowired
Public Userdaoimpl (DataSource DataSource) {
This.datasource= DataSource;
Template = new JdbcTemplate (This.datasource);
}
Spring's four basic annotations annotation (control layer, business layer, persistence layer) @Component, @