Spring 프로필 을 불 러 오 는 몇 가지 방법

6548 단어 springcxmlbeanlog4j
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

좋은 웹페이지 즐겨찾기