IOC注入和DI

好处 :

1.不依赖于应用服务器.
2.IOC(控制反转)技术实现组件控制. 通过DI(依赖注入)技术提高了组件之间的解耦.
3.通过AOP(面向切面编程)技术实现安全、事务、日志功能的统一管理,提高复用.
4.Spring框架可以几乎与其他任何框架进行良好的整合使用.
5.Spring所有项目之间不相互依赖.

IOC : 控制反转. 本质: 将手动创建对象的工作,交给Spring容器实现完成.
1.在Spring容器中可以配置创建实例对象.无论带参,无参构造实例都是可以实现的.
Spring.xml容器中配置:

2.如何通过注解的用法代替Spring配置文件中的写法.实现IOC控制.
2.1: 首先需要在实体类中: 采用注解., 注解支持的组件: context组件
@Component , @Controller , @Service , @Repository
2.2: 基于spring容器配置注解的扫描和支持.
<context:component-scan base-package=“父包”>
DI : 依赖注入.含义 : 指的是针对实体类中: 属性成员赋值操作.
1.针对实体中的属性实现注入: 采用容器配置写法实现.
1.1:类中依赖的属性: 必须get/set封装.
1.2:进行注入:
2.属性的注入: 也可采用注解的写法实现.
//无需提供属性的get/set封装方法实现注入.
@Resource(name=”对象名”) //根据对象名称进行spring容器查找,并注入.
Private BanJi bj1;
@Autowired //根据类型进行spring容器查找,找到指定类型对象,直接注入.
Private BanJi bj1;

IOC在applicationContext.xml中手动注入

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <!--  1. 手动调用无参构造实例化:   -->
    <bean id="s1" name="s1" class="com.zhongruan.bean.Student"></bean>
    <!--  2. 手动调用有参构造实例化:    -->
    <bean id="s2" name="s2" class="com.zhongruan.bean.Student">
        <constructor-arg index="0" value="001"></constructor-arg>
        <constructor-arg index="1" value="002"></constructor-arg>
    </bean>
    <!--   3. 手动实现依赖注入  -->
    <bean id="t1"  name="t1" class="com.zhongruan.bean.Teacher">
        <property name="tno" value="1"></property>
        <property name="tname" value="DaJiang"></property>
        <property name="subs">
            <list>
                <value>C语言</value>
                <value>Java</value>
                <value>Html</value>
            </list>
        </property>
        <property name="stu1" ref="s2"></property>
    </bean>
</beans>

IOC自动扫描注入

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!--  引入context组件
            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:tx="http://www.springframework.org/schema/tx"
		xmlns:context="http://www.springframework.org/schema/context"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
        使用IOC的注解扫描与支持:  base-package - 父路径
       -->
    <context:component-scan base-package="com.zhongruan.bean"></context:component-scan>
</beans>

IOC分层

<beans>
    <!--1.IOC实例化分层对象-->
    <bean id="controller1" name="controller1" class="com.zhongruan.controller.UserController1">
        <property name="userService" ref="service1"></property>
    </bean>
    <bean id="service1" name="service1" class="com.zhongruan.service.IUserService">
        <property name="userDao" ref="dao1"></property>
    </bean>
    <bean id="dao1" name="dao1" class="com.zhongruan.dao.UserDao"></bean>
</beans>

配置数据源连接数据库 将数据源注入道JDBCTemplate

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!--引入Context组件-->
    <!--使用IOC的注解扫描与支持  base-packge是父包 -->
    <context:component-scan base-package="com.zhongruan"></context:component-scan>
    <!--2.配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db_gm?useSSL=true&amp;characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    <!--将dataSource注入到JDBCTemplate-->
    <bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值