Home  >  Article  >  Java  >  Spring Framework Learning (5) Annotations

Spring Framework Learning (5) Annotations

黄舟
黄舟Original
2016-12-29 13:17:571267browse

Annotation is a mechanism similar to comments. Adding annotations to the code can use the information at a later time. Different from annotations, annotations are for us to see. The Java virtual machine will not compile, and annotations will not be compiled, but we can read the information in the annotations through the reflection mechanism. Annotations use the keyword @interface and inherit java.lang.annotition.Annotition


The spring framework provides us with the annotation function.
Using annotation programming is mainly to replace xml files and make development faster. However, the use of xml files is to modify the program to modify the source code. If you don't use xml files now, wouldn't it violate the opening and closing principle? It is true. However, annotations are also better than annotations. Using annotations does not require configuring so many xml files. The most important thing is high development efficiency. .
When annotations are not used, many 60e23eb984d18edbb092da6b8f295aba tags need to be configured in the spring framework's configuration file applicationContext.xml file to declare class objects. Using annotations, you do not need to add tags in the configuration file. Instead, add instructions in the "annotation" position of the corresponding class. The specific introduction is as follows:

The spring framework uses layered annotations.
Persistence layer: @Repository;
Service layer: @Service
Control layer: @Controller

1, to use annotations, you need to add namespace and constraint files to the configuration file

<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 which classes use annotations.

<context:component-scan base-package="com.lsz.spring" />

3, persistence layer annotation

Package com.lsz.spring;
 
@Repository
public class UserDao{
//。。。。
}

@Repository
is equivalent to

<bean id="userDao" class="com.lsz.spring.UserDao" />

4 in the configuration file, service layer annotation

@Service(value="testService")
public classTestService {
 
@Resource//相当于自动装配
private UserDao userDao ;
 
      public UserDao getUserDao() {
              returnuserDao;
      }
      public void setUserDao(UserDao userDao) {
             this.userDao= userDao;
      }
 
}
@Resource
The combination of relationships between objects is assembled by byName by default. If the associated object cannot be found based on the name, byType will be used to continue the search. The

@Service annotation is equivalent to
<bean id="testService" class="com.lsz.spring.UserService" />
5, and the control layer annotation
@Controller(value="ua")
@Scope(value="prototype")
public class UserAction {
 
     @Resource
     private UserService userService ;
 
     public UserService getUserService() {
          returnuserService;
     }
}

@Controller annotation is equivalent to

<bean id="ua" class="com.lsz.spring.UserAction " />


The annotation keywords in these three layers can be replaced by @Component.
Use annotations to declare objects. By default, the generated id name is the first letter of the class name in lowercase.

6, get the Action object from the Spring environment.
ServletContext application =request.getSession().getServletContext();
ApplicationContextac = WebApplicationContextUtils.getWebApplicationContext(application);
 
UserAction useraction = (UserAction)ac.getBean("ua");//获取控制层对象

response.setContentType("text/html;charset=GBK");//设置编码
PrintWriter out =response.getWriter();

//分别将三个层的对象打印出来。
out.println("Action:"+userAction);
out.println("Service:"+userAction.getUserService());
out.println("Dao:"+userAction.getUserService().getUserDao());

The above is the content of spring framework learning (5) annotations. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn