- 1.在spring框架中,使用properties文件读取里面的数据库配置信息
- <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:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <context:property-placeholder location="classpath:db.properties" />
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="user" value="${jdbc.user}"></property>
- <property name="password" value="${jdbc.password}"></property>
- <property name="driverClass" value="${jdbc.driverClass}"></property>
- <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
- <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
- <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
- </bean>
- </beans>
- db.properties文件信息
- jdbc.user=root
- jdbc.password=root
- jdbc.driverClass=com.mysql.jdbc.Driver
- jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring
- jdbc.initPoolSize=5
- jdbc.maxPoolSize=10
2.将其在springmvc框架中使用value注入到controller里面
我的项目目录结构
test
----- java
--------- com.test
---------------Proporties.java
---------------Test.java
-----resource
--------- applicationContext.xml
--------- application.properties
-------------------------------------------------------------------
application.properties内容
- jdbc.code = "this is a code"
-------------------------------------------------------------------
Proporties.java 源码
- package com.test;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- @Component
- public class Proporties {
- @Value("#{configProperties['jdbc.code']}")
- private String code;
- public String getCode() {
- return code;
- }
- public void setCode(String code) {
- this.code = code;
- }
- }
-------------------------------------------------------------------
applicationContext.xml 配置
- <!-- 获取properties中的值 -->
- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath:application.properties</value>
- </list>
- </property>
- </bean>
- <!-- Spring的动态变量,能在bean中直接调用 -->
- <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="properties" ref="configProperties" />
- </bean>
- <!-- 使用Annotation自动注册Bean ,扫描 Component-->
- <context:component-scan base-package="com.test" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
- </context:component-scan>
Test.java 源码
- @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
- @RunWith(SpringJUnit4ClassRunner.class
- public class Test{
- @Antowird
- private Proporties proporties;
- @Test
- public void testProperties(){
- System.out.println("获取的值为:" + proporties.getCode());
- }
- }
applicationcontext.xml 中的:
我的项目目录结构
test
----- java
--------- com.test
---------------Proporties.java
---------------Test.java
-----resource
--------- applicationContext.xml
--------- application.properties
-------------------------------------------------------------------
application.properties内容
- jdbc.code = "this is a code"
-------------------------------------------------------------------
Proporties.java 源码
- package com.test;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- @Component
- public class Proporties {
- @Value("#{configProperties['jdbc.code']}")
- private String code;
- public String getCode() {
- return code;
- }
- public void setCode(String code) {
- this.code = code;
- }
- }
-------------------------------------------------------------------
applicationContext.xml 配置
- <!-- 获取properties中的值 -->
- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath:application.properties</value>
- </list>
- </property>
- </bean>
- <!-- Spring的动态变量,能在bean中直接调用 -->
- <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="properties" ref="configProperties" />
- </bean>
- <!-- 使用Annotation自动注册Bean ,扫描 Component-->
- <context:component-scan base-package="com.test" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
- </context:component-scan>
Test.java 源码
- @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
- @RunWith(SpringJUnit4ClassRunner.class
- public class Test{
- @Antowird
- private Proporties proporties;
- @Test
- public void testProperties(){
- System.out.println("获取的值为:" + proporties.getCode());
- }
- }
1)在Spring-MVC.xml中有以下配置:
<!-- 扫描@Controller注解 --> <context:component-scan base-package="com.fq.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
可以看出要把最终的包写上,而不能这样写base-package=”com.fq”。这种写法对于include-filter来讲它都会扫描,而不是仅仅扫描@Controller。哈哈哈,这点需要注意。他一般会导致一个常见的错误,那就是事务不起作用,补救的方法是添加use-default-filters=”false”。
(2)在Spring-common.xml中有如下配置:
<!-- 配置扫描注解,不扫描@Controller注解 --> <context:component-scan base-package="com.fq"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
可以看到,他是要扫描com.fq包下的所有子类,不包含@Controller。对于exculude-filter不存在包不精确后都进行扫描的问题。
我的项目目录结构
test
----- java
--------- com.test
---------------Proporties.java
---------------Test.java
-----resource
--------- applicationContext.xml
--------- application.properties
-------------------------------------------------------------------
application.properties内容
- jdbc.code = "this is a code"
-------------------------------------------------------------------
Proporties.java 源码
- package com.test;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- @Component
- public class Proporties {
- @Value("#{configProperties['jdbc.code']}")
- private String code;
- public String getCode() {
- return code;
- }
- public void setCode(String code) {
- this.code = code;
- }
- }
-------------------------------------------------------------------
applicationContext.xml 配置
- <!-- 获取properties中的值 -->
- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath:application.properties</value>
- </list>
- </property>
- </bean>
- <!-- Spring的动态变量,能在bean中直接调用 -->
- <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="properties" ref="configProperties" />
- </bean>
- <!-- 使用Annotation自动注册Bean ,扫描 Component-->
- <context:component-scan base-package="com.test" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
- </context:component-scan>
Test.java 源码
- @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
- @RunWith(SpringJUnit4ClassRunner.class
- public class Test{
- @Antowird
- private Proporties proporties;
- @Test
- public void testProperties(){
- System.out.println("获取的值为:" + proporties.getCode());
- }
- }