Spring提供的其它注解

/ Spring / 没有评论 / 240浏览

在上一篇中我们使用了Autowired注解完成了对象的自动注入,并可以使用Qualifier注解帮助我们精确选择我们需要注入的对象。在这一篇中我们了解一下在spring中还提供了哪些注解,来帮助我们便利开发。

Resource注解是Java中提供的,并不是spring中实现的功能,但是用Resource注解依然可以完成对象的注入功能。

@Component("user")
public class User {

private String username;
private String password;
private String nickname;
@Resource
private Work work;

public User() {
setUsername("admin");
setNickname("吉林乌拉");
setPassword("jilinwula");
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public Work getWork() {
return work;
}

public void setWork(Work work) {
this.work = work;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
", work=" + work +
'}';
}
}
<bean name="work" class="com.jilinwula.spring.work.Work" p:address="上海"/>
<context:component-scan base-package="com.jilinwula.spring"/>
User{username='admin', password='jilinwula', nickname='吉林乌拉', work=Work{address='上海'}}

虽然Resource注解也能实现自动注入的功能,但是它和Autowired注解还是有区别的。Autowired注解默认是按对象的类型匹配的。而Resource注解默认是按名字匹配的,也就是对象中定义的实例变量的名字。

设置对象的作用域,在使用上与xml配置的方式相同。

@Scope("prototype")
@Component("user")
public class User {

private String username;
private String password;
private String nickname;
@Autowired
private Work work;

public User() {
setUsername("admin");
setNickname("吉林乌拉");
setPassword("jilinwula");
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public Work getWork() {
return work;
}

public void setWork(Work work) {
this.work = work;
}

}
<context:component-scan base-package="com.jilinwula.spring"/>
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
User user = applicationContext.getBean("user", User.class);
User user2 = applicationContext.getBean("user", User.class);
System.out.println(user);
System.out.println(user2);
}
com.jilinwula.spring.user.User@78a2da20
com.jilinwula.spring.user.User@dd3b207

和xml中的init-method属性功能一样。

@Scope("prototype")
@Component("user")
public class User {

private String username;
private String password;
private String nickname;
@Autowired
private Work work;

public User() {
setUsername("admin");
setNickname("吉林乌拉");
setPassword("jilinwula");
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public Work getWork() {
return work;
}

public void setWork(Work work) {
this.work = work;
}

@PostConstruct
public void init() {
System.out.println("PostConstruct");
}

}
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
PostConstruct
com.jilinwula.spring.user.User@7364985f

和xml中的destroy-method属性使用一致。

@Component("user")
public class User {

private String username;
private String password;
private String nickname;
@Autowired
private Work work;

public User() {
setUsername("admin");
setNickname("吉林乌拉");
setPassword("jilinwula");
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public Work getWork() {
return work;
}

public void setWork(Work work) {
this.work = work;
}

@PostConstruct
public void init() {
System.out.println("PostConstruct");
}

@PreDestroy
public void destroy() {
System.out.println("PreDestroy");
}

}
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
((ClassPathXmlApplicationContext) applicationContext).destroy(); // 关闭IOC容器
}
PostConstruct
PreDestroy