一、配置文件中添加 配置 <context:component-scan base-package=“com.test”/> 扫描com.test包中代码中的注解
@Component 是所有受Spring 管理组件的通用形式,不推荐使用
@Repository 对应数据层Bean
@Service 业务层Bean
@Controller 控制层Bean
如果不指定value,如: @Repositor 则生成的bean为类名(小写开头)
如果@Repository(“dao”) 则生成名为dao的bean
默认生成的bean为singleton类型,可用@Scope("prototype")进行声名
二、配置bean依赖关系
@Autowired 根据bean 类型从spring 上下文中进行查找,按byType自动注入
1、如果某个类的bean是唯一的,则可直接注入
@Autowired
private Student student
2、如果不是唯一的话,可用@Qualifier来指定注入Bean的名字
@Autowired
@Qualifier("student1")
private Student student
3、@Required 只能放置在setter方法上,且必须通过XML配置的setter注入
@Required
public void setAge(String age) {
this.age = age;
}
public void setAge(String age) {
this.age = age;
}
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="bupt.lesson3"/> </beans>
package bupt.lesson3; import org.springframework.beans.factory.annotation.Required; import org.springframework.stereotype.Repository; @Repository("student") public class Student { private int id; private String name; private String age; public Student(){ } public Student(int id, String name, String age) { this.id = this.id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
package bupt.lesson3; import org.springframework.stereotype.Repository; @Repository("studentDao") public class StudentDaoImpl implements StudentDao { @Override public boolean saveStudents(Student s) { // TODO Auto-generated method stub if (s != null){ System.out.println("学号=" + s.getId()); System.out.println("姓名=" + s.getName()); System.out.println("年龄=" + s.getAge()); return true; } return false; } }
package bupt.lesson3; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @Service("studentService") @Scope("prototype") public class StudentService { @Autowired @Qualifier("studentDao") private StudentDao sDao; @Autowired @Qualifier("student") private Student s; public Student getS() { return s; } public void setS(Student s) { this.s = s; } public StudentDao getsDao() { return sDao; } public void setsDao(StudentDao sDao) { this.sDao = sDao; } public boolean saveStudent(){ if (sDao.saveStudents(s)){ return true; }else{ return false; } } }
package test.bupt.lesson3; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import bupt.lesson3.StudentService; import com.sun.org.apache.xerces.internal.util.XML11Char; import junit.framework.Assert; import junit.framework.TestCase; public class testStudentService extends TestCase { public void testSaveStudent() { ApplicationContext ctx = new ClassPathXmlApplicationContext("lesson3.xml"); StudentService stService = (StudentService) ctx.getBean("studentService"); Assert.assertEquals(true, stService.saveStudent()); } }