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为其注入
public class TestDemo {
@Value("1")
private Integer value;
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}")的方式取出这些值
public class TestDemo {
@Value("${driverClass}")
private String driverClass;
System.out.println(driverClass);
}
}
ClassPathXmlApplicationContext x=new ClassPathXmlApplicationContext("applicationContext-public.xml");
TestDemo testDemo=(com.zq.test.TestDemo) x.getBean("test");
testDemo.say();
}
}
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文件中使用 网上找资料后发现有疑惑,进行测试发现都可以使用只是方式不同而已,或许有错漏之地方,留待以后改正
- 10月9日记录spring属性注入和的用法
- spring的属性注入和构造器注入
- Spring---->属性的注入
- Spring、基本类型属性和集合类型属性的注入
- bean标签的属性和spring属性注入
- 8月13日记录
- Spring 属性注入和构造器注入
- Spring笔记之一:依赖注入的方式属性注入和构造函数注入
- Spring注入值的2种方式:属性注入和构造注入
- 关于spring的属性注入
- Spring属性注入的方式
- spring属性的注入实例
- Spring的属性注入(SPEL)
- spring 静态属性的注入
- spring中属性文件和配置文件的注入方式
- Spring属性注入和构造器注入、工厂方法注入
- Spring简单属性注入和集合属性注入
- Spring属性注入Bean的List属性
- mysql 时间间隔
- Android AOP面向切面编程详解
- java获取当年第一天
- HTTP与HTTPS的区别
- PPTP server
- 10月9日记录spring属性注入和的用法
- <黑格尔《精神现象学》句读> -- 绪论
- Json解析之jackson的使用
- 启动myeclipse一直是update indexs解决方法
- 51nod 1490
- hdu 6129 (杨三角技巧)
- 韩国短道雷人一幕 害人终害己 日本教练表情亮了
- 个人理解c#的委托和事件,附带例子
- linux 串口操作详解