Spring项目基础注解
SpringBoot注解
@SpringBootApplication
包含了@ComponentScan
、@Configuration
和@EnableAutoConfiguration
注解Configuration
注解通常与@Bean
一起使用,使用这两个注解可以创建一个简单的Spring配置类,替代Xml配置,也可以使用@ImportResource
注解加载xml配置文件。下面两种效果是一样的:
<bean id="quartzScheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="autoStartup" value="true" /> <property name="startupDelay" value="20" /> <property name="overwriteExistingJobs" value="true" /> <property name="applicationContextSchedulerContextKey" value="applicationContextKey" /> <property name="configLocation" value="classpath:quartz.properties"/> </bean> <bean id="xxlJobDynamicScheduler" class="com.xxl.job.admin.core.schedule.XxlJobDynamicScheduler" init-method="init" destroy-method="destroy" > <property name="scheduler" ref="quartzScheduler"/> <property name="accessToken" value="" /> </bean>
@Configuration @AutoConfigureAfter(MybatisConfiguration.class) public class JobConfig implements EnvironmentAware { private RelaxedPropertyResolver propertyResolver; private String configLocation; @Bean public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) throws Exception{ SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); schedulerFactoryBean.setDataSource(dataSource); schedulerFactoryBean.setAutoStartup(true); schedulerFactoryBean.setStartupDelay(20); schedulerFactoryBean.setOverwriteExistingJobs(true); schedulerFactoryBean.setConfigLocation(new ClassPathResource(configLocation)); return schedulerFactoryBean; } @Bean(initMethod = "init", destroyMethod = "destroy") public XxlJobDynamicScheduler xxlJobDynamicScheduler(SchedulerFactoryBean schedulerFactoryBean){ XxlJobDynamicScheduler xxlJobDynamicScheduler = new XxlJobDynamicScheduler(); xxlJobDynamicScheduler.setScheduler(schedulerFactoryBean.getScheduler()); xxlJobDynamicScheduler.setAccessToken(""); return xxlJobDynamicScheduler; } @Override public void setEnvironment(Environment environment) { this.propertyResolver = new RelaxedPropertyResolver(environment, null); this.configLocation = propertyResolver.getProperty("quartz.configLocation"); } }
@EnableAutoConfiguration
这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring,由于 spring-boot-starter-web 添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。Starter POMs和Auto-Configuration:设计auto-configuration的目的是更好的使用”Starter POMs”,但这两个概念没有直接的联系。你可以自由地挑选starter POMs以外的jar依赖,并且Spring Boot将仍旧尽最大努力去自动配置你的应用。参考spring.factories
@ComponentScan
注解 见下文 Bean 声明注解
@Value
获取application.properties配置的属性的值@Value("${redis.cache-expire}") private int cacheExpire;
Bean 声明注解
@Service
业务逻辑层@Component
组件(不容易归类可以使用该组件)@Repository
数据访问层@Controller
Spring mvc 展现层@Configurable
声明当前类是一个配置类@ComponentScan("com.yss.ms")
设置自动扫描包下面所有的 (@Service @Component @Repository @Controller
) 使用springboot开发时候,启动类会隐式的指定了扫描时basePackage的路径,参考@SpringBootApplication@EnableAsync
开启异步任务支持@PropertySource("classpath:application.properties")
用于指定目录,指定编码读取properties文件
@Component
@PropertySource("classpath:application.properties",encoding="utf-8")
public class Application {
@Value("${example.name}")
private String name;
@EnableScheduling
注解开启对计划任务的支持
Bean 注入注解
@Autowired
Spring 提供 等同autowire=byType,根据类型的自动注入依赖@Qualifier
Spring 提供 等同autowire=byName,当@Autowired
注解需要判断多个 bean类型相同时,就需
要使用@Qualifier(“xxBean”)
来指定依赖的bean的id@Resource
属于JSR-250标准 作用同@Autowired
,是属于byName类 型的依赖注入,使用方式:
@Resource(name=”xxBean”)
,不带参数是默 认类名首字母小写@Value("xxxx")
注入普通字符串@Value("${xxx.xxx}")
注入配置文件中字符串@PostConstruct
标注在方法上,在构造函数执行完毕后执行@PreDestroy
标注在方法上,销毁前执行@Async
异步方法表明,若是在class上则全是@Scheduled
声明方法是计划任务@Conditional()
条件注解,当满足某条件时
Spring MCV
@RequestMapping
用于映射Web请求 返回体,编码格式都可以在此处设置
produces = {} 设置返回值json/xml charset 等
@RestControlle
组合注解,组合了@Controller和@ResponseBody@ResponseBody
将Controller的方法返回的对象通过适当的转换(通过配置可以返回Json或XML数据),写入
response对象的body数据区@RequestBody
用于读取request请求的body部分数据(Json串或XML数据),将其转化为需要的对象@PathVariable
用来接收路径参数,api/{id}
JUnit4 注解
@After
如果在@Before
注解方法中分配了额外的资源,那么在测试执行完后,需要释放分配的资源,使用@After
注解一个public void方法会使该方法在@Test
注解方法执行后被执行AfterClass
如果在@BeforeClass
注解方法中分配了代价高昂的额外的资源,那么在测试类中的所有测试方法执行完后,需要释放分配的资源,使用@AfterClass
注解一个public static void方法会使该方法在测试类中的所有测试方法执行完后被执行@Before
使用@Before
注解一个public void 方法会使该方法在@Test
注解方法被执行前执行@BeforeClass
使用@BeforeClass
注解一个public static void 方法,并且该方法不带任何参数,会使该方法在所有测试方法被执行前执行一次,并且只执行一次@Ignore
对包含测试类的类或@Test
注解方法使用@Ignore
注解将使被注解的类或方法不会被当做测试执行@Test
@Test
注解的public void方法将会被当做测试用例,JUnit每次都会创建一个新的测试实例,然后调用@Test
注解方法@RunWith
指定测试类使用某个运行器,如:@RunWith(SpringRunner.class)