[杂谈]杂谈章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;

 

好了,闲聊到这吧。。。

 

posted on 2019-03-06 17:52  深圳私塾  阅读(134)  评论(0)    收藏  举报

导航