Spring4.x❶ 两大核心之IOC

版权声明:本文为博主原创文章,未经博主允许不得转载。Q425680992 https://blog.csdn.net/g425680992/article/details/82495807

更多Spring开发在框架开发


1 Spring?

Spring是开源的轻量级框架,也是一站式框架,即在javaEE三层结构中,每一层都提供了不同的解决技术:层-SpringMVC ; 业务层-ioc ; 持久层-jdbcTemplate 。

Spring技术的两大核心:

  • IOC : 控制反转 , 用spring代替传统的new方法创建对象
  • AOP: 面向切面编程,扩展的功能不通过修改源码来实现。

本文案例先讲述IOC的具体应用

2 IOC案例

关于IOC: ioc是spring的两大核心之一,目的是避免new的方式创建类对象,而类创建过程中对类中属性的赋值则是注入属性 ,IOC有两种方式:

  • 配置文件的方式: 在XML中进行配置关联有关的类以及注入属性
  • 注解的方式: 利用注解的方式直接在类的源代码上进行操作

而一般情况下,我们习惯用配置文件+注解的方式

案例目标: User类和Work类,Test类不再使用new的方式创建对象,而是使用IOC的方式进行创建User类,并且实现注入属性的演示

项目环境: 导入jar包

本文使用Spring4.x版本,导入Spring基本包和日志相关jar包:
这里写图片描述
在测试2.2 注解的方式时,还需要提供spring-aop-4.2.4.RELEASE.jar

2.1 配置文件方法

User类,属性注入需要提供set方法,User类提供字符串属性和对象属性

package edu.kmust.entity;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
 * 实体类
 * @author zhaoyuqiang
 *
 */
public class User {
    private String username ;  //字符串类型
    private Work work ; //对象类型
    public String getUsername() {
        return username;
    }
    //必须提供set方法
    public void setUsername(String username) {
        this.username = username;
    }
    public Work getWork() {
        return work;
    }
    public void setWork(Work work) {
        this.work = work;
    }   
}

Work类

package edu.kmust.entity;
import org.springframework.stereotype.Service;
public class Work {
    /**
     * 添加用户
     */
    public void add() {
        System.out.println("添加用户的操作......");
    }
}

Spring核心配置文件,名称建议用applicationContext.xml,位置建议src下。

<?xml version="1.0" encoding="UTF-8"?>
<!--导入schema约束,在
   spring-framework-4.2.4.RELEASE-dist/spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html目录
   下的xsd-configuration.html里面有该约束-->
<!-- 配置文件的方式引入的约束 -->   
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 1.创建对象
            bean:
               id : 创建对象的名字(可以任意起名,不能包含下划线等特殊符号)
               class: 创建对象所在类的全路径
               scope: 配置单例或多例
                     singleton: 单例,通过IOC获取的对象都是同一个对象
                     prototype:多例,通过IOC获取的对象是不同的对象,一般用在struts2的action中
      -->
    <bean id="work" class="edu.kmust.entity.Work" ></bean>
    <bean id="user" class="edu.kmust.entity.User" scope="singleton">
       <!-- 2.1 属性注入: 字符串 
              name: 类属性名字
              value: 属性值
       -->
        <property name="username" value="黄思"></property>
        <!--2.2属性注入:对象
                    需要创建 注入对象work
                 name: 类的对象属性的名字
                 ref:  注入对象的bean标签的id值            
        -->
        <property name="work" ref="work"></property>
    </bean>  
</beans>

测试类,User类通过ioc创建,并且测试了属性有没有注入

@Test
    public void test01() {
        /*
         * 1.加载spring配置文件,根据配置文件创建
         *     org.springframework.context.ApplicationContext对象
         * 2. 用IOC的方式获取对象     
         */
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user");
        System.out.println(user.getUsername());
        user.getWork().add(); //不用new的方式就能调用其他类的方法,通常用在service调用dao中
    }

2.2 注解方法

User,使用注解方法创建对象,注入属性,不需要提供set方法

package edu.kmust.entity;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
 * 实体类
 * @author zhaoyuqiang
 *
 */
/*
 * 1.创建对象的注解Component/Controller/Service/Repository四个都可以
 *     类似于配置文件方式中的<bean:id="user" class="">
 *     value=“user”可以写成"user"
 *     Scope配置单/多例
 */
@Component(value="user") 
@Scope(value="prototype") 
public class User {
    /*
     * 2. 属性注入的注解
     *     @Autowired
     *     @Resource(name="注入对象的注解value值(另一个对象的Service注解的value值)")
     */
    @Value("黄思")
    private String username ;  //字符串类型
    @Resource(name="work")
    private Work work ; //对象类型
    public String getUsername() {
        return username;
    }
    public Work getWork() {
        return work;
    }
}

Work

package edu.kmust.entity;
import org.springframework.stereotype.Service;
/**
 * 注入对象
 * @author zhaoyuqiang
 *
 */
@Service(value="work")
public class Work {
    /**
     * 添加用户
     */
    public void add() {
        System.out.println("添加用户的操作......");
    }
}

配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 在beans约束的基础上,再导入context约束 -->
<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 http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
    <!-- 开启注解扫描
            到包里面扫描类、方法、属性上是否有注解
            context:component-scan:
                base-package: 要扫描的包的名字,如果有多个包,用逗号隔开,或者取共同部分,如edu.kmust
     -->
     <context:component-scan base-package="edu.kmust.entity"></context:component-scan>
</beans>

2.3 配置文件与注解一起使用

配置文件用来创建对象,注解方法用来注入属性

User

package edu.kmust.entity;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 实体类
 * @author zhaoyuqiang
 *
 */
public class User {
    @Value("黄思")
    private String username ;  //字符串类型
    @Resource(name="work")
    private Work work ; //对象类型
    public String getUsername() {
        return username;
    }
    public Work getWork() {
        return work;
    }
}

Work

package edu.kmust.entity;
import org.springframework.stereotype.Service;
/**
 * 注入对象
 * @author zhaoyuqiang
 *
 */
public class Work {
    /**
     * 添加用户
     */
    public void add() {
        System.out.println("添加用户的操作......");
    }
}

配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--beans+context约束-->
<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 http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
    <!--创建对象  -->
    <bean id="work" class="edu.kmust.entity.Work" scope="singleton"></bean>
    <bean id="user" class="edu.kmust.entity.User"></bean>
    <!-- 开启注解扫描
            到包里面扫描类、方法、属性上是否有注解
            context:component-scan:
                base-package: 要扫描的包的名字,如果有多个包,用逗号隔开,或者取共同部分,如edu.kmust
     -->
     <context:component-scan base-package="edu.kmust.entity"></context:component-scan>
     <!-- 只扫描属性 -->
     <!--<context:annotation-config></context:annotation-config>-->
</beans>

3 代码案例可参考:

https://download.csdn.net/download/g425680992/10651876

猜你喜欢

转载自blog.csdn.net/g425680992/article/details/82495807
今日推荐