一.基本的操作

1.基本配置信息:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd">

2.实例化容器的方式 

 <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="itemDao" ref="itemDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

     可以创建多个对象。

3.通过这种方式引入其他的配置文件

<beans>
    <import resource="services.xml"/>
</beans>

 4.通过".groovy"写beans暂时不知道有什么用

beans {
    dataSource(BasicDataSource) {
        driverClassName = "org.hsqldb.jdbcDriver"
        url = "jdbc:hsqldb:mem:grailsDB"
        username = "sa"
        password = ""
        settings = [mynew:"setting"]
    }
    sessionFactory(SessionFactory) {
        dataSource = dataSource
    }
    myService(MyService) {
        nestedBean = { AnotherBean bean ->
            dataSource = dataSource
        }
    }
}

5.获取容器的方法

(1)主要的方法

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);

// use configured instance
List<String> userList = service.getUsernameList();

(2)加载".groovy"配置文件的方法

ApplicationContext context = new GenericGroovyApplicationContext("services.groovy", "daos.groovy");

    还有其他的,以后再说。

上一篇:UVM-工厂机制(factory)


下一篇:Spring源码学习篇3 - XML配置了解 Profiles