Spring-加载配置文件3种方式

1.创建项目
    spring092601
2.引入spring jar包
    commons-logging.jar
    junit-4.4.jar
    log4j.jar
    spring-beans-3.2.0.RELEASE.jar
    spring-context-3.2.0.RELEASE.jar
    spring-core-3.2.0.RELEASE.jar
    spring-expression-3.2.0.RELEASE.jar
3.添加配置文件
    在conf下添加spring的核心配置文件applicationContext.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:p="http://www.springframework.org/schema/p"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    </beans>
4.创建业务bean
    在src目录下创建
    包名:cn.jbit.spring092601.domain
    /**
     * spring入门
     * @author Administrator
     *
     */
    public class HelloSpring implements Serializable {
        private String name;
        public HelloSpring() {
            System.out.println("调用无参构造方法");
        }
        public HelloSpring(String name) {
            super();
            this.name = name;
        }
        public void setName(String name) {
            System.out.println("set");
            this.name = name;
        }
        public String getName() {
            System.out.println("get");
            return name;
        }
    }
5.在配置文件中编写bean的配置,并注入属性值,也就是我们的DI(Dependency Injection)依赖注入
    配置如下:
    <?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:p="http://www.springframework.org/schema/p"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
        <!-- spring入门
            控制反转IOC
            默认构造方法装配Bean
         -->
        <bean id="helloSpring" class="cn.jbit.spring092601.domain.HelloSpring">
            <!--
                给属性注入一个值:hello 
             -->
            <property name="name">
                <value>hello</value>
            </property>
        </bean>
    </beans>
6.测试bean的属性注入
    在test目录下测试
    包名:cn.jbit.spring092601.domain
    public class HelloSpringTest {
        /**
         * 1.ClassPathXmlApplicationContext方式
         * 依赖注入方式实现
         */
        @Test
        public void testHellpSpring3(){
            /*
             * 对象由spring创建
             */
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
            System.out.println(helloSpring.getName());
        }
        
        
        /**
         * 2.FileSystemXmlApplicationContext实现加载applicationContext.xml配置文件
         */
        @Test
        public void testHellpSpring4(){
            /*
             * 对象由spring创建
             */
            ApplicationContext context = new FileSystemXmlApplicationContext("C:/Documents and Settings/Administrator/Workspaces/MyEclipse 8.5/spring092601/conf/applicationContext.xml");
            HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
            System.out.println(helloSpring.getName());
        }
        
        
        /**
         *3.BeanFactory实现加载applicationContext.xml文件
         */
        @Test
        public void testHellpSpring5(){
            /*
             * 构造函数方式
             * 对象由spring创建,调用无参构造方法来实例化Bean
             */
            BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
            //调用set方法
            HelloSpring helloSpring = (HelloSpring) beanFactory.getBean("helloSpring");
            //调用get方法
            System.out.println(helloSpring.getName());
        }

    }

本文转自  素颜猪  51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1558444


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值