最全的JavaSpring注解

  • 格式:docx
  • 大小:325.09 KB
  • 文档页数:36

最全的JavaSpring注解

注解是个好东西,但好东西我们也是看见过,整理过,理解过,用过才知道好。不求我们每个都记住,但求保有印象,在需要的时候能提取出来再查找相关资料,平时工作就不会显得那么被动了。

1.@Configuration注解

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

@Configuration注解的类必需使用扫描.如下:

[html] view plain copy

print?

1. @Configuration

2. public class MainConfig {

3.

4. //在properties文件里配置

5. @Value("${wx_appid}")

6. public String appid;

7.

8. protected MainConfig(){}

9.

10. @Bean

11. public WxMpService wxMpService() {

12. WxMpService wxMpService = new WxMpServiceImpl();

13. wxMpService.setWxMpConfigStorage(wxMpConfigStorage());

14. return wxMpService;

15. } 16. }

定义一个MainConfig,用@Configuration注解,那MainConfig相当于xml里的beans,里面用@Bean注解的和xml里定义的bean等价,用扫描该类,最终我们可以在程序里用@AutoWired或@Resource注解取得用@Bean注解的bean,和用xml先配置bean然后在程序里自动注入一样。目的是减少xml里配置。

2.@Value注解

为了简化从properties里取配置,可以使用@Value, 可以properties文件中的配置值。

在dispatcher-servlet.xml里引入properties文件。

[html] view plain copy

print?

1.

在程序里使用@Value:

@Value("${wx_appid}")

publicString appid;

即使给变量赋了初值也会以配置文件的值为准。

3. @Controller, @Service, @Repository,@Component

目前4种注解意思是一样,并没有什么区别,区别只是名字不同。使用方法:

1.使用扫描被注解的类

2. 在类上写注解: @Controller

public class TestController {

}

4. @PostConstruct 和 @PreDestory

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

例如:

[html] view plain copy

print?

1. public class TestService {

2.

3. @PostConstruct

4. public void init(){

5. System.out.println("初始化");

6. }

7.

8. @PreDestroy

9. public void dostory(){

10. System.out.println("销毁");

11. }

12. }

@PostConstruct:在构造方法和init方法(如果有的话)之间得到调用,且只会执行一次。

@PreDestory:注解的方法在destory()方法调用后得到执行。

网上拷贝的流程图:

引深一点,Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种:

1.通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;

2.通过 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;

3.在指定方法上加上@PostConstruct 或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用

但他们之前并不等价。即使3个方法都用上了,也有先后顺序.

Constructor > @PostConstruct >InitializingBean > init-method

5. @Primary

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

例如:

[html] view plain copy

print?

1. @Component

2. public class Apple implements Fruit{

3.

4. @Override

5. public String hello() {

6. return "我是苹果";

7. }

8. }

9.

10. @Component

11. @Primary 12. public class Pear implements Fruit{

13.

14. @Override

15. public String hello(String lyrics) {

16. return "梨子";

17. }

18. }

19.

20. public class FruitService {

21.

22. //Fruit有2个实例子类,因为梨子用@Primary,那么会使用Pear注入

23. @Autowired

24. private Fruit fruit;

25.

26. public String hello(){

27. return fruit.hello();

28. }

29. }

6. @Lazy(true)

用于指定该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

java里使用线程用3种方法:

1. 继承Thread,重写run方法

2. 实现Runnable,重写run方法

3. 使用Callable和Future接口创建线程,并能得到返回值。

前2种简单,第3种方式特别提示一下,例子如下:

[html] view plain copy

print? 1. class MyCallable implements Callable {

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方法:

[html] view plain copy

print?

1. public static void main(String[] args) {

2. Callable myCallable = new MyCallable(); // 创建MyCallable对象

3. FutureTask ft = new FutureTask(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(); //线程进入到就绪状态