spring 注入静态属性或注入枚举

1:注入枚举

xml:(com.chinacache.alarm.enmu.AlarmMessage 是枚举类)

<bean id="alarmMessage"
		class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
		<property name="staticField" value="com.chinacache.alarm.enmu.AlarmMessage.EMAIL" />
	</bean>

目标类使用@Autowired注入就可以了

 

2:注入静态属性(@PostConstruct初始化时调用)

@Resource
private HibernateTemplate hibernateTemplate;

private static TalentsDaoImplTest td;

@PostConstruct
public void init() {
  td = this;
  td.hibernateTemplate = this.hibernateTemplate;
}

@Test
public void testStatic() {
  Talents talents = td.hibernateTemplate.get(Talents.class, 1L);
  System.out.println(talents.getAge());
}

 转自:http://irfen.iteye.com/blog/1335016

 

 

 

你可能感兴趣的:(spring)