Spring 프로필 을 불 러 오 는 몇 가지 방법
package com.liu.test;
public class BeanFile {
private String beanFile = " Bean ";
public void setBeanFile(String beanFile) {
this.beanFile = beanFile;
}
public String getBeanFile() {
return beanFile;
}
}
package com.liu.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
public class MainBeanFile {
public static void main(String[] args) {
// HelloBean
BeanFile bf = new BeanFile();
System.out.println(bf.getBeanFile());
/**
* XmlBeanFactory(Resource resource) Resource xml
* Resource :AbstractResource, ClassPathResource, FileSystemResource,
* InputStreamResource, ServletContextResource, UrlResource
*/
/*
* InputStreamResource(InputStream inputStream) bean.xml ,
*/
InputStream is = null;
try {
is = new FileInputStream("c:/workspace/spring/WebRoot/WEB-INF/InputStreamResource.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Resource resource = new InputStreamResource(is);
sayHello(resource);
/*
* ClassPathResource(String path) bean.xml (src)
*/
resource = new ClassPathResource("ClassPathResource.xml");
sayHello(resource);
/*
* FileSystemResource(String path) bean.xml ,
* c:/workspace/spring/src/com/liu/test/FileSystemResource.xml
*/
resource = new FileSystemResource("c:/workspace/spring/src/com/liu/test/FileSystemResource.xml");
sayHello(resource);
/*
* Properties bean.properties -- (src)
*/
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("bean.properties"));
BeanFactory factory = (BeanFactory) reg;
bf = (BeanFile) factory.getBean("beanFile");
System.out.println(" " + bf.getBeanFile() + " Bean.properties");
/*
* ApplicationContext bean.xml -- (src)
*/
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"/ApplicationContext.xml");
bf = (BeanFile) appContext.getBean("beanFile");
System.out.println(" " + bf.getBeanFile() + " Bean.xml");
}
public static void sayHello(Resource resource) {
BeanFactory factory = new XmlBeanFactory(resource);
BeanFile bf = (BeanFile) factory.getBean("beanFile");
System.out.println(" " + bf.getBeanFile() + " Bean.xml");
}
}
InputStreamResource.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beanFile" class="com.liu.test.BeanFile">
<property name="beanFile">
<value>InputStreamResource(InputStream inputStream)</value>
</property>
</bean>
</beans>
ClassPathResource.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beanFile" class="com.liu.test.BeanFile">
<property name="beanFile">
<value>ClassPathResource(String path)</value>
</property>
</bean>
</beans>
FileSystemResource.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beanFile" class="com.liu.test.BeanFile">
<property name="beanFile">
<value>FileSystemResource(String path)</value>
</property>
</bean>
</beans>
ApplicationContext.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beanFile" class="com.liu.test.BeanFile">
<property name="beanFile">
<value>ApplicationContext</value>
</property>
</bean>
</beans>
bean.properties(Spring ,bean.properties (src) )
beanFile.class=com.liu.test.BeanFile
beanFile.beanFile=properties
:( log4j )
Bean
InputStreamResource(InputStream inputStream) Bean.xml
ClassPathResource(String path) Bean.xml
FileSystemResource(String path) Bean.xml
properties Bean.properties
ApplicationContext Bean.xml
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.