Spring之各种类型的自动装配和注解

一、自动装配:

在applicationContext.xml中添加

<!-- 自动装配 -->
	<bean id="course1" class="org.ruangong.entity.Course" autowire="byName">
		<property name="cname" value="java"></property>
		<property name="chour" value="2"></property>
	</bean>

  通过 autowire=“byName”实现自动装配。

也可以在xml文件头部添加default-autowire="byName",设置全部装配。

二、注解:

创建类文件:StudentDaoImpl:

package org.ruangong.dao;

import org.ruangong.entity.Student;
import org.springframework.stereotype.Repository;
@Repository("studentDao")
public class StudentDaoImpl {
	public void addStudent(Student student){
		System.out.println("增加学生...");
	}
}

  @Repository("studentDao")相当于:<bean id="studentDao" class="org.ruangong.dao.StudentDaoImpl"></bean>

用conponent也可以。但是:

dao层注解用:@Repository

service层注解用:@Service

控制器层(servlet)注解用:@Controller

原文地址:https://www.cnblogs.com/jccjcc/p/13977204.html