栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring-bean作用域

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring-bean作用域

紧接着上文《Spring-IOC》,说下Spring-bean作用域。

bean作用域

在Spring中,可以通过bean标签里 scope 属性设置单实例还是多实例,默认情况下,是单实例。

  • singleton:表示是单实例对象,加载spring配置文件时候就会创建单实例对象。
  • prototype:表示是多实例对象,不是在加载spring配置文件时候创建对象,而是在调用getBean方法时候创建多实例对象。
bean生命周期

详见《Spring Bean的生命周期(二)》

FactoryBean

Spring有两种类型bean,一种普通bean,另外一种工厂bean(FactoryBean)。

  • 普通bean:在配置文件中定义bean类型就是返回类型
  • 工厂bean:在配置文件中定义bean类型可以和返回类型不一样

第一步 创建类,让这个类作为工厂bean,实现接口 FactoryBean。

public class MyBean implements FactoryBean {
    //定义返回bean
    @Override
    public Course getObject() throws Exception {
        Course course = new Course();
        course.setCname("abc");
        return course;
    }
    @Override
    public Class getObjectType() {
        return null;
    }
    @Override
    public boolean isSingleton() {
        return false;
    }
}

第二步 实现接口里面的方法,在实现的方法中定义返回的bean类型



测试:

@Test
public void test3() {
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean3.xml");
    Course course = context.getBean("myBean", Course.class);
    System.out.println(course);
}
xml自动装配

根据指定装配规则(属性名称byName或者属性类型byType),Spring自动将匹配的属性值进行注入。

  • @Autowired:根据属性类型进行自动装配
  • @Qualifier:和@Autowired一起使用,根据名称进行注入。
  • @Resource:可以根据类型注入,也可以根据名称注入。
  • @Value:注入普通类型属性

《Spring-IOC》-基于注解方式实现 已经说明。

引入外部属性文件

以配置druid连接池为例,首先引入druid-1.1.9.jar。配置文件增加数据库配置
applicationContext.xml:




    
        
        
        
        
    

程序调用:

package springIocTest;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("springIocTest/applicationContext.xml");

        DruidDataSource dataSource = context.getBean("dataSource",DruidDataSource.class);
        System.out.println(dataSource.getDriverClassName());
        System.out.println(dataSource.getPassword());
    }
}

运行结果:
com.mysql.jdbc.Driver
root

以上是数据库配置,但如果不修改applicationContext.xml的情况下,通过druid自带的配置文件进行动态加载,就需要引入外部属性文件配置。

1、创建外部属性文件

创建外部属性文件,*.properties格式结尾的文件,配置数据库信息。
jdbc.properties:

prop.driverClassName=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.username=root
prop.password=admin
2、引入外部属性文件到spring配置文件中 引入context名称空间

引入xmlns:context=“http://www.springframework.org/schema/context”

在spring配置文件使用标签引入外部属性文件

具体applicationContext.xml如下:




    
    

    
    
        
        
        
        
    

指定外部属性文件
${prop.driverClassName}、${prop.password} 等动态读取指定外部属性文件配置的prop.driverClassName、prop.password等。

程序调用:

package springIocTest;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("springIocTest/applicationContext.xml");

        DruidDataSource dataSource = context.getBean("dataSource",DruidDataSource.class);
        System.out.println(dataSource.getDriverClassName());
        System.out.println(dataSource.getPassword());
    }
}

运行结果:
com.mysql.jdbc.Driver
admin

通过结果看出,只需要修改外部属性文件jdbc.properties的值,Spring获取的bean对应值就会动态修改。

和 区别

仅用于激活已经在spring容器里注册过的bean。对于没有在spring容器中注册的bean,它并不能执行任何操作。
除了具有的功能之外,还具有自动将@component,@service,@Repository等注解的对象注册到spring容器中的功能。

有两个class A,B,并且B的对象被注入到A中。

package Annotation;
public class B {
    public B() {
        System.out.println("creating bean B: " + this);
    }
}

package Annotation;
public class A {
    private B bbb;
    public A() {
        System.out.println("creating bean A: " + this);
    }
    public void setBbb(B bbb) {
        System.out.println("setting A.bbb with " + bbb);
        this.bbb = bbb;
    }
}

在applicationContext.xml中加入下面的配置 :



   

加载applicationContext.xml配置文件,

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Annotation/applicationContext.xml"});
A a = (A) context.getBean("aBean");
System.out.println("a.bbb: " + a.bbb);

将得到下面的结果:

:
package Annotation;
import org.springframework.beans.factory.annotation.Autowired;

public class A {
    @Autowired
    public B bbb;

    public A() {
        System.out.println("creating bean A: " + this);
    }
}

applicationContext.xml配置文件去除属性,简化后:




运行结果:

这就是 所做的事情,用于激活那些已经在spring容器里注册过的bean。

如果我们把applicationContext.xml配置文件修改为:


运行结果:

那是因为对于没有在spring容器中注册的bean,它并不能执行任何操作。

package Annotation;
import org.springframework.stereotype.Component;

@Component(value = "bBean")
public class B {
    public B() {
        System.out.println("creating bean B: " + this);
    }
}

package Annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component(value = "aBean")
public class A {
    @Autowired
    public B bbb;

    public A() {
        System.out.println("creating bean A: " + this);
    }
}

applicationContext.xml配置文件:


运行结果:

除了具有的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。

如果同时使用这两个配置会不会出现重复注入的情况呢?



答案:因为和 同时存在的时候,前者会被忽略。如@autowire,@resource等注入注解只会被注入一次!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/972913.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号