[Spring]手工装配之注解装配/@Resource/@Autowired/@Qualifier
程序员文章站
2022-05-23 21:49:55
...
手工xml装配太太太太太麻烦了……注解应运而生。并且自动装配不是很好……
用了注解就只需要在xml配置bean即可,其他都不需要加了
基本xml头
<?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: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:annotation-config />
</beans>
注解
@[email protected]一般同用
单个@Autowired默认按类型装配
两个配合为按名称,效果作用同@Resource
形如:
@Autowired @Qualifier("student")
package com.yiki.beanimp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.yiki.bean.TeacherInterface;
public class Teacher implements TeacherInterface {
@Autowired @Qualifier("student")
private Student student;
public Teacher() {
// TODO Auto-generated constructor stub
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
/* (non-Javadoc)
* @see com.yiki.beanimp.TeacherInterface#Tsave()
*/
@Override
public void Tsave(){
System.out.println("teacher");
student.SSave();
}
}
@Resource
默认按名称装配
package com.yiki.beanimp;
import javax.annotation.Resource;
import com.yiki.bean.StudentInterface;
public class Student implements StudentInterface {
/*###################################################################
* ### @Resource(name="")如果name不指定,默认为属性名(就是monitor,而不是类类型名
* ###如果找不到,它会自动根据类型就是Monitor来找
* ### 可以用在set方法上或者成员定义上,二选一
* ###############################################################
* */
private String sname;
@Resource
private Monitor monitor;
public Monitor getMonitor() {
return monitor;
}
public void setMonitor(Monitor monitor) {
this.monitor = monitor;
}
public Student() {
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Override
public void SSave() {
System.out.println("Student");
monitor.Msave();
}
}
package com.yiki.beanimp;
import com.yiki.bean.MonitorInterface;
public class Monitor implements MonitorInterface {
private String monitor;
public Monitor() {
}
public String getMonitor() {
return monitor;
}
public void setMonitor(String monitor) {
this.monitor = monitor;
}
/* (non-Javadoc)
* @see com.yiki.beanimp.MonitorInterface#Msave()
*/
@Override
public void Msave(){
System.out.println("monitor");
}
}
上一篇: 手工注入(未完)
下一篇: 接口初心者心得第二版
推荐阅读
-
Spring注解之@Autowired、@Qualifier、@Resource、@Value
-
Spring框架学习之Spring @Autowired实现自动装配的代码
-
Spring注解:自动装配(@Autowired+@Qualifier+@Primary、@Resource、@Inject)
-
Spring注解之@Autowired、@Qualifier、@Resource、@Value
-
[Spring]手工装配之注解装配/@Resource/@Autowired/@Qualifier
-
Spring注解:自动装配(@Autowired+@Qualifier+@Primary、@Resource、@Inject)
-
Spring框架学习之Spring @Autowired实现自动装配的代码