【配置文件节点】java世界配置文件节点

Spring <context:property-placeholder/>

期望:能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息 解决:spring3中提供了一种简便的方式就是context:property-placeholder/元素 只需要在spring的配置文件里添加一句:
<context:property-placeholder location="classpath:jdbc.properties"/> 即可,这里location值为参数配置文件的位置,参数配置文件通常放在src目录下,而参数配置文件的格式跟java通用的参数配置文件相同,即键值对的形式,例如: #jdbc配置 test.jdbc.driverClassName=com.mysql.jdbc.Driver test.jdbc.url=jdbc:mysql://localhost:3306/test test.jdbc.username=root test.jdbc.password=root 行内#号后面部分为注释 应用: 1.这样一来就可以为spring配置的bean的属性设置值了,比如spring有一个jdbc数据源的类DriverManagerDataSource 在配置文件里这么定义bean: <bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${test.jdbc.driverClassName}"/> <property name="url" value="${test.jdbc.url}"/> <property name="username" value="${test.jdbc.username}"/> <property name="password" value="${test.jdbc.password}"/> </bean> http://stamen.iteye.com/blog/1926166

Spring <context:annotation-config/>

在基于主机方式配置Spring的配置文件中,你可能会见到
<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。 注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。 例如: 如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下 <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/> 如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor 如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。 如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下: <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。 不过,呵呵,我们使用注解一般都会配置扫描包路径选项 <context:component-scan base-package=”XX.XX”/> 该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

<context:component-scan base-package="com.javaniu" />

关于spring自动检测组件的使用方式网上太多了,而且也不是我记录的重点,我想说下一点可能你还不知道的经验
我们知道如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入。
 
先写一个小例子,剩下的在下面解释
<!-- 定义扫描根路径为leot.test,不使用默认的扫描方式 -->
<context:component-scan base-package="leot.test" use-default-filters="false">
  <!-- 扫描符合@Service @Repository的类 -->
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
 
下面是引用spring framework开发手册中的一段话“
Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。@Component是所有受Spring管理组件的通用形式;而@Repository、@Service和 @Controller则是@Component的细化,用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。也就是说,你能用@Component来注解你的组件类,但如果用@Repository、@Service 或@Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联。例如,这些典型化注解可以成为理想的切入点目标。当然,在Spring Framework以后的版本中, @Repository、@Service和 @Controller也许还能携带更多语义。如此一来,如果你正在考虑服务层中是该用@Component还是@Service,那@Service显然是更好的选择。同样的,就像前面说的那样, @Repository已经能在持久化层中进行异常转换时被作为标记使用了。”
 
下面是网上目前关于组件扫描最详细的介绍
Spring applicationContext.xml的<context:component-scan>標籤用途比我想像的還要實用。而且後來才知道,有了<context:component-scan>,另一個<context:annotation-config/>標籤根本可以移除掉,因為被包含進去了。原本我survery Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下尋找有@Component和@Configuration的target Class。而現在如下的飯粒:
<context:component-scan base-package="com.foo" use-default-filters="false">
<context:include-filter type="regex" expression="com.foo.bar.*Config"/>
<context:include-filter type="regex" expression="com.foo.config.*"/>
</context:component-scan>
  <context:component-scan>提供兩個子標籤:<context:include-filter><context:exclude-filter>各代表引入和排除的過濾。而上例把use-default-filters屬性設為false,意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean,由filter子標籤代勞。
  filter標籤在Spring3有五個type,如下:
Filter Type    Examples Expression    Description
annotation    org.example.SomeAnnotation    符合SomeAnnoation的target class
assignable    org.example.SomeClass    指定class或interface的全名
aspectj    org.example..*Service+    AspectJ語法
regex    org.example.Default.*    Regelar Expression
custom    org.example.MyTypeFilter    Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter
  所以上例用的regex就有個語病,com.foo.config.* 可以找到com.foo.config.WebLogger,但也可以找到com1fool2config3abcde,因為小數點在Regex是任意字元,是故要用.把小數點跳脫為佳。(2010/3/15補充:但要使用.方式,其use-default-filters不能為false,否則抓不到,感覺是Bug)
  Spring3提供豐富的Filter支援,有益配置策略,不需面臨Configuration Hell,比如Regex的com.foo.*.action.*Config,這樣就可以找到com.foo package下所有action子package的*Config的target class。
 
我按他的例子,配置了我自己的如下:
<context:component-scan base-package="com.xhlx.finance.budget"  >
  <context:include-filter type="regex" expression="com.lee.finance.budget.service.*"/>
  <context:include-filter type="regex" expression="com.lee.finance.budget.security.*"/> 
</context:component-scan>
但是死活扫描不到,网上又没有更好的讲解,没办法只好自己试,改成
<context:component-scan base-package="com.xhlx.finance.budget" >
  <context:include-filter type="regex" expression="com.lee.finance.budget.*"/>
</context:component-scan>
这样连没有加注解的类也扫描并实例化,结果报错,因为有的类根本就没有默认的构造函数不能实例化,能不报错吗
改成
<context:component-scan base-package="com.xhlx.finance.budget"  >
  <context:include-filter type="regex" expression="com.lee.finance.budget.service.*"/>  
</context:component-scan>问题依旧
<context:component-scan base-package="com.xhlx.finance.budget" >
  <context:include-filter type="regex" expression="com.lee.finance.budget.service.TestService"/>
</context:component-scan>
嘿,这次可以了,写全限定名就可以,表达式却不行,但是如果我有成千上百个还得一个一个这样配置啊?不行,继续研究,我想有个base-package的配置,从表面意思来看这是个“基本包”,是否表示下面的过滤路径是基于这个包的呢?于是试着改成
<context:component-scan base-package="com.xhlx.finance.budget" >
  <context:include-filter type="regex" expression=".service.*"/>
</context:component-scan>
嘿,行了。 

<mvc:annotation-driven />

<mvc:annotation-driven />说明 
据spring api 文档的解释<mvc:annotation-driven /> 主要是解析@Controller 
@RequestMapping 等注解的。  
官方说明:  
<mvc:annotation-driven 
/> 
declares 
explicit 
support 
for 
annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, although support for those is the default behaviour), as well as adding support for declrative validation via @Valid and message body marshalling with @RequestBody/ResponseBody.  翻译: 
<mvc:annotation-driven/>声明明确支持注解驱动的MVC控制器(即@RequestMapping, @Controller,支持那些虽然是默认行为),以及增加支持declrative验证通过@有效和邮件正文编组@ RequestBody/ ResponseBody。  
下面是我个人认为:文档中提及主要是申明注解,@Controller,@RequestMapping但是后面加了一句默认行为,个人认为一般情况下 申明此类注解是默认的,但是在一些特殊的情况下是需要手动配置申明的。为了求真以上结果是否真确,我做了以下3个测试。  
1一般.默认情况 无加载静态资源 无配置<mvc:annotation-driven /> 
一般情况下 当使用sprin MVC 时候若没有加载静态资源 比如 (<mvc:resources mapping="/css/**" location="/css/" />) 的时候系统默认会帮我们绑定路劲
原文地址:https://www.cnblogs.com/viewcozy/p/4705451.html