[杂谈]杂谈章3 JAVA中如何用自动注入
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中如何用自动注入的更多相关文章
- JAVA中如何用接口实现多继承和多态 (非常好)
---------------------------------------------------------------多态1.JAVA里没有多继承,一个类之能有一个父类.而继承的表现就是多态. ...
- 《Java入门第三季》第二章 认识 Java 中的字符串
什么是 Java 中的字符串.1.在Java的世界里,字符串被作为String类型的对象处理. 2.通用的初始化的方式:String s = new String("i love you & ...
- 在java中如何用键盘输入一个数,字符,字符串
一,利用 Scanner 实现从键盘读入integer或float 型数据 import java.util.*; public class test { public static void mai ...
- 第1章 Java中常用字符串方法总结
1.1 charAt方法——提取指定字符 1.2 codePointAt方法——提取索引字符代码点 1.3 codePointBefore方法——获取索引前一个字符的代码点 1.4 codePoint ...
- 在Java中如何用String类中的indexof方法得到一个词的出现频率
public class Test{ public static void main(String[] args) { String s="hello jack hello look me ...
- Java中关于类型自动提升的两个注意点。
问题一:在进行赋值运算时,进行类型提升: 例如:short s1 = 1;s1 = s1 + 1; (错的编译通不过) short s2 = 1;s2 += 1;(正确,编译和运行都能通过) 为什么呢 ...
- Java中的容器和注入分析
为什么会出现容器的注入? 容器:顾名思义,装东西的器物. 至于spring中bean,aop,ioc等一些都只是实现的方式:具体容器哪些值得我们借鉴,我个人觉得是封装的思想.将你一个独立的系统功能放到 ...
- Java中复合赋值运算符自动进行强制类型转换
public class Operation { public static void main(String[] args) { int num1 = 10; num1 = num1 / 2.2; ...
- Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案
点关注,不迷路:持续更新Java架构相关技术及资讯热文!!! 相信使用Mybaits的小伙伴们一定会经常编写类似如下的代码: 可以看到 userMapper 下有个红色警告.虽然代码本身并没有问题,能 ...
随机推荐
- 浏览器调试动态js脚本
前两天拉取公司前端代码修改,发现在开发者工具的sources选项里边,居然没有列出来我要调试的js脚本,后来观察了一下,脚本是动态在页面里引入的,可能是因为这样所以不显示出来,但是如果不能断点调试,只 ...
- python 使用ElementTree解析xml
以country.xml为例,内容如下: <?xml version="1.0"?> <data> <country name="Liech ...
- [UE4]添加手柄
一.在上一节的VRPawnBase中,再添加2个Motion Controller,分别命名为:LeftMotionController.RightMotionController,分别代表左右手柄. ...
- 3-hive、sqoop
1.HIVE 1.交互命令 use db_name; create database db_name //创建数据库 create database if not exists db_name //创 ...
- 9-安装redis
1.在linux上安装C语言环境 yum install gcc-c++ 2.解压源码包 tar -xvf /opt/soft/redis-3.0.0.tar -C /opt/app/ 3.编译源码( ...
- 【Linux】【Chrome】安装Chrome浏览器的攻略
https://blog.csdn.net/chenlix/article/details/72526205 1.切换到root: su - 或者 sudo -i 2.下载新的软件源定义: cd /e ...
- 深度学习VS机器学习——到底什么区别
转自:https://baijiahao.baidu.com/s?id=1595509949786067084&wfr=spider&for=pc 最近在听深度学习的课,老师提了一个基 ...
- html阿里云网页练习实现代码
html <body> <!-- 固定浮动栏 --> <div class="guding"> <p> ...
- Echarts 饼状图自定义颜色
今天给饼状图着色问题,找了好久 终于找到了 话不多说直接上代码 $.ajax({ url: "/HuanBaoYunTai/ajax/HuanBaoYunTaiService.ashx&qu ...
- 多层josn数据 修改
var aa = { a: 1, b: { c: 1, d: 1 }, e: [{ f: 1, g: 2 }, { h: 1, i: { j: 3, k: [{ l: 55, m: [1, 2, 3, ...