Spring框架(二)

二,Spring工厂类的结构图

spring工厂类结构图.png

1,ApplicationContext和BeanFactory(继承关系)

The org.springframework.beans and org.springframework.context packages are the basis for Spring

Framework’s IoC container. The BeanFactory interface provides an advanced configuration
mechanism capable of managing any type of object. ApplicationContext is a sub-interface of
BeanFactory. It adds:
• Easier integration with Spring’s AOP features
• Message resource handling (for use in internationalization)
• Event publication
• Application-layer specific contexts such as the WebApplicationContext for use in web
applications.

In short, the BeanFactory provides the configuration framework and basic functionality, and the

ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a
complete superset of the BeanFactory

Several implementations of the ApplicationContext interface are supplied with Spring. In stand-alone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext. While XML has been the traditional format for defining configuration metadata, you can instruct the container to use Java annotations or code as the metadata format by providing a small amount of XML configuration to declaratively enable support for these additional metadata formats.

In most application scenarios, explicit user code is not required to instantiate one or more instances of a Spring IoC container. For example, in a web application scenario, a simple eight (or so) lines of boilerplate web descriptor XML in the web.xml file of the application typically suffices

ApplicationContext继承BeanFactory

BeanFactory:调用getBean的时候,才会生成类的实例

ApplicationContext :新版本的工厂类,加载配置文件的时候,就会将Spring管理的类都实例化。

2,实际开发中用到的实现类

ApplicationContext有三个实现类

  • ClassPathXmlApplicationContext :加载类路径下的配置文件

  • FileSystemXmlApplicationContext :加载文件系统下的配置文件

  • AnnounceApplicationContext:使用注解时加载配置类

Several implementations of the ApplicationContext interface are supplied with Spring. In stand-

alone applications, it is common to create an instance of ClassPathXmlApplicationContext or
FileSystemXmlApplicationContext.

3,使用容器(Using the Container)

官方实例

The ApplicationContext is the interface for an advanced factory capable of maintaining a registry of
different beans and their dependencies. By using the method T getBean(String name, Class
requiredType), you can retrieve instances of your beans.
The ApplicationContext lets you read bean definitions and access them, as the following example
shows:

// 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 userList = service.getUsernameList();

4,实例化一个容器

The location path or paths supplied to an ApplicationContext constructor are resource strings that let the container load configuration metadata from a variety of external resources, such as the local file system, the Java CLASSPATH, and so on.

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

文档中的一个例子:

The following example shows the service layer objects (services.xml) configuration file:




 

 
     
     
     
 

 


The following example shows the data access objects daos.xml file:




 
     
 

 
     
 

 


==daos.xml中的acountDao和itemDao做为了属性注入了service.xml中,这是什么开发思维??==

service调用DAO不是很正常的事吗(2020.2.13)

三,开发前准备

1,整合Junit单元测试

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
*整合对单元测试
*ApplicationContext ac = new        *ClassPathXmlApplicationContext("applicationContext.xml");
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class AopTest {
    
    @Resource(name="goods")
    private GoodsDao goods;
    
    @Test
    public void aopTest1() {
        goods.find();
        goods.add();
        goods.update();
        goods.delete();
        
    }
    
}

2,xml约束配置

整合p命名空间,context,tx(事务),aop等所需的约束



    

3,基于maven开发的所需的依赖

 

    org.springframework
    spring-context
    5.1.7.RELEASE




    javax.servlet
    javax.servlet-api
    4.0.1
    provided




    org.apache.logging.log4j
    log4j-core
    2.11.1




    org.springframework
    spring-test
    5.1.7.RELEASE
    test




    mysql
    mysql-connector-java
    5.1.47


    org.springframework
    spring-jdbc
    5.1.7.RELEASE


    org.springframework
    spring-tx
    5.1.7.RELEASE




    org.apache.commons
    commons-dbcp2
    2.5.0


    org.apache.commons
    commons-pool2
    2.6.2




    com.mchange
    c3p0
    0.9.5.2
 




你可能感兴趣的:(Spring框架(二))