抄袭来源:https://blog.csdn.net/weixin_39805338/article/details/80770472

1.@Configuration注解

该类等价 与XML中配置beans,相当于Ioc容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean,与xml中配置的bean意思一样。
@Configuration注解的类必需使用扫描.如下:

  1. @Configuration
  2. public class MainConfig {
  3. //在properties文件里配置
  4. @Value(“${wx_appid}”)
  5. public String appid;
  6. protected MainConfig(){}
  7. @Bean
  8. public WxMpService wxMpService() {
  9. WxMpService wxMpService = new WxMpServiceImpl();
  10. wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
  11. return wxMpService;
  12. }
  13. }

3.

@Controller,

@Service,

@Repository,

@Component

目前4种注解意思是一样,并没有什么区别,区别只是名字不同。使用方法:
1.使用扫描被注解的类
2. 在类上写注解:

  1. @Controller
  2. public class TestController {
  3. }

4. @PostConstruct 和 @PreDestory


实现初始化和销毁bean之前进行的操作,只能有一个方法可以用此注释进行注释,方法不能有参数,返回值必需是void,方法需要是非静态的。
例如:

  1. public class TestService {
  2. @PostConstruct
  3. public void init(){
  4. System.out.println("初始化");
  5. }
  6. @PreDestroy
  7. public void dostory(){
  8. System.out.println("销毁");
  9. }
  10. }

@PostConstruct:在构造方法和init方法(如果有的话)之间得到调用,且只会执行一次。
@PreDestory:注解的方法在destory()方法调用后得到执行。
Spring注解 - 图1

5. @Primary

自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常。
例如:

  1. @Component
  2. public class Apple implements Fruit{
  3. @Override
  4. public String hello() {
  5. return "我是苹果";
  6. }
  7. }
  8. @Component
  9. @Primary
  10. public class Pear implements Fruit{
  11. @Override
  12. public String hello(String lyrics) {
  13. return "梨子";
  14. }
  15. }
  16. public class FruitService {
  17. //Fruit有2个实例子类,因为梨子用@Primary,那么会使用Pear注入
  18. @Autowired
  19. private Fruit fruit;
  20. public String hello(){
  21. return fruit.hello();
  22. }
  23. }

6. @Lazy(true)

  1. 用于指定该Bean是否取消预初始化,用于注解类,延迟初始化。

7. @Autowired

Autowired默认先按byType,如果发现找到多个bean,则,又按照byName方式比对,如果还有多个,则报出异常。
1.可以手动指定按byName方式注入,使用@Qualifier。
//通过此注解完成从spring配置文件中 查找满足Fruit的bean,然后按//@Qualifier指定pean
@Autowired
@Qualifier(“pean”)
public Fruit fruit;
2.如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false)
public Fruit fruit;

8. @Resource

默认按 byName自动注入,如果找不到再按byType找bean,如果还是找不到则抛异常,无论按byName还是byType如果找到多个,则抛异常。
可以手动指定bean,它有2个属性分别是name和type,使用name属性,则使用byName的自动注入,而使用type属性时则使用byType自动注入。
@Resource(name=”bean名字”)

@Resource(type=”bean的class”)
这个注解是属于J2EE的,减少了与spring的耦合。

9. @Async

  1. java里使用线程用3种方法:<br />1. 继承Thread,重写run方法<br />2. 实现Runnable,重写run方法<br />3. 使用CallableFuture接口创建线程,并能得到返回值。<br />前2种简单,第3种方式特别提示一下,例子如下:
  1. class MyCallable implements Callable<Integer> {
  2. private int i = 0;
  3. // 与run()方法不同的是,call()方法具有返回值
  4. @Override
  5. public Integer call() {
  6. int sum = 0;
  7. for (; i < 100; i++) {
  8. System.out.println(Thread.currentThread().getName() + " " + i);
  9. sum += i;
  10. }
  11. return sum;
  12. }
  13. }

main方法:

  1. public static void main(String[] args) {
  2. Callable<Integer> myCallable = new MyCallable(); // 创建MyCallable对象
  3. FutureTask<Integer> ft = new FutureTask<Integer>(myCallable); //使用FutureTask来包装MyCallable对象
  4. for (int i = 0; i < 100; i++) {
  5. System.out.println(Thread.currentThread().getName() + + i);
  6. if (i == 30) {
  7. Thread thread = new Thread(ft); //FutureTask对象作为Thread对象的target创建新的线程
  8. thread.start(); //线程进入到就绪状态
  9. }
  10. }
  11. System.out.println(“主线程for循环执行完毕..”);
  12. try {
  13. int sum = ft.get(); //取得新创建的新线程中的call()方法返回的结果
  14. System.out.println(“sum = + sum);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. } catch (ExecutionException e) {
  18. e.printStackTrace();
  19. }
  20. }

太多不想写了 参考文章url:https://blog.csdn.net/weixin_39805338/article/details/80770472

10.@Named

@Named和Spring的@Component功能相同。@Named可以有值,如果没有值生成的Bean名称默认和类名相同。比如
@Named public class Person

@Named(“cc”) public class Person

11. @Inject

使用@Inject需要引用javax.inject.jar,它与Spring没有关系,是jsr330规范。
与@Autowired有互换性。

12. @Singleton

只要在类上加上这个注解,就可以实现一个单例类,不需要自己手动编写单例实现类

13.@Valid,@Valided

@Valid

网上一大片使用@Valid失效不能用的情况。为什么呢?
1.@Valid必需使用在以@RequestBody接收参数的情况下。
2.使用ajax以POST方式提示数据,禁止用Fiddler以及浏览器直接访问的方式测试接口
3.用添加注解驱动。
4.@Valid是应用在javabean上的校验。

  1. <dependency>
  2. <groupId>org.hibernate</groupId>
  3. <artifactId>hibernate-validator</artifactId>
  4. <version>4.2.0.Final</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.fasterxml.jackson.core</groupId>
  8. <artifactId>jackson-annotations</artifactId>
  9. <version>2.5.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.fasterxml.jackson.core</groupId>
  13. <artifactId>jackson-core</artifactId>
  14. <version>2.5.3</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.fasterxml.jackson.core</groupId>
  18. <artifactId>jackson-databind</artifactId>
  19. <version>2.5.3</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.codehaus.jackson</groupId>
  23. <artifactId>jackson-mapper-asl</artifactId>
  24. <version>1.9.8</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>com.fasterxml.jackson.module</groupId>
  28. <artifactId>jackson-module-jaxb-annotations</artifactId>
  29. <version>2.5.3</version>

https://blog.csdn.net/weixin_39805338/article/details/80770472