Spring Framework Learning (v) annotations
Annotation Annotation, which is a kind of annotation-like mechanism, adds annotations in code that can be used at a later time. Unlike annotations, annotations are for us, theJava virtual machine does not compile, the annotations are not compiled, but we can read the information in the annotations through the reflection mechanism. Annotations Use the keyword @interfaceto inherit java. lang.annotition.Annotition
The Spring Framework provides us with annotation functionality.
Programming with annotations, primarily to replace XML files, makes development faster. However, the use of XML files is to resolve the modification of the program to modify the source code, and now do not use the XML file, then the open and closed principle is not violated, it is indeed. However, annotations are also good annotations, the use of annotations do not have to configure so many XML files, the most important is the development of high efficiency.
When annotations are not being used, theSpring Framework's configuration file applicationcontext.xml file needs to be configured with a lot of <bean> tags to declare class objects. With annotations, you do not have to add a label pull in the configuration file, which corresponds to adding a description to the corresponding class's comment location. The details are as follows:
the spring framework uses layered annotations.
Persistence layer:@Repository;
Service layer:@Service
Control layer:@Controller
1, using annotations, you need to add namespaces and constraint files to the configuration file
[HTML]View Plaincopyprint?
- <beans ...
- xmlns:context="Http://www.springframework.org/schema/context"
- Xsi:schemalocation= "
- ...
- Http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
- ">
2, tell the framework that those classes are using annotations.
<context:component-scan base-package= "com.lsz. Spring"/>
3, persistent layer annotations
[HTML]View Plaincopyprint?
- Package com.lsz.spring;
- @Repository
- public class userdao{
- 。。。。
- }
@Repository is equivalent to the configuration file in the
[HTML]View Plaincopyprint?
- <Bean id= "Userdao" class="Com.lsz.spring.UserDao" />
4, Service layer annotations
[HTML]View Plaincopyprint?
- @Service (value="Testservice")
- Public Classtestservice {
- @Resource//equivalent to automatic assembly
- Private Userdao Userdao;
- Public Userdao Getuserdao () {
- Returnuserdao;
- }
- public void Setuserdao (Userdao Userdao) {
- this.userdao= Userdao;
- }
- }
@Resource the combination of inter-object relationships, the default is the byname way to assemble, if the associated object is not found by name, then Bytype continues to find.
@Service annotations are equivalent to
[HTML]View Plaincopyprint?
- <Bean id= "testservice" class="Com.lsz.spring.UserService" />
5, Control layer annotations
[HTML]View Plaincopyprint?
- @Controller (value="ua")
- @Scope (value="prototype")
- public class Useraction {
- @Resource
- Private UserService UserService;
- Public UserService Getuserservice () {
- Returnuserservice;
- }
- }
@Controller annotations are equivalent to
[HTML]View Plaincopyprint?
- <Bean id="ua" class="com.lsz.spring.UserAction" />
The annotation keywords in these three layers can be replaced with @Component .
using annotations to declare an object, the ID name generated by default is called the first letter of the class name lowercase.
6, gets the Action object from the Spring environment .
[Java]View Plaincopyprint?
- ServletContext application =request.getsession (). Getservletcontext ();
- Applicationcontextac = Webapplicationcontextutils.getwebapplicationcontext (application);
- Useraction useraction = (useraction) ac.getbean ("UA"); Get control Layer Object
- Response.setcontenttype ("TEXT/HTML;CHARSET=GBK"); Set Encoding
- PrintWriter out =response.getwriter ();
- Print three layers of objects separately.
- Out.println ("Action:" +useraction);
- Out.println ("Service:" +useraction.getuserservice ());
- Out.println ("Dao:" +useraction.getuserservice (). Getuserdao ());
Spring Framework Learning (v) annotations