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:
spring.name=spring
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;