最近做的项目需要获取配置文件中的bean属性操作
ApplicationContext cnt= new ClassPathXmlApplicationContext("classpath*:spring/spring-test.xml");
User wp = (User) applicationContext.getBean("UserService");
wp.sendMessge(a, a.getChatId());
主要是加载到IOC容器中才能使用
spring-test.xml如下:
<bean id="userService" class="com.userService">
<property name="a" value="${producer}"></property>
<property name="b" ref="b"></property>
</bean>
</beans>
userService代码如下:
private String a;
private B b;
public void seta(String a) {
this.a= a;
}
public void setB(B b) {
this.b= b;
}
public void sendMessge(Object obj, String bizId) {
Message message = new Message(a, JSONObject.toJSONString(obj), bizId);
b.send(message);
}