[小白进阶日记]Spring中Bean的三种装配方式

Spring中Bean的三种装配方式


1. XML配置文件方式
2.@注解方式
3.Java代码方式
4.网络资料

XML配置文件形式
首先引入 xml配置的头
<?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.xsd">
        
        配置bean
        
</beans>

1.构造注入方式
<bean id="唯一ID" class="类全限定名">
    普通数据类型注入(八大基本数据类型+String)
    <constructor-arg name="属性名" value=""></constructor-arg>
    
    引用数据类型注入  此处需将要引用的bean也交给Spring容器管理
    <constructor-arg name="属性名" ref="引用对象的ID"></constructor-arg>
    
    List属性注入
    <constructor-arg name="属性名">
        <list>
            <value></value>
        </list> 
    </constructor-arg>
    
    Map属性注入
    <constructor-arg name="属性"> 
        <map> 
            <entry key="key键" value="value值"></entry>
        </map> 
    </constructor-arg> 
    
    property属性注入
    <constructor-arg name="属性"> 
        <props> 
            <prop key="key键">value值</prop>
        </props>
    </constructor-arg> 
2.属性注入方式
<bean id="唯一ID" class="类的全限定名">
    普通数据类型注入(八大基本数据类型+String)
    <property name="属性名" value=""></property>
    
    //引用数据类型注入  此处需将要引用的bean也交给Spring容器管理
    <property name="属性名" ref="引用对象的ID"></property>
    
    List属性注入
    <property name="属性名">
        <list>
            <value></value>
        </list> 
    </property>
    
    Map属性注入
    <property name="属性"> 
        <map> 
            <entry key="key键" value="value值"></entry>
        </map> 
    </property> 
    
    property属性注入
    <property name="属性"> 
        <props> 
            <prop key="key键">value值</prop>
        </props>
    </property> 
</bean>
@注解方式
1.配置xml文件
<?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:aop="http://www.springframework.org/schema/aop"       
    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
        https://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="要扫描的包"></context:component-scan>

</beans>
2.声明Bean
/**
Component(value = "")默认的value是类名首字母小写
可以自己定义value 也就是 bean 的ID
*/
@Component 
public class 类名{
    /**通过Value 的方式进行赋值*/
    @Value(value = "")
    private int 属性;
     /**
    自动装配的方式进行赋值 required 不写默认为true 为必须注入的 false 为可以不注入 
    首先AutoWired会根据类型去Spring容器中寻找符合的bean 如果没有那么会去查看required,
    如果required是true那么就抛出异常(Excption) 如果是false那么就不进行注入让其为null
    */
    @AutoWired(required=boolean/**
    如果属性是一个接口,AutoWired找到的该接口实现类有多个
    那么就会进行Qualifier查找,根据value去Spring中找id为该value的bean来进行装配
    */
    @Qualifier(value = "") 
    private Class 引用的属性;
    /**
    根据name 去Spring中寻找bean 此处name 为 bean的 ID
    */
    @Resource(name ="")
    private Class 引用的属性;
    
   // 以上注解均可放在set方法上
 }
Java方式
1.配置xml文件
<?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:aop="http://www.springframework.org/schema/aop"       
    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
        https://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="要扫描的包"></context:component-scan>

</beans>
Java 文件
@Configuration
public class JavaConfig { 
    @Bean(name = "",initMethod = "初始化时执行方法",destroyMethod = "销毁时执行方法")
    public Persion getPersion(){
        return new Persion();    
    }
 }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值