在Spring的IOC和DI组合使用对mvc模式进行使用,这里最经典的模式是面向接口编程,实现接口的那个对象不需要在类中定义,在Spring的配置文件中配置一下即可: Spring容器会自动的为你创建对象。
顺便补充一句哈: 在bean中对属性设置值的话,是根据set方法来的,源代码中就是这样子定义的
第一种方式: 使用在Spring容器中配置Bean ,具体的代码:
Spring容器的配置文件:
<bean id="studentAction" class="cn.itcast.spring.mvc.bean.StudentAction">
<property name="studentService">
<ref bean="studentService"/>
property>
bean>
<bean id="studentService" class="cn.itcast.spring.mvc.bean.StudentServiceImpl">
<property name="studentDao">
<ref bean="studentDao"/>
property>
bean>
<bean id="studentDao" class="cn.itcast.spring.mvc.bean.StudentDaoImpl">bean>
所有的测试代码,相信知道mvc模式的哥们都知道这种写法哈:
控制层:
public class StudentAction {
//这个是service接口,实现这个接口的子类在spring容器中进行生成
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void saveStudent(){
studentService.saveStudent();
}
public void updateStudent(){
studentService.updateStudent();
}
}
业务逻辑层:
public class StudentServiceImpl implements StudentService {
//这个是dao接口,实现子类在spring容器中创建,那么这个是连接dao的
private StudentDao studentDao;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public void saveStudent() {
studentDao.saveStudent();
}
@Override
public void updateStudent() {
studentDao.updateStudent();
}
}
dao层(数据访问层):
public class StudentDaoImpl implements StudentDao {
@Override
public void saveStudent() {
System.out.println("saveStudent....");
}
@Override
public void updateStudent() {
System.out.println("updateStudent.....");
}
测试:
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("cn/itcast/spring/mvc/bean/applicationContext.xml");
StudentAction studentAction =(StudentAction)context.getBean("studentAction");
studentAction.saveStudent();
studentAction.updateStudent();
}
第二种方式: 用注解形式对mvc进行修改,这就标志着在spring配置文件中少些xml配置,虽然效率有点低,但是在项目中是无关紧要的,
使用
全局搜索注解形式l: 意思就是说在包下会自动寻找所有的类:
Spring:配置文件中的配置:
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="cn.itcast.mvc.spring.action.nobean">context:component-scan>
beans>
在控制层中的代码:
@Controller("studentAction")//如果在这里写上值的话,那么就是相当于是
//如果不写值的话,那么就会默认的根据类的名字取名字 且把第一个字母换成小写,后面的不变
public class StudentAction {
//这个是service接口,实现这个接口的子类在spring容器中进行生成引用对象,
@Resource(name="studentService")//如果写上的话就是引用对象,根据引用的对象的id识别,不写的话就根据类型搜索
private StudentService studentService;
public void saveStudent(){
studentService.saveStudent();
}
public void updateStudent(){
studentService.updateStudent();
}
在业务逻辑层中的代码:
@Controller("studentService")
public class StudentServiceImpl implements StudentService {
//这个是dao接口,实现子类在spring容器中创建,那么这个是连接dao的
@Resource(name="studentDao")
private StudentDao studentDao;
@Override
public void saveStudent() {
studentDao.saveStudent();
}
@Override
public void updateStudent() {
studentDao.updateStudent();
}
在dao层的代码:
@Controller("studentDao")// 写这个的意思还有就是创建这个类的对象,只能作用在类上
public class StudentDaoImpl implements StudentDao {
@Override
public void saveStudent() {
System.out.println("saveStudent....");
}
@Override
public void updateStudent() {
System.out.println("updateStudent.....");
}
测试:
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("cn/itcast/mvc/spring/action/nobean/applicationContext.xml");
StudentAction studentAction =(StudentAction)context.getBean("studentAction");
studentAction.saveStudent();
studentAction.updateStudent();
}
其实第二种的注解方式是在Spring 配置文件的 标签基础上搞得, 是寻找bean标签的id属性的值或者是根据类型匹配。
在spring配置文件中的配置:
在Action中的代码:
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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:annotation-config>context:annotation-config>
<bean id="studentAction" class="cn.itcast.spring.test.StudentAction">bean>
<bean id="studentService" class="cn.itcast.spring.test.StudentServiceImpl">bean>
<bean id="studentDao" class="cn.itcast.spring.test.StudentDaoImpl">bean>
beans>
控制层的代码:
//这个是service接口,实现这个接口的子类在spring容器中进行生成引用对象,
//如果写上的话就会到spring的配置文件中和 的值去找,resource中不写值的话那么根据类型去匹配
@Resource(name="studentService")
private StudentService studentService;
public void saveStudent(){
studentService.saveStudent();
}
public void updateStudent(){
studentService.updateStudent();
}
业务逻辑层的代码:
public class StudentServiceImpl implements StudentService {
//这个是dao接口,实现子类在spring容器中创建,那么这个是连接dao的
@Resource(name="studentDao")
private StudentDao studentDao;
@Override
public void saveStudent() {
studentDao.saveStudent();
}
@Override
public void updateStudent() {
studentDao.updateStudent();
}
dao层的代码:
@Override
public void saveStudent() {
System.out.println("saveStudent....");
}
@Override
public void updateStudent() {
System.out.println("updateStudent.....");
}
测试:
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("cn/itcast/spring/test/applicationContext.xml");
StudentAction studentAction =(StudentAction)context.getBean("studentAction");
studentAction.saveStudent();
studentAction.updateStudent();
}
其实个人感觉: 是注解的方式的话: 只要是声明对应的注解,不i写值也是可以的: Sprng容器会自定的配合搜索