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<T>
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<String> 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:

<?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">

 <!-- services -->

 <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>

 <!-- more bean definitions for services go here -->

</beans>

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

<?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">

 <bean id="accountDao"
     class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
     <!-- additional collaborators and configuration for this bean go here -->
 </bean>

 <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
     <!-- additional collaborators and configuration for this bean go here -->
 </bean>

 <!-- more bean definitions for data access objects go here -->

</beans>

==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等所需的约束

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
<beans>

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

 <!-- Spring基本开发所需的依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>

<!-- 编写jsp时报错所需的依赖 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

<!-- 日志记录所需的依赖 -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
</dependency>

<!-- 整合单元测试所需依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.7.RELEASE</version>
    <scope>test</scope>
</dependency>

<!-- JDBC模板的使用所需的依赖 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>

<!-- DBCP所需依赖 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.6.2</version>
</dependency>

<!-- c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency> 

<!-- 这个有点老,引入不了 貌似不用了上次更新2009-->
<!-- <dependency>
    <groupId>com.mchange.c3p0</groupId>
    <artifactId>com.springsource.com.mchange.v2.c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency> -->

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容