Spring自動注入:
<!-- Spring自動注入 autowrite屬性
有五個值 - default:默認值,如果beans里有配置了全司autowire屬性,就使用全局配置的值,否則為no
- no : 不自動注入
- byName :根據id名和ref="id"自動注入
- byType :根據bean的類型和ref=""的類型自動注入,如果有兩個符合類型的bean,則報錯
- constructor :根據構造方法的參數名自動注入
-->
<bean id="teacher" class="com.spring.pojo.Teacher"></bean>
<bean id="people" class="com.spring.pojo.People" autowire="byName"></bean>
Spring讀取Properties文件:
db.properties:
applicationContext.xml:
<!-- Spring 讀取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 掃描注解 -->
<context:component-scan base-package="com.spring.test,com.spring.pojo"></context:component-scan>
<!-- 可以用${keyName} 取出properties文件的值 -->
<bean id="teacher" class="com.spring.pojo.Teacher">
<!-- <property name="name" value="${spring.name}"></property> -->
</bean>
使用注解的方式:
//使用注解的方式為全局變量賦值,該類必需由Spring管理
@Value("${spring.name}")
public String name;