Spring框架是公司比较常用的框架。
=====================
Spring jar 包下载
=====================
在自己本地运行测试spring的项目,首先要有spring相关jar包的支持,相关的jar可以按着这个流程去下载
======================
@Autowired注解
=====================
通过使用自动注入@Autowired的方法来简化代码,在获取某个属性值时不用再写该属性的get 和set 方法,同时减少代码之间的耦合。运行机制就是
当我们对bean类中的某个属性加上@Autowired后,当Spring容器启动时,就会扫描配置文件中的bean,当发现该bean中有自动注入@Autowired的属性时,就把该属性注入到相应的bean类中去。
但是对于配置文件bean的属性的获取方法要一致,要么通过自动注入获取,要么通过get和set方法获取,不然的话,会造成属性获取的混乱。
这一区别在于配置文件中是否有自动注入的组件bean
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
学习过的网上比较好的Spring自动注入
========================
@Value
=======================
有时候我们也需要在类中使用配置文件properties中的信息,这时候就可以用@Value在配置文件中读取相应的值。
具体要涉及到
想要获取某个属性值的类,并在定义该属性值之前标注@Value xxxxx,
XML配置文件
applicationContext.xml
以及
配置文件properties
(1)想要获取类class的配置
@Value("${qy .localDir}") //
private String qylocalDir;//属性的定义
File f=new File(qylocalDIR+"fileName");
(2)applicationContext.xml
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:qy.localDir</value>
</list>
</property>
</bean>
(3)配置文件properties,用来存放具体属性值的文件
qy.localDir=/home/bbipadm/data/GDHPFSA/GDHPFSA11/recv/
@Value相关的文章,可以借鉴