Spring入门一(IOC,配置对象创建的三种方式,依赖注入的两大方式)

Spring的主要思想是控制反转(IOC)面向切面编程(AOP)
1.控制反转:对象的交由Spring容器来管理:对象的创建、属性注入。
2.bean对象创建的三种方式:通过构造方法、通过静态方法、通过实例方法。

 <bean id="example1" class="com.Example"/><!--通过构造器-->
        <bean id="example2" class="com.Example" factory-method="createInstance"/><!--通过静态方法-->
        <bean id="example3" factory-bean="example1" factory-method="createObject" /><!--通过实例方法-->

main方法:

public class Main {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        System.out.println(context.getBean("example1"));
        System.out.println(context.getBean("example2"));
        System.out.println(context.getBean("example3"));
    }
}

3.属性的注入:通过构造器,可以使用constructor的index属性、name属性、type属性来和bean对象中的属性相匹配。

<bean id="student" class="com.Student">
    <constructor-arg index="0" value="zhangsan"/>
    <constructor-arg type="int" value="20"/>
    <constructor-arg index="2" value="M"/>
    <constructor-arg name="teacher" ref="tea"/>
</bean>
<bean id="tea" class="com.Teacher">
    <constructor-arg name="name" value="李老师" />
    <constructor-arg type="int" value="20" />
</bean>

Student类的属性:

    private String name;
    private int age;
    private char sex;
    private Teacher teacher;

Teacher对象的属性:

    private String name;
    private int age;

main方法:

public class Main {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
       Student stu=(Student)context.getBean("student");
        System.out.println(stu);
    }
}

运行结果:

Student{name='zhangsan', age=20, sex=M, teacher=Teacher{name='李老师', age=20}}

通过set方法注入:

<bean id="student" class="com.Student">
	<property name="name" value="张三" />
	 <property name="age" value="20"/>
	 <property name="sex" value="F"/>
	 <property name="teacher" ref="tea" />
</bean>
<bean id="tea" class="com.Teacher">
	 <property name="name" value="王老师"/>
	 <property name="age" value="35" />
</bean>

main方法保持不变,运行结果:

Student{name='张三', age=20, sex=F, teacher=Teacher{name='王老师', age=35}}

集合类型的装配:
bean对象的属性:

    private Properties properties;
    private List list;
    private Map map;
    private Set set;

属性值的注入:

<bean id="arr" class="com.Array"><!--创建对象-->
    <property name="properties"><!--对第一个属性值注入-->
        <props>
            <prop key="name">张三</prop>
            <prop key="age">20</prop>
        </props>
    </property>

    <property name="list">
        <list>
            <value>zhangsan</value>
            <value>20</value>
            <ref bean="student"/>
        </list>
    </property>

    <property name="map">
        <map>
            <entry key="name" value="zhangsan" />
            <entry key="age" value="20"/>
        </map>
    </property>

    <property name="set">
        <set>
            <value>123</value>
            <value>this is a string</value>
            <ref bean="student"/>
        </set>
    </property>
</bean>

main方法中几种数据结构的遍历:

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Array array = (Array) context.getBean("arr");
        Properties pro = array.getProperties();
        Set<Map.Entry<Object, Object>> set = pro.entrySet();
        for (Map.Entry<Object, Object> s : set) {
            System.out.println(s.getKey() + "--------" + s.getValue());
        }
        System.out.println("-----------------------------------------------------------");
        List list = array.getList();
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println(obj);
        }
        System.out.println("-----------------------------------------------------------");
        Map map = array.getMap();
        Set set1 = map.keySet();
        Iterator iterator1 = set1.iterator();
        while (iterator1.hasNext()) {
            String key = (String) iterator1.next();
            System.out.println(key + "------------" + map.get(key));
        }
        System.out.println("-----------------------------------------------------------");
        Set set2 = array.getSet();
        Iterator iter = set2.iterator();
        while (iter.hasNext())
        {
            System.out.println(iter.next());
        }
    }
}

运行结果:

age--------20
name--------张三
-----------------------------------------------------------
zhangsan
20
Student{name='张三', age=20, sex=F, teacher=Teacher{name='王老师', age=35}}
-----------------------------------------------------------
name------------zhangsan
age------------20
-----------------------------------------------------------
123
this is a string
Student{name='张三', age=20, sex=F, teacher=Teacher{name='王老师', age=35}}

配置文件中bean标签的属性:
scope:可以在配置文件的bean标签中加scope,也可在bean类上面加@Scope(“singleton”)注释。

  • 值为singleton 时,表示单例,容器只会创建一个bean对象,要用到时,从容器中返回该对象即可。
  • 值为prototype时,表示多个,需要时,会创建新的对象。
  • 值为session时,表示作为域为一个会话。比如购物车,如果是原型,则所有用户的商品都添加都一个购物车中了;如果是多例,则每添加一个商品,就会创建一个购物车对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值