10月9日记录spring属性注入和的用法

来源:互联网 发布:php require__DIR__ 编辑:程序博客网 时间:2024/05/23 00:10

因为之前面试过这方面的问题所以特地找了资料,记录一下,最简单的属性注入

1:xml版本,在xml文件里声明这个bean,注意这样声明需要TEstDemo中的value方法有set方法
       <bean id="test" class="com.zq.test.TestDemo">
       <property name="value" value="nihao"></property>
       </bean>

在测试类中

  ClassPathXmlApplicationContext x=new ClassPathXmlApplicationContext("applicationContext-public.xml");
  TestDemo testDemo=(com.zq.test.TestDemo) x.getBean("test");
  testDemo.say();

输出结果为nihao

2:annotation版本把用@Component("test")把这个类注册为spring的bean,用@value为其注入

@Component("test")
public class TestDemo {
  @Value("1")
  private Integer value;
  public void say(){
   System.out.println(value);
  }
}

测试类输出结果为1

3:使用propertis文件管理松散的属性,数据在propertis文件中以键值对方式进行存储

3.1:使用工具类property-placeholder

<context:property-placeholder location="classpath:conn.properties"/>

之后可以在xml页面中使用el表达式取出值

<bean id="dataSource" class="${dataSource}">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>

在代码中也可以使用@Value("${key}")的方式取出这些值

@Component("test")
public class TestDemo {
  @Value("${driverClass}")
  private String driverClass;
  public void say(){
   System.out.println(driverClass);
  }
}
在测试类
public void test1(){
  ClassPathXmlApplicationContext x=new ClassPathXmlApplicationContext("applicationContext-public.xml");
  TestDemo testDemo=(com.zq.test.TestDemo) x.getBean("test");
  testDemo.say();
}
}
输出结果为oracle.jdbc.driver.OracleDriver

3.2:使用类org.springframework.beans.factory.config.PropertyPlaceholderConfigurer等同于以上的方法,注意他的locations的值是数组形式的

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:conn.properties</value>
</array>
</property>
</bean>

使用方法等同于以上方法

3.3:使用工具类<util:properties id="prop" location="classpath:public.properties"></util:properties>此方法需要引入xsd文件

 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-4.2.xsd

在xml文件中也可以使用@Value("#{configProperties['key']}")方式进行注入

<util:properties id="prop" location="classpath:conn.properties"></util:properties>
<bean id="test" class="com.zq.test.TestDemo">
<property name="driverClass" value="#{prop['driverClass']}"></property>
</bean>

测试类中

public void test1(){
  ClassPathXmlApplicationContext x=new ClassPathXmlApplicationContext("applicationContext-public.xml");
  TestDemo testDemo=(com.zq.test.TestDemo) x.getBean("test");
  testDemo.say();
}
}

输出结果为oracle.jdbc.driver.OracleDriver

在类中使用@Value("#{configProperties['key']}")进行注入

@Component("test")
public class TestDemo {
 @Value("#{prop['driverClass']}")
  private String driverClass;

  public void say(){
   System.out.println(driverClass);
  }
}

测试类输出结果也为oracle.jdbc.driver.OracleDriver

3.4:使用类org.springframework.beans.factory.config.PropertiesFactoryBean等同于以上方法

<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<array>
<value>classpath:public.properties</value>
</array>
</property>
</bean>

使用方法和以上相似。

晕了很久朋友给的一个视频上面说3.1,3.2两个方法只能在xml文件中使用 网上找资料后发现有疑惑,进行测试发现都可以使用只是方式不同而已,或许有错漏之地方,留待以后改正



原创粉丝点击