Spring(一)IOC

IOC

public class Hello {
    private String str;

    public Hello() {
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}


<!--  使用spring来创建对象,在spring中,这些都称为bean  -->
<bean id="hello" class="com.zhang.pojo.Hello">
    <property name="str" value="Spring"/>
</bean>

IOC创建对象方式

  • 无参构造(默认)
private String name;

public User() {
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


<bean id="user" class="com.zhang.pojo.User">
    <property name="name" value="User"/>
</bean>

  • 有参构造
    • 下表赋值
    <bean id="user" class="com.zhang.pojo.User">
        <constructor-arg index="0" value="User"/>
    </bean>
  • 通过类型赋值(不建议)
    <bean id="user" class="com.zhang.pojo.User">
        <constructor-arg type="java.lang.String" value="User"/>
    </bean>
  • 通过参数名赋值
    <bean id="user" class="com.zhang.pojo.User">
        <constructor-arg name="name" value="User"/>
    </bean>

在配置文件加载的时候,容器中管理的对象就已经实例化

DI(依赖注入)

构造器注入

前面已有

set方式注入(重)

  • 依赖注入:Set
    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入
<?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="address" class="com.zhang.pojo.Address">
        <property name="address" value="address"/>
    </bean>

    <bean id="student" class="com.zhang.pojo.Student">

        <!--第一种,普通值注入,value-->
        <property name="name" value="zzh"/>

        <!--第二种,Bean注入,ref-->
        <property name="address" ref="address"/>

        <!--数组注入,ref-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>三国演义</value>
                <value>水浒传</value>
            </array>
        </property>

        <!--List-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
                <value>跑步</value>
            </list>
        </property>

        <!--map-->
        <property name="card">
            <map>
                <entry key="身份证" value="111111111122223333"/>
                <entry key="银行卡" value="121334648665494684"/>
            </map>
        </property>

        <!--set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>DOD</value>
            </set>
        </property>

        <!--null-->
        <property name="wife">
            <null/>
        </property>
        
        <!--Properties
        key=value
        -->
        <property name="info">
            <props>
                <prop key="driver">20190525</prop>
                <prop key="性别"></prop>
                <prop key="username">root</prop>
                <prop key="password"></prop>
            </props>
        </property>
    </bean>

</beans>

扩展方式注入

通过p命名空间和c命名空间注入
不可以直接使用,需要导入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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入,可以直接注入属性的值:Property的意思-->
    <bean id="user" class="com.zhang.pojo.User" p:name="zzh" p:age="18"/>

    <!--c命名空间注入,通过构造器注入:construct-args-->
    <bean id="user2" class="com.zhang.pojo.User" c:name="zjh" c:age="20"/>

</beans>

bean作用域

在这里插入图片描述
1.单例模式(默认)

<bean id="user" class="com.zhang.pojo.User" p:name="zzh" p:age="18" scope="singleton"/>

2.原型模式:每次从容器中获取的时候都会创建一个新对象

<bean id="user" class="com.zhang.pojo.User" p:name="zzh" p:age="18" scope="prototype"/>

3.request、session、application,这些只能在web开发中使用

Bean的自动装配

  • 自动装配是Spring满足bean依赖的一种方式
  • spring会在上下文中自动寻找,并且给bean自动装配属性

在Spring中有三种装配的方式
1.在xml中显示的配置(手动)
2.在java中显示配置
3.隐式的自动装配bean【重点】

测试
byName自动装配
<!--
    byName:会自动在容器上下文中查找,和自己对象set方法后面值相对应的对象
-->
<bean name="person" class="com.zhang.pojo.Person" autowire="byName"/>
byType自动装配
<!--
    byType:会自动阿紫容器上下文中查找,和自己对象属性相同的对象
-->
<bean name="person" class="com.zhang.pojo.Person" autowire="byType"/>

总结:

  • byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自己注入的属性的set方法的值一致
  • byType的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
使用注解实现自动装配

使用注解需要的支持:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

@Autowired
直接在属性上使用,也可以在set方法上使用

@Nullable   自断后标记这个注解,说明这个字段可以为null
public @interface Autowired {
    boolean required() default true;
}


//如果显式的定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
@Autowired(required = false)


@Resource  功能更强大,但是没有提示

总结:
@Resource和@Autowired的区别:

  • 都是用来自动装配的,都可以放在属性字段上
  • @Autowired,通过byType的方式实现
  • @Resource 默认通过byName方式来实现的,如果找不到名字,就通过byType。

使用注解开发

bean
属性如何注入
//等价于之前的<bean id = "user" class = "xxx"/>
//@Component组件
@Component
public class User {
    @Value("zzh")
    private String name;
    
}
衍生的注解

@Component有几个衍生注解,在我们web开发中,按照mvc三层架构分层

  • dao【@Repository】
  • service【@Service】
  • conreoller【@Conreoller】
    这四个注解功能是一样的额,都是将某个类注册到spring中,装配Bean
自动装配
作用域

@Scope

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值