spring的依赖注入---Bean章

依赖注入
IOC的作用:降低程序的耦合性
依赖关系管理:交给spring管理
在当前对象中需要用到其他对象时 由spring提供 只需在配置文件中配置说明
依赖关系的维护:
能注入的数据有三类:
1)基本类型和String类
2)其他bean类型(在配置文件中或者注解配置过的bean)
3)复杂类型/集合类型
注入的方式有三种:
1)使用构造函数提供
2)使用set方法提供
3)使用注解提供


解释一个bean的作用 通过bean中的class属性中的对象创建一个对象存入sringIOC的容器当中 并且可以通过id属性中的值获取


构造函数注入 constractor-arg
type:指定注入的数据的类型, 该数据类型也是构造函数中某个或者其他参数的类型
index:用于指定注入的数据在构造函数中的下标索引位置 ,索引是从0开始的
name:用于指定构造函数中的参数名称赋值
value: 用于提供基本类型和String类型的属性
ref: 用于指定引用其他bean类型 它指的就是spring中的IOC核心容器中出现过的对象 引用关联的对象

    优势:在注入数据时(bean对象时)是必须的操作,否则无法创建成功
    弊端:改变了类(bean对象)的实例化方式,使我们在创建对象时 如果用不到这些数据也要提供

提供接口

public interface IAccountService {

    void insertAccount();

}

接口的实现类

public class AccountServiceImpl implements IAccountService {
    //如果是经常改变的数据就不适合注入的方式
    private String name;
    private Integer age;
    private Date birthday;

    public AccountServiceImpl(String name, Integer age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public void insertAccount() {
        System.out.println("进入方法");
    }
}

bean.xml

<bean id="accountService" class="com.spring.service.impl.AccountServiceImpl">
            <constructor-arg  name="name" value="text" ></constructor-arg>
            <constructor-arg  name="age" value="18" ></constructor-arg>
            <constructor-arg  name="birthday" ref="now" ></constructor-arg>
        </bean>
        <bean id="now" class="java.util.Date"></bean>

启动类

public class Client {
    public static void  main(String[] args){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService service = ac.getBean("accountService", IAccountService.class);
        service.insertAccount();
    }
}

set方法注入 更常用
涉及标签:propotype
标签属性:
name:用于指定注入是用到的set方法名称
value:用于提供基本类型和String类
ref: 用于指定引用其他bean类型 它指的就是spring中的IOC核心容器中出现过的对象 引用关联的对象

      优势:创建对象时。没有明确的限制。可以直接使用构造函数
      弊端:如果有某个成员必须有值,则获取对象时set方法没有执行
public class AccountServiceImpl2 implements IAccountService {
    //如果是经常改变的数据就不适合注入的方式
    private String name;
    private Integer age;
    private Date birthday;

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

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void insertAccount() {
        System.out.println("进入方法2");
    }
}

<bean id="accountService2" class="com.spring.service.impl.AccountServiceImpl2">
        <property name="name" value="test"></property>
        <property name="age" value="18"></property>
        <property name="birthday" ref="now"></property>
    </bean>
    <bean id="now" class="java.util.Date"></bean>
public class Client {
    public static void  main(String[] args){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService service = ac.getBean("accountService2", IAccountService.class);
        service.insertAccount();
    }
}

复杂类型/集合注入
用于给List结构集合的标签:list array set
用于给map结构集合的标签:map props
结构相同标签可以互换

public class AccountServiceImpl3 implements IAccountService {

    private String[] myStrs;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String, String> map;
    private Properties properties;

    public void setMyStrs(String[] myStrs) {
        this.myStrs = myStrs;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void insertAccount() {
        System.out.println(Arrays.toString(myStrs));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(map);
        System.out.println(properties);
    }
}

<bean id="accountService3" class="com.spring.service.impl.AccountServiceImpl3">
        <property name="myStrs">
            <array>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
        <property name="mySet">
            <set>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="testa" value="aaa"></entry>
                <entry key="testb">
                    <value>bbb</value>
                </entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="testa">aaa</prop>
                <prop key="testb">bbb</prop>
                <prop key="testc">ccc</prop>
            </props>
        </property>
    </bean>
</beans>
public class Client {
    public static void  main(String[] args){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService service = ac.getBean("accountService3", IAccountService.class);
        service.insertAccount();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值