Spring的核心api和两种实例化方式

一.spring的核心api

Spring有如下的核心api

容纳Bean

BeanFactory :这是一个工厂,用于生成任意bean。采取延迟加载,第一次getBean时才会初始化Bean。Bean工厂是最简单的容器,提供了基础的依赖注入支持。创建各种类型的Bean。

ApplicationContext(应用上下文):是BeanFactory的子接口,功能更强大。(国际化处理、事件传递、Bean自动装配、各种不同应用层的Context实现)。当配置文件被加载,就进行对象实例化。建立在bean工厂的基础之上,提供系统架构服务。applicationcontext.xml中的Bean通过反射到applicationContext上下文容器中。

二、加载Bean.xml ApplicationContext.xml对象引用的三种方式

ClassPathXmlApplicationContext 用于加载classpath(类路径、src)下的xml,加载xml运行时位置 --> /WEB-INF/classes/...xml

FileSystemXmlApplicationContext 用于加载指定盘符下的xml,加载xml运行时位置 --> /WEB-INF/...xml,通过java web ServletContext.getRealPath() 获得具体盘符,绝对路径

XmlWebApplicationContext从web系统中进行加载,当Tomcat启动时就进行加载

三、Spring的两种实例化方式

1.使用ApplicationContext方式进行加载

2.使用BeanFactory方式进行加载

知识点:从ApplicationContext应用上下文容器中获取Bean和从Bean工厂容器中获取Bean有什么区别。

当我们使用ApplicationContext去实例化Bean.xml时,该文件中配置的Bean被实例化(该bean scope时singleton单例)。配置的Bean,如果是singleton,则提前实例化好,速度快。如果不是singleton,Bean也会跟BeanFactory相同进行延时加载。

1
ApplicationContext ac=new ClassPathXmlApplicationContext(path); 

当我们使用BeanFactory去获取Bean,这个时候只是实例化BeanFactory容器,而不实例化容器中的Bean,只有当我们使用getBean某个bean的时候,才会实时的创建Bean。

1
BeanFactory bf=new XmlBeanFactory(new ClassPathResource(path));

ApplicationContext可以提前加载Bean,BeanFactory是实时加载Bean。BeanFactory一般在移动端使用。BeanFactory可以节省内存,但是实时创建加载,速度慢。

一般没有特殊要求,应当使用ApplicationContext完成。

在ApplicationContext中配置的Bean默认是singleton单例。

MainApp.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class MainApp {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
 
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        HelloWorld obj2=(HelloWorld)context.getBean("helloWorld");
        //bybService.sayByb();
        System.out.println(obj+"    "+obj2);
 
    }
 
}

Bean.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
    <context:annotation-config />
    <bean id="helloWorld" class="HelloWorld">
        <property name="message" value="aaa!" />
    </bean>
 
</beans>

HelloWorld.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import org.springframework.beans.factory.annotation.Autowired;
 
public class HelloWorld {
    public String message;
 
 
    public void setMessage(String message) {
        this.message = message;
    }
 
    public void getMessage() {
        System.out.println("message : " + message);
    }
 
    public void sayHello(){
        System.out.println(message+"Hello");
    }
 
}

 运行结果如下:

 可以看出两个bean获取结果相同,为单例。也就是说ApplicationContext默认创建Bean的模式为单例模式。

如果更改Bean的创建模式为原型模式,scope="prototype"

Bean.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
    <context:annotation-config />
    <bean id="helloWorld" scope="prototype" class="HelloWorld">
        <property name="message" value="aaa!" />
    </bean>
 
</beans>

 运行结果如下:

可以看见每次创建的Bean都是一个新的Bean。

第一步 建立Spring工程,并向其中导入必须的jar包

第二步 建立BookDao接口,BookDaoImpl实现类,BookService接口,BookServiceImpl实现类

1
2
3
4
5
package com.zk.myspring;
 
public interface BookDao {
    public void addBook();
}
1
2
3
4
5
6
7
8
9
10
11
package com.zk.myspring;
 
public class BookDaoImpl implements BookDao{
 
    @Override
    public void addBook() {
        // TODO Auto-generated method stub
        System.out.println("addBook");
    }
 
}
1
2
3
4
5
package com.zk.myspring;
 
public interface BookService {
    public abstract void addBook();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.zk.myspring;
 
public class BookServiceImpl implements BookService{
 
    //方式一:接口等于实现类
    //private BookDao bookDao=new BookDaoImpl();
    //方式二:接口+set方法
    private BookDao bookdao;
     
    //BookService依赖注入 BookDao
    public void setBookdao(BookDao bookdao) {//依赖注入
        this.bookdao = bookdao;
    }
 
    @Override
    public void addBook() {
        // TODO Auto-generated method stub
        //this.bookdao.addBook();
        System.out.println("add book");
    }
}

第三步 创建一个ApplicationContext.xml文件,并在文件中配置BookDao和BookService的bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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: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">
<!-- 创建Dao -->
<!-- 模拟spring执行过程
创建Service实例:BookService bookservice=new BookServiceImpl();  IOC <bean>
创建Dao实例:BookDao bookDao=new BookDaoImpl();   IOC
将dao设置给service:bookservice.setBookDao(bookDao);    DI  <property>
 
<property> 用于属性注入
name:bean的属性名,通过setter方法获得
ref:另一个bean的id值的引用
 -->
<!--创建Dao  -->
<bean id="bookDaoId" class="com.zk.myspring.BookDaoImpl" ></bean>
<!-- 创建service 依赖注入bookdaoId-->
<bean id="bookServiceId" class="com.zk.myspring.BookServiceImpl">
<property name="bookdao" ref="bookDaoId"></property>
</bean>
</beans> 

最后一步 使用两种方式实例化bean,我们可以使用ApplicationContext和BeanFactory分别实例化Bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.zk.myspring;
 
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
 
public class TestBook {
    public static void main(String[]args)
    {
        //从spring容器中获得
        String path="ApplicationContext.xml";
        ApplicationContext ac=new ClassPathXmlApplicationContext(path);
        BookService bs=(BookService) ac.getBean("bookServiceId");
        bs.addBook();
    }
    @Test
    public void test1(){
        //使用BeanFactory实例化
        String path="ApplicationContext.xml";
        BeanFactory bf=new XmlBeanFactory(new ClassPathResource(path));
        BookService bs=(BookService) bf.getBean("bookServiceId");
        bs.addBook();
    }
}

分别运行两个实例

ApplicationContext运行结果

BeanFactory运行结果

成功

四、自定义工厂

新建一个Spring_003 工程,并加入MyBeanFactory.java、TestBeanFactory.java、UserService.java、UserServiceImpl.java

MyBeanFactory表示自定义工厂

1
2
3
4
5
6
7
package com.zk.BeanFactory;
 
public class MyBeanFactory {
    public static UserService createService(){
        return new UserServiceImpl();
    }
}

UserService.java

1
2
3
4
5
package com.zk.BeanFactory;
 
public interface UserService {
    public void addUser();
}

UserServiceImpl.java

1
2
3
4
5
6
7
8
9
10
11
package com.zk.BeanFactory;
 
public class UserServiceImpl implements UserService{
 
    @Override
    public void addUser() {
        // TODO Auto-generated method stub
        System.out.println("UserService");
    }
 
}

配置ApplicationContext.xml

1
2
3
4
5
6
7
8
9
<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userservice" class="com.zk.BeanFactory.UserServiceImpl"></bean>
<!--  <bean id="user" class="com.zk.BeanFactory.User"></bean>-->
</beans>

最后写一个TestBeanFactory.java文件

1
2
3
4
5
6
7
8
9
10
11
12
package com.zk.BeanFactory;
 
import org.junit.Test;
 
 
public class TestBeanFactory {
    @Test
    public void Demo(){
        UserService userservice=MyBeanFactory.createService();
        userservice.addUser();
    }
}

执行结果

success

posted @   leagueandlegends  阅读(1134)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示