Spring Framework_学习记录_15
作者:互联网
1.Using MessageSource To Get Text From Property Files
- DrawingApp.java
package org.zcs.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
//BeanFactory factory = new FileSystemXmlApplicationContext("src/spring.xml");
//Triangle triangle = new Triangle();
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Shape circle = (Shape)context.getBean("circle");
circle.draw();
System.out.println(context.getMessage("greeting", null, "Default Greeting", null));
}
}
- Circle.java
package org.zcs.spring;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
@Component
public class Circle implements Shape {
@Autowired
private MessageSource messageSource;
public MessageSource getMessageSource() {
return messageSource;
}
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
private Point center;
public Point getCenter() {
return center;
}
@Resource
public void setCenter(Point center) {
this.center = center;
}
@PostConstruct
public void initlize() {
System.out.println("initlizing a circle ");
}
@PreDestroy
public void destroy() {
System.out.println("destroy a circle");
}
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("Drawing Circle:");
System.out.println(messageSource.getMessage("drawing.circle", null, "Defalut Drawing circle", null));
System.out.println(messageSource.getMessage("drawing.point", new Object[] {center.getX(), center.getY()}, "Defalut Drawing point", null));
System.out.println(messageSource.getMessage("greeting", null, "Defalut Greeting", null));
}
}
- message.properties
greeting=Hello!
drawing.circle = Drawing a Circle
drawing.point = Circle : point is :( {0} ,{1})
- spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<!--
<bean id = "triangle" class= "org.zcs.spring.Triangle" autowire= "byName" >
<property name="pointA" ref = "pointA"></property>
<property name="pointB" ref = "pointB"></property>
<property name="pointC" ref = "pointC"></property>
</bean> -->
<bean id= "pointA" class = "org.zcs.spring.Point">
<qualifier value = "circleRelated"></qualifier>
<property name="x" value = "${pointA.pointX}"></property>
<property name="y" value = "${pointA.pointY}"></property>
</bean>
<bean id= "pointB" class = "org.zcs.spring.Point">
<property name="x" value = "20"></property>
<property name="y" value = "0"></property>
</bean>
<bean id= "center" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "20"></property>
</bean>
<!-- <bean id = "circle" class= "org.zcs.spring.Circle" >
</bean> -->
<context:component-scan base-package="org.zcs.spring"></context:component-scan>
<bean class = "org.zcs.spring.MyBeanFactory" ></bean>
<bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value = "pointsconfig.properties"></property>
</bean>
<bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
</bean>
<bean id = "messageSource" class = "org.springframework.context.support.ResourceBundleMessageSource">
<property name = "basenames">
<list>
<value>message</value>
</list>
</property>
</bean>
</beans>
2.学习记录
- 本节学习使用了如何对properties文件资源中获取自己想要的数据
- 学习了在properties文件中定义数据
- 学习了怎么样从中获取两个参数
标签:15,Spring,springframework,Framework,org,import,circle,messageSource,public 来源: https://blog.csdn.net/qq_38386316/article/details/88779064