当前位置: 移动技术网 > IT编程>开发语言>Java > Spring注解

Spring注解

2019年10月16日  | 移动技术网IT编程  | 我要评论
@service用于标注业务层组件
@controller用于标注控制层组件(如struts中的action)
@repository用于标注数据访问组件,即dao组件
@component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@autowired后不需要getter()和setter()方法,spring也会自动注入。
在接口前面标上@autowired注释使得接口可以被容器注入,如:
@autowired
@qualifier("chinese")
private man man;
当接口存在两个实现类的时候必须使用@qualifier指定注入哪个实现类,否则可以省略,只写@autowired。
使用@autowired时你的organdaoibatis 必须以@service或@component注解才行。
 
之前用户使用的是3个注解注解他们的main类。分别是@configuration,@enableautoconfiguration,@componentscan。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@springbootapplication。
@springbootapplication = (默认属性)@configuration + @enableautoconfiguration + @componentscan。
 
@springbootapplication
public class applicationmain {
public static void main(string[] args) {
springapplication.run(application.class, args);
}
}
同等于:
@configuration
@enableautoconfiguration
@componentscan
public class applicationmain {
public static void main(string[] args) {
springapplication.run(application.class, args);
}
}
 
分开解释@configuration,@enableautoconfiguration,@componentscan。
1、@configuration:提到@configuration就要提到他的搭档@bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。
派生到我的代码片
<beans>
<bean id = "car" class="com.test.car">
<property name="wheel" ref = "wheel"></property>
</bean>
<bean id = "wheel" class="com.test.wheel"></bean>
</beans>
 
相当于:
派生到我的代码片
@configuration
public class conf {
@bean
public car car() {
car car = new car();
car.setwheel(wheel());
return car;
}
@bean
public wheel wheel() {
return new wheel();
}
}
@configuration的注解类标识这个类可以使用spring ioc容器作为bean定义的来源。@bean注解告诉spring,一个带有@bean的注解方法将返回一个对象,该对象应该被注册为在spring应用程序上下文中的bean。
2、@enableautoconfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。
3、@componentscan:会自动扫描指定包下的全部标有@component的类,并注册成bean,当然包括@component下的子注解@service,@repository,@controller。
 
@responsebody
用该注解修饰的函数,会将结果直接填充到http的响应体中,一般用于构建restful的api;
@controller
用于定义控制器类,在spring 项目中由控制器负责将用户发来的url请求转发到对应的服务接口(service层)。
@restcontroller
@responsebody和@controller的合集
@requestmapping
提供路由信息,负责url到controller中的具体函数的映射。
@enableautoconfiguration
spring boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的spring应用。例如,如果你的classpath下存在hsqldb,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@enableautoconfiguration或者@springbootapplication注解添加到一个@configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@enableautoconfiguration注解的排除属性来禁用它们。例子代码如下:
@componentscan
表示将该类自动发现(扫描)并注册为bean,可以自动收集所有的spring组件,包括@configuration类。我们经常使用@componentscan注解搜索beans,并结合@autowired注解导入。
@configuration
相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@configuration类作为项目的配置主类——可以使用@importresource注解加载xml配置文件。
@springbootapplication
相当于@enableautoconfiguration、@componentscan和@configuration的合集。
@import
用来导入其他配置类。
@importresource
用来加载xml配置文件。
@autowired
自动导入依赖的bean
@service
一般用于修饰service层的组件
@repository
使用@repository注解可以确保dao或者repositories提供异常转译,这个注解修饰的dao或者repositories类会被componetscan发现并配置,同时也不需要为它们提供xml配置项
 
使用mybatis注解需要的配置。如下面的代码所示,使用@mapperscan来扫描注册mybatis数据库接口类,其中basepackages属性表明接口类所在的包,sqlsessiontemplateref表明接口类使用的sqlsessiontemplate。如果项目需要配置两个数据库,@mapperscan也需要分别配置。
@controller用于标注控制层组件(如struts中的action)
@repository用于标注数据访问组件,即dao组件
@component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@autowired后不需要getter()和setter()方法,spring也会自动注入。  
在接口前面标上@autowired注释使得接口可以被容器注入,如:
  1. @autowired  
  2. @qualifier("chinese")  
  3. private man man;   
当接口存在两个实现类的时候必须使用@qualifier指定注入哪个实现类,否则可以省略,只写@autowired。
使用@autowired时你的organdaoibatis 必须以@service或@component注解才行。
 
之前用户使用的是3个注解注解他们的main类。分别是@configuration,@enableautoconfiguration,@componentscan。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@springbootapplication
@springbootapplication = (默认属性)@configuration + @enableautoconfiguration + @componentscan
 
  1. @springbootapplication  
  2. public class applicationmain {  
  3.     public static void main(string[] args) {  
  4.         springapplication.run(application.class, args);  
  5.     }  
  6. }  
同等于:
  1. @configuration
  2. @enableautoconfiguration
  3. @componentscan 
  4. public class applicationmain {  
  5.     public static void main(string[] args) {  
  6.         springapplication.run(application.class, args);  
  7.     }  
  8. }  
 
分开解释@configuration,@enableautoconfiguration,@componentscan。
1、@configuration:提到@configuration就要提到他的搭档@bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。
派生到我的代码片
  1. <beans>  
  2.     <bean id = "car" class="com.test.car">  
  3.         <property name="wheel" ref = "wheel"></property>  
  4.     </bean>  
  5.     <bean id = "wheel" class="com.test.wheel"></bean>  
  6. </beans>  
 
相当于:
派生到我的代码片
  1. @configuration  
  2. public class conf {  
  3.     @bean  
  4.     public car car() {  
  5.         car car = new car();  
  6.         car.setwheel(wheel());  
  7.         return car;  
  8.     }  
  9.     @bean   
  10.     public wheel wheel() {  
  11.         return new wheel();  
  12.     }  
  13. }  
@configuration的注解类标识这个类可以使用spring ioc容器作为bean定义的来源。@bean注解告诉spring,一个带有@bean的注解方法将返回一个对象,该对象应该被注册为在spring应用程序上下文中的bean。
2、@enableautoconfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。
3、@componentscan:会自动扫描指定包下的全部标有@component的类,并注册成bean,当然包括@component下的子注解@service,@repository,@controller。
 
  1. @responsebody 
用该注解修饰的函数,会将结果直接填充到http的响应体中,一般用于构建restful的api;
  1. @controller 
用于定义控制器类,在spring 项目中由控制器负责将用户发来的url请求转发到对应的服务接口(service层)。
  1. @restcontroller 
@responsebody和@controller的合集
  1. @requestmapping 
提供路由信息,负责url到controller中的具体函数的映射。
  1. @enableautoconfiguration 
spring boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的spring应用。例如,如果你的classpath下存在hsqldb,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@enableautoconfiguration或者@springbootapplication注解添加到一个@configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@enableautoconfiguration注解的排除属性来禁用它们。例子代码如下:
  1. @componentscan 
表示将该类自动发现(扫描)并注册为bean,可以自动收集所有的spring组件,包括@configuration类。我们经常使用@componentscan注解搜索beans,并结合@autowired注解导入。
  1. @configuration 
相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@configuration类作为项目的配置主类——可以使用@importresource注解加载xml配置文件。
  1. @springbootapplication 
相当于@enableautoconfiguration、@componentscan和@configuration的合集。
  1. @import 
用来导入其他配置类。
  1. @importresource 
用来加载xml配置文件。
  1. @autowired 
自动导入依赖的bean
  1. @service 
一般用于修饰service层的组件
  1. @repository 
使用@repository注解可以确保dao或者repositories提供异常转译,这个注解修饰的dao或者repositories类会被componetscan发现并配置,同时也不需要为它们提供xml配置项
 
使用mybatis注解需要的配置。如下面的代码所示,使用@mapperscan来扫描注册mybatis数据库接口类,其中basepackages属性表明接口类所在的包,sqlsessiontemplateref表明接口类使用的sqlsessiontemplate。如果项目需要配置两个数据库,@mapperscan也需要分别配置。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网