ioc实现

目录

1、入门、

1.、1编写相关类

1、2、在applicationContext.xml配置bean,类=对象的创建交给spring实现

1.3、测试

2.属性注入

2.1


IOC:Inversion of Control 控制反转. 指的是 对象的创建权反转(交给)给 Spring.
作用是实现了程序的解耦合.

1、入门、

1.、1编写相关类

public interface UserDao {
public void sayHello();
}
public class UserDaoImpl implements UserDao {
@Override
public void sayHello() {
System.out.println("Hello Spring...");
}
}

1、2、在applicationContext.xml配置bean,类=对象的创建交给spring实现

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.2.xsd 
     http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
      
      <bean id="userDao" class="cn.itcast.spring.demo1.UserDaoImpl"></bean>
 

</beans>

1.3、测试

@Test
// Spring 的方式:
public void demo2(){
// 创建 Spring 的工厂类:
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("applicationContext.xml");
// 通过工厂解析 XML 获取 Bean 的实例.
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.sayHello();
}

2.属性注入

2.1

【构造方法的方式注入属性】
<!-- 第一种:构造方法的方式 -->
<bean id="car" class="cn.itcast.spring.demo4.Car">
<constructor-arg name="name" value="保时捷"/>
<constructor-arg name="price" value="1000000"/>
</bean>

【set 方法的方式注入属性】
<!-- 第二种:set 方法的方式 -->
<bean id="car2" class="cn.itcast.spring.demo4.Car2">
<property name="name" value="奇瑞 QQ"/>
<property name="price" value="40000"/>
</bean>

【对象类型的注入】
<!-- 注入对象类型的属性 -->
<bean id="person" class="cn.itcast.spring.demo4.Person">
<property name="name" value="会希"/>
<!-- ref 属性:引用另一个 bean 的 id 或 name -->
<property name="car2" ref="car2"/>
</bean>

【复杂类型的注入】
<bean id="collectionBean" class="cn.itcast.spring.demo5.CollectionBean">
<!-- 数组类型的属性 -->
<property name="arrs">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list> 
</property>
<!-- 注入 List 集合的数据 -->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
<!-- 注入 Map 集合 -->
<property name="map">
<map>
<entry key="aaa" value="111"/>
<entry key="bbb" value="222"/>
<entry key="ccc" value="333"/>
</map>
</property>
<!-- Properties 的注入 -->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值