栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

spring-自动加载配置文件使用属性文件注入

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring-自动加载配置文件使用属性文件注入

在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构

src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到WEB-INF下

首先看下pom.xml,需要引入一些依赖项:

 1  3     4.0.0 4     yjmyzz 5     jsf-web 6     1.0 7     war 8  9 10     11         12         13             junit14             junit15             4.716             test17         18 19         20         21             org.jboss.spec.javax.faces22             jboss-jsf-api_2.1_spec23             2.1.19.1.Final-redhat-124             compile25         26 27 28         29         30             org.springframework31             spring-context32             4.0.2.RELEASE33         34 35 36         37             org.springframework38             spring-web39             4.0.2.RELEASE40         41 42 43         44         45             javax.servlet46             servlet-api47             2.548         49 50     51 52     53         54             55                 maven-compiler-plugin56                 3.157                 58                     1.759                     1.760                 61             62             63                 maven-war-plugin64                 2.365                 66                     webapp67                     false68                 69             70         71     72 

pom.xml

 

1. 自动加载配置文件

在web项目中,可以让spring自动加载配置文件(即上图中的src/main/resouces/spring下的xml文件),WEB-INF/web.xml中参考以下设置:

 1  2  3      jsf-web 4       5       6        index.html    7       8          9     10      11        org.springframework.web.context.ContextLoaderListener12       13       14     15     16      contextConfigLocation17       18        classpath*:spring/applicationContext-*.xml19       20         21 22 

web.xml

解释一下: classpath*:spring/applicationContext-*.xml 这里表示将加载classpath路径下 spring目录下的所有以applicationContext-开头的xml文件 , 通常为了保持配置文件的清爽 , 我们会把配置分成多份 : 比如 applicationContext-db.xml 用来配置DataSource , applicationContext-cache.xml用来配置缓存...等等.

 

2.代码中如何取得ApplicationContext实例

 1 package yjmyzz.utils; 2  3 import javax.faces.context.FacesContext; 4 import javax.servlet.ServletContext; 5  6 import org.springframework.context.ApplicationContext; 7 import org.springframework.web.context.support.WebApplicationContextUtils; 8  9 public class ApplicationContextUtils {10 11     public static ApplicationContext getApplicationContext() {12         ServletContext context = (ServletContext) FacesContext13                 .getCurrentInstance().getExternalContext().getContext();14         ApplicationContext appctx = WebApplicationContextUtils15                 .getRequiredWebApplicationContext(context);16 17         return appctx;18     }19 20     public static  T getBean(Class t) {21         return getApplicationContext().getBean(t);22     }23 }

ApplicationContextUtils

 有了这个工具类 , 就可以方便的取得注入的Bean

 

3. 使用properties文件注入

为了演示注入效果,先定义一个基本的Entity类

 1 package yjmyzz.entity; 2  3 import java.io.Serializable; 4  5 public class ProductEntity implements Serializable { 6  7     private static final long serialVersionUID = -2055674628624266800L; 8     11     private String productNo;12 13     16     private String productName;17 18     21     private Long productId;22 23     public String getProductNo() {24         return productNo;25     }26 27     public void setProductNo(String productNo) {28         this.productNo = productNo;29     }30 31     public String getProductName() {32         return productName;33     }34 35     public void setProductName(String productName) {36         this.productName = productName;37     }38 39     public Long getProductId() {40         return productId;41     }42 43     public void setProductId(Long productIdLong) {44         this.productId = productIdLong;45     }46 47     @Override48     public String toString() {49         return productId + "/" + productNo + "/" + productName;50     }51 52 }

ProductEntity

然后在applicationContext-beans.xml中配置以下内容:

 1  2  6  7      9         10             11                 classpath:properties/*.properties12             13         14     15 16     17         18         19         20         26     27 

spring配置文件

注:classpath:properties/*.properties表示运行时 , spring容器会自动加载classpathproperties目录下的所有以.properties后缀结尾的文件 ,  我们在src/main/resources/properties/下放置一个product.properties属性文件 , 内容如下:

1 product.id=32 product.no=n953 product.name=phone

product.properties

该文件被spring自动加载后 , 就可以用里面定义的属性值 , 为Bean做setter属性注入 , 即配置文件中的

 

4.验证注入是否成功

在HomeController里 ,  向Spring容器要一个Bean ,  显示下它的属性:

 1 package yjmyzz.controller; 2  3 import javax.faces.bean.ManagedBean; 4  5 import yjmyzz.entity.ProductEntity; 6 import yjmyzz.utils.ApplicationContextUtils; 7  8 @ManagedBean(name = "Home") 9 public class HomeController {10 11     public String sayHello() {12 13         ProductEntity product = ApplicationContextUtils14                 .getBean(ProductEntity.class);15 16         return product.toString();17     }18 19 }

HomeController

index.xhtml里仍然跟上篇相同:

 1   2   6  7  8     jsf-web 9  10  11     12         #{Home.sayHello()}13         14     15  16 

index.xhtml

 

最后部署到jboss上 , 运行截图如下:

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号