6.笔记JAVA框架学习——Bean配置Properties属性

6.笔记JAVA框架学习——Bean配置Properties属性

继续上节的学习笔记。

在app.xml文件中,增加如下,配置bean:

      <bean id="service"class="Service">

            <property name="dao">

                  <!-- 内部 bean,类似于匿名内部类对象. 不能被外部的 bean 来引用, 也没有必要设置 id 属性 -->

                  <bean class="Dao">

                        <property name="dataSource"value="c3p0"></property>

                  </bean>

            </property>

      </bean>

     

      <bean id="action"class="Action">

            <property name="service"ref="service"></property>

            <!-- 设置级联属性(了解) -->

            <property name="service.dao.dataSource"value="DBCP2"></property>

      </bean>

增加action.java内容如下:

publicclass Action {

 

      private Service service;

     

      publicvoidsetService(Service service) {

            this.service= service;

      }

     

      public ServicegetService() {

            returnservice;

      }

     

      publicvoid execute(){

            System.out.println("Action's execute...");

            service.save();

      }

     

}

修改主函数,main.java如下:

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Main {

                  

                   public static void main(String[] args) {               

                                    //1. 创建 Spring IOC 容器

                                    ApplicationContext apx = newClassPathXmlApplicationContext("app.xml");

                                    Service service = (Service)apx.getBean("service");

                                    System.out.println(service);

                                    service.save();

                   }               

}

执行如下:

Dao'sConstructor...

Service@72d818d1

Service'ssave

save by DBCP2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值