spring/java ---->记录和整理用过的注解以及spring装配bean方式

spring注解

@Scope:该注解全限定名称是:org.springframework.context.annotation.Scope。@Scope指定Spring容器如何创建Bean的实例,Singleton(spring默认的创建Bean实例的方式),Prototype,Request,Session,GlobalSession。

@Bean:该注解全限定名称是:org.springframework.context.annotation.Bean。在方法上使用该注解,表示方法返回值是一个Bean。和注解@Configuration使用可实现Java配置

@ComponentScan:该注解全限定名称是:org.springframework.context.annotation.ComponentScan。自动扫描包名下所有使用了声明式注解的类,并注册为Bean

@Configuration:该注解全限定名称是:org.springframework.context.annotation.Configuration。声明当前类是一个配置类。和注解@Bean使用可实现Java配置

@PropertySource:该注解全限定名称是:org.springframework.context.annotation.PropertySource。指定配置文件的位置

@Primary:该注解全限定名称是:org.springframework.context.annotation.Primary。注解在方法上。当按类型自动装配可能会导致多个候选者,@Primary可以实现优先选择特定的bean

@Profile:该注解全限定名称是:org.springframework.context.annotation.Profile。注解在类或者方法上。实现在不同情况下实例化不同的Bean。可以实现根据环境的不同使用不同的配置

@Conditional:该注解全限定名称是:org.springframework.context.annotation.Conditional。实现在满足一定条件的情况下创建一个特定的Bean

@Async:该注解全限定名称是:org.springframework.scheduling.annotation.Async。可以注解在方法或者类上。表示该方法是异步的

@EnableAsync:该注解全限定名称是:org.springframework.scheduling.annotation.EnableAsync。开启对异步任务支持

@Scheduled:该注解全限定名称是:org.springframework.scheduling.annotation.Scheduled。注解在方法上。表示该方法是计划任务

@EnableScheduling:该注解全限定名称是:org.springframework.scheduling.annotation.EnableScheduling。开启对计划任务支持

@Value:该注解全限定名称是:org.springframework.beans.factory.annotation.Value。@Value注入各种资源,例如,字符串,配置文件,操作系统属性等

@Autowired:该注解全限定名称是:org.springframework.beans.factory.annotation.Autowired。注入Bean。可在set方法或者属性上使用

@Service:该注解全限定名称是:org.springframework.stereotype.Service。声明Bean。业务逻辑层使用

@Repository:该注解全限定名称是:org.springframework.stereotype.Repository。声明Bean。持久层使用

@Controller:该注解全限定名称是:org.springframework.stereotype.Controller。声明Bean。控制层使用

@Component:该注解全限定名称是:org.springframework.stereotype.Component。声明Bean。组件,角色不明确

@Enable*  开启某一项功能。

@EnableAspectJAutoProxy:开启Spring对AspectJ的支持

@EnableAsync:开启对异步方法的支持

@EnableScheduling:开启对计划任务的支持

@EnableWebMvc:开启对web mvc的配置支持

@EnableJpaRepository:开启对Spring data Jpa repository的支持

@EnableTransactionManagement:开启注解式事务的支持

@EnableCaching:开启注解式缓存的支持

。。。

上面这些注解的共同特点是都是组合注解,都有一个@Import注解,用它来导入配置类

mvc

@EnableWebMvc:该注解全限定名称是:org.springframework.web.servlet.config.annotationEnableWebMvc。开启对web mvc的配置支持

@RequestMapping:该注解全限定名称是:org.springframework.web.bind.annotation.RequestMapping。注解在类或者方法上。映射web请求路径

@ResponseBody:该注解全限定名称是:org.springframework.web.bind.annotation.ResponseBody。注解在方法上或者返回值前。将返回的结果放在response中,而不是放在一个页面中

@RequestBody:该注解全限定名称是:org.springframework.web.bind.annotation.RequestBody。注解在参数前面。将请求的参数放在request中,而不是放在url中

@PathVariable:该注解全限定名称是:org.springframework.web.bind.annotation.PathVariable。注解在参数前面。获得url路径中的动态参数

@RequestParam:该注解全限定名称是:org.springframework.web.bind.annotation.RequestParam。注解在参数前面。将url请求中的参数(key=value)赋值给方法中的形式参数

@Controller:该注解全限定名称是:org.springframework.stereotype.Controller。声明Bean。控制层使用

@RestController:该注解全限定名称是:org.springframework.web.bind.annotation.RestController。@ResponseBody和@Controller的组合注解。注解在类或者方法上。将返回的结果放在response中,而不是放在一个页面中

全局处理

@ControllerAdvice:该注解全限定名称是:org.springframework.web.bind.annotation.ControllerAdvice。可以对控制器进行全局配置。

@ExceptionHandler:该注解全限定名称是:org.springframework.web.bind.annotation.ExceptionHandler。用于全局处理控制器的异常

@ModelAttribute:该注解全限定名称是:org.springframework.web.bind.annotation.ModelAttribute。1、绑定键值对到model对象。2、让全局的RequestMapping获得此处设置的键值对

test

@ContextConfiguration:该注解全限定名称是:org.springframework.test.context.ContextConfiguration。加载配置ApplicationContext

@ActiveProfiles:该注解全限定名称是:org.springframework.test.context.ActiveProfiles。确定参与测试时活动的Profile

@WebAppConfiguration :该注解全限定名称是:org.springframework.test.context.web.WebAppConfiguration。用来声明加载的ApplicationContext是一个WebApplicationContext

java注解

JSR-250:

@PostConstruct:该注解全限定名称是:javax.annotation.PostConstruct。用于Bean的生命周期的操作。在构造方法执行之后执行

@PreDestroy:该注解全限定名称是:javax.annotation.PreDestroy。用于Bean的生命周期的操作。在销毁方法执行之前执行

@Resource:该注解全限定名称是:javax.annotation.Resource。注入Bean。可在set方法或者属性上使用

JSR-330

@Inject:该注解全限定名称是:javax.inject.Inject。注入Bean。可在set方法或者属性上使用

test(junit)

@RunWith:该注解全限定名称是:org.junit.runner.RunWith。在Junit环境下提供一个spring测试上下文环境

@Test:该注解全限定名称是:org.junit.Test。注解在方法上。表示这是需要进行测试的

@Before:该注解全限定名称是:org.junit.Before。注解在方法上。表示进行测试之前需要做的准备,加载配置文件等

  

上面的注解所在的jar:

spring-context-4.3.18.RELEASE.jar

spring-beans-4.3.18.RELEASE.jar

spring-web-4.3.18.RELEASE.jar

spring-webmvc-4.3.18.RELEASE.jar

spring-test-4.3.18.RELEASE.jar

junit-4.12.jar

jsr250-api-1.0.jar

javaee-api-7.0.jar

spring装配方式

1、通过xml进行显式装配:xml文件+java文件

xml配置

1 <bean id="abc" class="a.b.C"/>
2 <bean id="xyz" class="x.y.Z">
3      <properpty name="c" ref="abc"/> <!--引用bean abc-->
4 </bean>

java文件:指定set方法

 1 package x.y;
 2 
 3 import a.b.C;
 4 
 5 public class Z{  //注入类a.b.C
 6 
 7   private C c;
 8   public void setC(C c){
 9      this.c = c;
10   }
11 
12 }

 2、通过注解方式装配bean

xml配置

1 <context:component-scan base-package="a.b,x.y"/> <!--开启注解扫描-->
2 
3 <bean id="abc" class="a.b.C"/>
4 <bean id="xyz" class="x.y.Z"/> <!--没有引用bean abc-->

java文件

 1 package x.y;
 2  
 3 import a.b.C;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 public class Z{  //注入类a.b.C
 6 
 7 private C c;
 8 
 9 @Autowired //由@Autowired注入类C,方法级别上的注解???
10 public void setC(C c){
11       this.c = c;
12   }
13 
14  }

猜你喜欢

转载自www.cnblogs.com/mrray1105/p/9660297.html