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;

好了,闲聊到这吧。。。

[杂谈]杂谈章3 JAVA中如何用自动注入的更多相关文章

  1. JAVA中如何用接口实现多继承和多态 (非常好)

    ---------------------------------------------------------------多态1.JAVA里没有多继承,一个类之能有一个父类.而继承的表现就是多态. ...

  2. 《Java入门第三季》第二章 认识 Java 中的字符串

    什么是 Java 中的字符串.1.在Java的世界里,字符串被作为String类型的对象处理. 2.通用的初始化的方式:String s = new String("i love you & ...

  3. 在java中如何用键盘输入一个数,字符,字符串

    一,利用 Scanner 实现从键盘读入integer或float 型数据 import java.util.*; public class test { public static void mai ...

  4. 第1章 Java中常用字符串方法总结

    1.1 charAt方法——提取指定字符 1.2 codePointAt方法——提取索引字符代码点 1.3 codePointBefore方法——获取索引前一个字符的代码点 1.4 codePoint ...

  5. 在Java中如何用String类中的indexof方法得到一个词的出现频率

    public class Test{ public static void main(String[] args) { String s="hello jack hello look me ...

  6. Java中关于类型自动提升的两个注意点。

    问题一:在进行赋值运算时,进行类型提升: 例如:short s1 = 1;s1 = s1 + 1; (错的编译通不过) short s2 = 1;s2 += 1;(正确,编译和运行都能通过) 为什么呢 ...

  7. Java中的容器和注入分析

    为什么会出现容器的注入? 容器:顾名思义,装东西的器物. 至于spring中bean,aop,ioc等一些都只是实现的方式:具体容器哪些值得我们借鉴,我个人觉得是封装的思想.将你一个独立的系统功能放到 ...

  8. Java中复合赋值运算符自动进行强制类型转换

    public class Operation { public static void main(String[] args) { int num1 = 10; num1 = num1 / 2.2; ...

  9. Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案

    点关注,不迷路:持续更新Java架构相关技术及资讯热文!!! 相信使用Mybaits的小伙伴们一定会经常编写类似如下的代码: 可以看到 userMapper 下有个红色警告.虽然代码本身并没有问题,能 ...

随机推荐

  1. CRM 2016 及 CRM 365 更新地址

    CRM2016安装程序下载地址: https://www.microsoft.com/zh-cn/download/details.aspx?id=50372 CRM 365 更新地址: https: ...

  2. 查看Linux内置命令和外部命令

    1. [hl@localhost ~]$ which cd /bin/cd [hl@localhost ~]$ type cd cd is a shell builtin

  3. Zookeeper的一致性协议:Zab

        Zookeeper使用了一种称为Zab(Zookeeper Atomic Broadcast)的协议作为其一致性复制的核心,据其作者说这是一种新发算法,其特点是充分考虑了Yahoo的具体情况: ...

  4. [UnityShader基础]05.模板测试

    参考链接: https://blog.csdn.net/u011047171/article/details/46928463 https://blog.csdn.net/JohnBlu/articl ...

  5. VS2015密匙--VS2015打开丢失msvcp140.dll--cannot find one or more components ,please reinstall the application

    win7旗舰版 64位 + vs2015 专业版 1.安装VS2015过程中可能需要用到的VS2015专业版钥匙:(测试,可用) HMGNV-WCYXV-X7G9W-YCX63-B98R2 2.VS2 ...

  6. openStack 重新resize时会进行重新调度,可能在本机Resize 扩展资源,也可能存在的情况时 ,新扩展的资源在当前节点不足分配,整个虚拟机将进行迁移调度,进行异机迁移时需要迁移 的两台主机间能使用nova系统用户经passless登录

    openStack 重新resize时会进行重新调度,可能在本机Resize 扩展资源,也可能存在的情况时 ,新扩展的资源在当前节点不足分配,整个虚拟机将进行迁移调度,进行异机迁移时需要迁移 的两台主 ...

  7. Xeon Phi 《协处理器高性能编程指南》随书代码整理 part 1

    ▶ 第三章,逐步优化了一个二维卷积计算的过程 ● 基准代码 #include <stdio.h> #include <stdlib.h> #include <string ...

  8. Linux 开机、重启和用户登录注销、用户管理

    关机&重启命令 shutdown shutdown -h now:表示立即关机 shutdown -h 1:表示1分钟后关机 shutdown -r now:立即重启 halt 就是直接使用, ...

  9. leetcode32

    class Solution { public: int longestValidParentheses(string s) { ; stack<int> st; ; i < n; ...

  10. Java中的反射总结

    反射是获取运行时类信息,即常量区中的Class信息. 要获取类信息,必然需要依据,不然系统怎么指定你要获取那个类信息, 类信息在java中就是Class类的一个对象,它是一个java类抽象,换句话说它 ...