【Spring框架】Bean的实例化方式

Bean的实例化方式

Spring容器中配置Bean的方式主要有两种:

  • 基于配置方式;
  • 基于注解方式;
基于XML配置方式装配Bean

Bean基于配置方式实例化有三种形式:

  • 通过无参构造实例化;
  • 基于静态工厂方式实例化;
  • 基于普通工厂方式实例化;
基于无参构造方式
  • 在博客Spring的IOC介绍中,所使用的实例就是无参构造来实例化的对象。
<?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  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <!--使用bean节点来创建对象 id属性表示对象 class属性表示要交给容器管理的对象的全路径-->
    <bean id="user"  class="com.tulun.bean.User"/>
</beans>
  • 注意:
    • 如果不指定构造函数,系统会生成默认的无参构造函数;
    • 如果指定有有参构造函数,必须显性的指定一个无参构造函数,否则实例化对象会抛出异常。
基于静态工厂方法实例化
  • 首先使用一个工厂的静态方法返回一个对象。
public class Factory {
    //创建User对象的静态方法
    public static User getUserBean() {
        return new User();
    }
}
  • 在配置文件中使用静态方法返回对象。
<?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  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <!--通过静态工厂方法创建对象,直接使用class来执行静态类,Factory-method指定方法就行-->
    <bean id="user" class="com.tulun.Factory" factory-method="getUserBean"/>
</beans>
基于普通工厂方法实例化
  • 通过工厂的非静态方法得到当前的一个对象。
public class Factory {
    //创建User对象的普工方法
    public User getUserBean() {
        return new User();
    }
}
  • 配置文件中使用工厂的非静态方法返回对象。
<?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  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <!--通过非静态工厂访问对象-->
    <!--首先创建工厂对象-->
    <bean id="factory" class="com.tulun.Factory"/>
    
    <!--指定工厂对象和工厂方法-->
    <bean id="user" class="com.tulun.bean.User" factory-bean="factory" factory-method="getUserBean"/>
</beans>
测试代码
  • 三种方式获取对象测试代码相同,结果也相同。
public class App {
    public static void main(String[] args) {
        //获取IOC的容器
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器来获取当前的对象
        User user = (User) applicationContext.getBean("user");
        System.out.println(user);
    }
}

在这里插入图片描述

Spring配置中Bean标签属性介绍
  • id属性:给当前的对象取名字,可以使用特殊字符 (/*)。
  • name属性:给当前对象取名字,不可以使用特殊字符。
  • calss属性:表示要交给容器管理的对象的全路径。
  • scope属性:表示当前的对象是单例还是多例 。
    • singleton:单例;
    • prototype:多例;
    • 注解形式:@Scope("")
基于注解方式装配Bean
  • 基于注解的形式要比xml配置文件的方式更加简洁。
  • 具体操作步骤如下:
1、Spring配置文件中引入context约束
<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.xsd    
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
</beans> 
2、Spring的配置文件中开启注解的扫描
<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.xsd    
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
              
    <!--开启注解扫描: base-package(必填)指令包路径: 会扫描类方法、属性上是否有注解-->
    <context:component-scan base-package="com.tulun.bean"></context:component-scan>
    
    <!--扫描属性上面的注解(不建议使用)-->
    <context:annotation-config></context:annotation-config>
</beans> 
  • 注:必须要引入context约束,否则无法出现context标签。
3、在类上添加@Component注解
@Component(value = "user")
//    <bean id="user"  class="com.tulun.bean.User"/> 等同于在配置中配置bean
public class User {
    private String id;
    private String name;
}
4、使用注解形式来获取对象实例
public class App {
    public static void main(String[] args) {
        //获取IOC的容器
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器来获取当前的对象
        User user = (User) applicationContext.getBean("user");
        System.out.println(user);
    }
}

在这里插入图片描述

Spring中注解的介绍

Spring中主要提供了4个注解来标注Bean:

  • @Component:通用的标注形式;
  • @Repository:对Dao实现类进行标注;
  • @Service:对Service实现类进行标注;
  • @Controller:对Controller实现类进行标注;

后端的业务分层一般业务请求顺序:Controller(进行URL结束和返回)-》Service(业务逻辑)-》Dao(访问数据层)

  • @Component是Spring提供的通用的组件注解;
  • @Controller@Service@Repository是@Component的衍生,功能一样,是可以互换的,使用不同的注解主要是为了区分被注解的类处于不同的业务层,使逻辑更加清晰。
  • 这四个注解主要定义bean,创建bean,使用上是要标注在类上,@Component(value="user")或者@Component("user")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值