[杂谈]杂谈章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 下有个红色警告.虽然代码本身并没有问题,能 ...
随机推荐
- googletest--Death Test和Exception Test
Death Test验证某个状态会使进程以某个错误码和错误消息离开 #include <gtest\gtest.h> #include "MyStack.h" // D ...
- Kubernetes Service Account如何生成Token
Service Account是运行pods用到的帐号,默认是default.如果apiserver启动配置--admission-control=ServiceAccount,Service Acc ...
- Java笔试面试题整理第七波
转载至:http://blog.csdn.net/shakespeare001/article/details/51388516 作者:山代王(开心阳) 1.super的作用 在Java中su ...
- windows下安装Kettle
先下载kettle稳定版本https://community.hitachivantara.com/docs/DOC-1009855 下载后并且解压 安装kettle必须安装jdk在你的电脑上,ket ...
- 论文 | YOLO(You Only Look Once)目标检测
论文:You Only Look Once: Unified, Real-Time Object Detection 原文链接:https://arxiv.org/abs/1506.02640 背景介 ...
- 【Linux命令】Linux下的tar压缩解压缩命令详解(转)
tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...
- 一针见血tomcat
一针见血tomcat 一.Tomcat各组件认知 1 Tomcat架构说明 Tomcat是一个基于JAVA的WEB容器,其实现了JAVA EE中的 Servlet 与 jsp 规范,与Nginx ap ...
- rpm梳理
- Maven CXF wsdl2Java 给指定名空间设置包名
<plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin< ...
- spring boot报Unsupported Media Type Content type '*/*;charset=UTF-8' not supported
1.请求设置Content-Type:application/json即可 ajax一般默认:Content-Type: application/x-www-form-urlencoded;chars ...