复习整理的DAY30_Spring IOC(xml配置与注解)

Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架。

IOC是什么?

IOC是Inversion of Control的缩写,翻译为控制反转。

什么叫控制反转?

要知道,原本我们创建对象,都是主动去new对象,也就是,需要什么,才去创建什么。

而现在,控制反转,指的就是我们将对象在外部容器中创建成功,用的时候,再取出来自己用。这个创建对象的过程不是我们主动的,我们只是被动的接受容器创建好的对象。

在Spring中IOC是在Spring容器中将对象创建成功,再通过set赋值的方法将容器中对象赋给我们自己定义的对象。

而这个具体的将容器中对象赋给我们定义对象的实现过程,称为DI(Dependency injection),依赖注入。

依赖什么?注入什么?

依赖Spring容器,注入Spring容器new的对象。

AOP是什么?

AOP是Aspect Oriented Programming的缩写,翻译为面向切面。

什么是面向切面?

可以说是对传统面向对象编程的一个补充,是一种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想。我们将切入到指定类指定方法的代码片段叫做切面,将切入到哪些类、哪些方法(位置)叫做切点。

具体实例(使用IDEA):

一些jar包:

spring-lib.rar-Java文档类资源-CSDN下载

第一步,将这些jar包作为一个lib文件夹放在项目下,记得在Project Sructure配置

第二步,在src下创建applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.1.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.1.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util-4.1.xsd">


</beans>

第三步,创建spring包,在该包里创建entity实体类的包,创建Student类,创建Banji类

public class Banji {
    private Integer id;
    private String name;

    public Banji() {
    }

    public Banji(Integer id, String name) {
        System.out.println("Banji.Banji");
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Banji{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String gender;
    private Banji banji;

    public Student() {
        System.out.println("Student.Student");
    }

    public Student(Integer id, String name, Integer age, String gender, Banji banji) {
        System.out.println("Student.Student");
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.banji = banji;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Banji getBanji() {
        return banji;
    }

    public void setBanji(Banji banji) {
        this.banji = banji;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", banji=" + banji +
                '}';
    }
}

(使用applicationContext.xml文件配置,不使用注解)

第四步,创建与entity同级的dao包,创建接口IStudentDao,然后在dao包内创建接口实现类的包impl,在该包内创建StudentDaoImpl接口实现类

IStudentDao接口

public interface IStudentDao {
    void selectAll();
}

StudentDaoImpl实现类

public class StudentDaoImpl implements IStudentDao {
    @Override
    public void selectAll() {
        // 测试所用
        System.out.println("StudentDaoImpl.selectAll");
    }
}

第五步,创建service包,创建接口IStudentService,然后在service包内创建接口实现类的包impl,在该包内创建StudentServiceImpl接口实现类

IStudentService接口

public interface IStudentService {
    void selectAll();
}

StudentServiceImpl实现类

public class StudentServiceImpl implements IStudentService {

    private IStudentDao studentDao = new StudentDaoImpl();

    public IStudentDao getStudentDao() {
        return studentDao;
    }

    public void setStudentDao(IStudentDao studentDao) {
        // 测试所用
        System.out.println("StudentServiceImpl.setStudentDao");
        this.studentDao = studentDao;
    }

    @Override
    public void selectAll() {
        // 测试所用
        System.out.println("StudentServiceImpl.selectAll");
        studentDao.selectAll();
    }
}

第六步,创建与entity同级的controller包,在包内创建StudentController类

public class StudentController {
    private IStudentService service;

    public void setService(IStudentService service) {
        // 测试所用
        System.out.println("StudentController.setService");
        this.service = service;
    }

    public void selectAll() {
        // 测试所用
        System.out.println("StudentController.selectAll");
        service.selectAll();
    }
}

第七步,在applicationContext.xml文件中配置:

(1)对实体类:

配置:

	<!-- IOC容器配置,要创建的所有对象都配置到这里,交给Spring容器来管理 -->	
    <bean scope="prototype" name="banji" class="spring.entity.Banji">
		<constructor-arg name="id" value="1"></constructor-arg>
		<constructor-arg name="name" value="Java2100"></constructor-arg>
	</bean>
    <bean scope="prototype" name="student" class="spring.entity.Student">
		<constructor-arg name="id" value="1"></constructor-arg>
		<constructor-arg name="name" value="zhangsan"/>
		<constructor-arg name="age" value="23"/>
		<constructor-arg name="gender" value="男"/>
		<constructor-arg name="banji" ref="banji"></constructor-arg>
	</bean>

建了一个包test,包内建了一个类Demo,在类中,

    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }

运行一下看看

 说明:

每次容器启动的时候会创建容器(applicationContext.xml)中配置的所有的bean对象。

Spring容器用bean创建了对象,constructor-arg标签表示通过含参构造,

scope的prototype表示多例的,也就是说我们每一次调用Spring容器创建的对象是不一样的,

如果是scope的singleton,表示单例的,每一次调用的Spring容器创建的对象是相同的(可以尝试创建两个对象==比较输出看看)

(2)对DAO-Service-Controller层

	<bean name="studentDao" class="spring.dao.impl.StudentDaoImpl"></bean>
	<bean name="studentService" class="spring.service.impl.StudentServiceImpl">
		<property name="studentDao" ref="studentDao"></property>
	</bean>
	<bean name="studentController" class="spring.controller.StudentController">
		<property name="service" ref="studentService"></property>
	</bean>
    @Test
    public void test2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentController controller = (StudentController) context.getBean("studentController");
        controller.selectAll();
    }

 打印:

说明:Spring容器通过bean创建一个对象,通过property将关联的对象set赋值给对应的属性,做完这些之后,才是自己获得了Spring容器中的对象来调用函数。

注意,用property标签时需要写对应的set函数。

 使用注解:

从第四步开始稍微不同(以Banji为例):

在dao包内创建IBanjiDao接口,在dao包的实现包impl内创建BanjiDaoImpl实现类,

使用注解@Repository

public interface IBanjiDao {
    void select();
}
@Repository
public class BanjiDaoImpl implements IBanjiDao {
    @Override
    public void select() {
        System.out.println("BanjiDaoImpl.select");
    }
}

说明:@Repository注解可以标记在任何的类上,用来表明该类是用来执行与数据库相关的操作(即dao对象),并支持自动处理数据库操作产生的异常。

使用注解@Repository相当于在applicationContext.xml中

<bean name="banjiDaoImpl" class="spring.dao.impl.BanjiDaoImpl"></bean>

也就是说会自动将实现类的名字首字母小写作为bean的名字,类型也会自动设置为该类型。

如果想要改名字,可以@Repository(name="banjiDao")或者@Repository("banjiDao")

第五步,在service包内创建接口IBanjiService,在该包的实现包impl内创建实现类BanjiServiceImpl

使用注解@Service。

public interface IBanjiService {
    void selectAll();
}
@Service
public class BanjiServiceImpl implements IBanjiService {

    @Autowired
    private IBanjiDao banjiDao;
    @Override
    public void selectAll() {
        System.out.println("BanjiServiceImpl.selectAll");
        banjiDao.select();
    }
}

说明:

@Service,@Controller,@Repository是针对不同的使用场景所采取的特定功能化的注解组件,比如在这里,我们是模仿了service层,就需要用@Service注解。

相当于在applicationContext.xml中

<bean name="banjiServiceImpl" class="spring.service.impl.BanjiServiceImpl"></bean>

改名的话一样,@Service("banjiService")或@Service(name="banjiService"),@Controller同上

@Autowired注解表示自动装配,什么意思呢?就是说,通过参数的数据类型自动装配,如果一个bean的数据类型和另外一个bean的property属性的数据类型兼容,就自动装配,如果有多个匹配的类型,就会报错。

也可以用@Resource,这个注解是通过名称装配的,如果一个bean的name 和另外一个bean的 property的name 相同,就自动装配,相当于<property name="banjiDao" ref="banjiDao"/>,但不需要写set函数,@Resource也可以自己命名,比如@Resource(name="banjiDaoImpl"),这样就相当于<property name="banjiDaoImpl" ref="banjiDaoImpl"/>

第六步,在controller包内创建BanjiController类,使用注解

@Controller
public class BanjiController {
    @Autowired
    private IBanjiService banjiService;
    public void selectAll() {
        System.out.println("BanjiController.selectAll");
        banjiService.selectAll();
    }
}

说明:加了注解@Controller相当于

<bean name="banjiController" class="spring.controller.BanjiController"></bean>

第七步,在applicationContext.xml中要加入扫描

	<context:component-scan base-package="spring"></context:component-scan>

在Demo中,测试一下,

    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BanjiController controller = (BanjiController) context.getBean("banjiController");
        controller.selectAll();;
    }

打印:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值