参考资料:
依赖注入和控制反转的理解
https://www.cnblogs.com/xdp-gacl/p/3495887.html
Java开发者必须掌握的20个Spring常用注解
https://www.jianshu.com/p/71e8971d2bc5?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
补充:
1、@responseBody
https://www.jianshu.com/p/1233b22738d8
@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML
数据,需要注意的呢,在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。
@RequestMapping("/login")
@ResponseBody
public User login(User user){
return user;
}
User字段:userName pwd
那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}'
效果等同于如下代码:
@RequestMapping("/login")
public void login(User user, HttpServletResponse response){
response.getWriter.write(JSONObject.fromObject(user).toString());
}
2、@RequestBody
在接收Json格式后需要设置入对象中使用@RequestBody注解,如果要将返回的对象转换为Json格式,需要使用@ResponseBody注解即可。
其实 @RequestBody接收的是一个Json对象的字符串,而不是一个Json对象。然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象变成字符串。同时ajax请求的时候也要指定dataType: "json",contentType:"application/json" 这样就可以轻易的将一个对象或者List传到Java端,使用@RequestBody即可绑定对象或者List.
3、@Repository 与 @Mapper
https://blog.csdn.net/Xu_JL1997/article/details/90934359
@Mapper是Mybatis的注解,在 Spring 程序中,Mybatis 需要找到对应的 mapper,在编译的时候动态生成代理类,实现数据库查询功能,所以我们需要在接口上添加 @Mapper 注解。
@Mapper
public interface UserDao {
...
}
而@Repository是Spring的注解,用在dao层的实现类上,用于生成dao层的bean。
@Repository
public class UserDaoImpl implements UserDao{
@Override
public int insertUser(){
JdbcTemplate template = new JdbcTemplate();
...
}
}
4、@Profile
https://www.jianshu.com/p/75de79fba705
https://www.cnblogs.com/dubhlinn/p/10708240.html
指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
参考示例:
@Configuration
public class AppConfig {
@Profile("english")
@Bean
public English getEnglish() { return new English(); }
@Profile("chinese")
@Bean
public Chinese getChinese() { return new Chinese(); }
}
class Chinese { }
class English { }
// 测试类
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.getEnvironment().setActiveProfiles("chinese");
applicationContext.register(AppConfig.class);
applicationContext.refresh();
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName); // 这里你可以看看打印的bean是否和你想的一样
}
}
}
5、@EnableAsync和@Async
https://www.jianshu.com/p/27a0f69ac6f7
@Async注解的意思是可以异步执行,就是开启多线程的意思。可以标注在方法、类上。
为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync
6、@component
把普通pojo实例化到spring容器中,相当于配置文件中的
<bean id="" class=""/>
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类
下面写这个是引入component的扫描组件 <context:component-scan base-package=”com.mmnc”>
其中base-package为需要扫描的包(含所有子包)
1、@Service用于标注业务层组件
2、@Controller用于标注控制层组件(如struts中的action)
3、@Repository用于标注数据访问组件,即DAO组件.
4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
7、@Scheduled
springboot的schedule和quartz到底怎么选以及如何支持并发和避坑
https://www.jianshu.com/p/61e3abc22fbd