操作步骤
1、添加jar包
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
2、新建 applicationContext.xml文件
2.1 如果使用扫描+注解的方式,需要在xml文件中配置需要扫描的类
<context:component-scan base-package="com.nut.dao"></context:component-scan>
3、在需要扫描的类上加注解
3.1在类名上加下面四个注解
@Component //搞不清用这个
@Repository //在dao层
@Service //在service层
@Controller //在control层
3.2在需要扫描的变量名上加下面注解
@Resource
4、使用监听器启动sprig的配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applictionContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>