PART1 加配置文件

创建自动加载bean的配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

<context:annotation-config />
<context:component-scan base-package="org.shine.javaproject.*" />
<aop:aspectj-autoproxy />
</beans>

修改web.xml配置文件添加如下内容

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:javaproject.xml</param-value>
</context-param>

PART2 如何找配置文件

配置文件中的classpath和classpath*区别:

classpath:只会到你的class路径中查找找文件。

classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。

注意: 用classpath*:需要遍历所有的classpath,所以加载速度是很慢的;因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*。

classpath*的使用:

当项目中有多个classpath路径,并同时加载多个classpath路径下(此种情况多数不会遇到)的文件,*就发挥了作用,如果不加*,则表示仅仅加载第一个classpath路径。

一些使用技巧:

1、从上面使用的场景看,可以在路径上使用通配符*进行模糊查找。比如:

<param-value>classpath:applicationContext-*.xml</param-value>  

2、"**/"表示的是任意目录;"**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。

3、程序部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的WEB-INF/classes目录下;classpath:与classpath*:的区别在于,前者只会从第一个classpath中加载,而 后者会从所有的classpath中加载。

4、如果要加载的资源,不在当前ClassLoader的路径里,那么用classpath:前缀是找不到的,这种情况下就需要使用classpath*:前缀。

5、在多个classpath中存在同名资源,都需要加载时,那么用classpath:只会加载第一个,这种情况下也需要用classpath*:前缀。

PART3 @Service @Autowired @Controller咋用

直接在POM.XML中引入吧

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>

@Service @Autowired @Controller

POM中引入了,还是报错呢;好吧,Class直接Import来吧

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.stereotype.Service;

好了,闲聊到这吧。。。

最新文章

  1. 设计模式01观察者模式(java)
  2. 准备熟悉Kaggle -菜鸟进阶
  3. 打开都是“Smart Adobe CC Blocker v1.0”已损坏,打不开。 您应该将它移到废纸篓。
  4. awesome-scala
  5. IntelliJ13+tomcat+jrebel实现热部署(亲测可用)
  6. vmware装redhat该光盘无法被挂载
  7. Obout - ASP.NET HTML Editor
  8. centos 安装nginx
  9. 6.PHP 教程_PHP数据类型
  10. JSON.stringify 方法
  11. JavaScript 运动框架 Step by step(转)
  12. photoshop 魔术橡皮擦
  13. 微软2017MVP大礼包拆箱攻略
  14. Python面试题之copy/deepcopy详解
  15. (一)DOM 常用操作 —— “查找”节点
  16. hw3
  17. [转]分别使用Node.js Express 和 Koa 做简单的登录页
  18. python3获取当前目录和上级目录
  19. KbmMW 服务器架构简介
  20. SSL虚拟主机安全方案

热门文章

  1. mongo数据的导入(mongoimport)和导出(mongoexport)
  2. 学Python的原因
  3. java中int算法的有趣现象
  4. Java的Finalizer引发的内存溢出
  5. JavaScript基础应用
  6. 7、Curator的常规操作
  7. GraphicsMagick命令
  8. IDEA查看类继承关系及生成类关系图
  9. Java选择排序,插入排序,快速排序
  10. github 添加wiki