spring注解

Spring注解扫盲墙

以下列举了这几天碰到的一些关于Spring注解的用法,注解在之前一直没有系统接触过,现在为了弥补,特意在这里动态更新平时遇到的注解,目的是为了见一个学一个,动态扩容!

@Autowired

Autowired注解就是从spring容器当中去找到对应的Bean去定义变量,而Bean来自于Spring的xml配置文件,只有在xml文件中配置了某个类的Bean,该Bean才会出现在spring容器中。 !非规范使用容易导致Bean注入失败。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.spring.model;
import org.springframework.beans.factory.annotation.Autowired;
public class Zoo {
// 将@Autowired注解的required属性设置为false 找不到Bean定义为null
@Autowired(required=false)
private Tiger tiger;

@Autowired(required=false)
private Monkey monkey;

public String toString(){
return tiger + "\n" + monkey;
}

}

@Qualifier

Qualifier用于在有多个实现类的Bean情况下指定Bean的名称

1
2
3
public interface Icar{}
public BMWCar implements Icar{}
public BenzCar implements ICar{}

因为Icar接口有两个实现类 BMWCar 和 BenzCar,当两个实现类都在xml中创建了Bean(如下所示),@Autowired注解就无法辨别使用哪一个Bean来创建对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

<context:component-scan base-package="com.spring" />

<!-- Autowired注解配合Qualifier注解 -->
<bean id="carFactory" class="com.spring.model.CarFactory" />
<bean id="bmwCar" class="com.spring.service.impl.BMWCar" />
<bean id="benz" class="com.spring.service.impl.BenzCar" />

</beans>

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.spring.model;
import org.springframework.beans.factory.annotation.Autowired;
import com.spring.service.ICar;

public class CarFactory {

@Autowired
private ICar car;
public String toString(){
return car.getCarName();
}

}

这种情况下,就可以使用@Qualifier 来指定其中一个Bean 来创建对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.spring.model;
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier;
import com.spring.service.ICar;
public class CarFactory {

@Autowired
@Qualifier("bmwCar")
private ICar car;

public String toString(){
return car.getCarName();
}

}

@service

使用@Service,声明@Service修饰的类名是一个Bean,这样xml不用配置就可以使用@Autowired

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.spring.model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

// @Service("Zoo")
// @Scope("prototype") // @Scope singleton代表单例 prototype代表原型
@Service // 默认Bean的id是zoo, 我可以通过上述注释方法将Bean的id设为Zoo
public class Zoo {

@Autowired
private Tiger tiger;

@Autowired
private Monkey monkey;

public String toString(){
return tiger + "\n" + monkey;
}

}

@Controller 一般用于Consumer层

1
2
3
4
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User>{
}

可以使@Scope(VALUE) VALUE=’singleton’为单例模式 VALUE=’prototype’保证当前有请求时都会创建一个Action对象,从而保证每一个请求都有一个单独的Action来处理,避免公用一个Action带来的线程安全问题

注解扫描

1
<context:component-scan base-package="com.spring" />

@Data @AllArgsConstructor @NoArgsConstructor

这三个是Lombok插件的注解,具体用法如下所示,直接在User类中 定义其应该有的属性,而不必写setter和getter方法,此外这里还有一个关于DAO层查询数据库返回对象的问题,使用该注释可以避免出现返回class对象字段缺失或者其他报错的问题。使用@NoArgsConstructor 可以定义空的类初始化方法

!需要注意的点: class 中的属性顺序应该和定义表时的属性顺序尽可能一致,否则可能出现错误(踩过坑)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable{

/**
* 用户数据结构:
* userId : 用户id 主键
* userName : 用户名
* userPwd : 用户pwd
*/
private int id;
private String userName;
private String userPwd;
}

@Resource