Spring_学习02

一. HelloSpring

1. 创建Maven项目,导入jar包

spring-webmvc

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>5.3.12</version>
</dependency>

2. 创建实体类Hello

import lombok.Data;

@Data
public class Hello {
    private String name;
}

3. 创建spring的配置文件ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

<!--
    使用Spring来创建对象, 在Spring中统称为Bean
    bean 标签中的属性id代表要实例化的对象的名, class代表对象
    property 为赋值 name属性指明属性名, value 指明赋值
-->
    <bean id="hello01" class="com.ctgu.pojo.Hello">
        <property name="name" value="WengBoLiang"/>
    </bean>
</beans>

4. 创建测试类

@Test
public void hello(){
	//获取Spring的上下文对象
	ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
	//将所有对象放在Spring中管理, 要使用直接取出即可
	Hello hello01 = (Hello) context.getBean("hello01");
	System.out.println(hello01);
}

5. 结果及分析

Spring_学习02
示例中全程没有new Hello(), 那么结果中输出的hello01是谁创建的?

  • 是由spring创建的
    Hello 对象的属性是怎么设置的 ?
  • 是由spring容器设置的

这个过程就成为IOC(控制反转):

  • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
  • 程序本身不创建对象 , 而变成被动的接收对象 .

IOC是一种编程思想,由主动的编程变成被动的接收

6. 修改Spring_学习01中的例子

新增spring配置文件, 添加bean

<bean id="mysqlImpl" class="com.ctgu.dao.UserDaoMysqlImpl"/>
<bean id="sqlserverImpl" class="com.ctgu.dao.UserDaoSqlserverImpl"/>

<bean id="userService" class="com.ctgu.service.UserServiceImpl">
	<property name="userDao" ref="sqlserverImpl"/>
</bean>

测试类:

@Test
public void hello(){
	//获取Spring的上下文对象
	ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
	//将所有对象放在Spring中管理, 要使用直接取出即可

	UserService userService = (UserService) context.getBean("userService");
	userService.getUser();
}

结果:
Spring_学习02

到了现在, 我们无需在程序中改动任何代码就可以实现不同的实现类, 只需要在xml配置文件中进行简单的修改即可:
Spring_学习02

二. IOC创建对象方式

1. 通过无参构造方法来创建

  1. UserWithNoParameters
import lombok.Data;

@Data
public class UserWithNoParameters {
    private String name;
}

  1. beans.xml
<!--    无参构造 类的bean 的配置 -->
<bean id="userNoP" class="com.ctgu.pojo.UserWithNoParameters">
	<property name="name" value="翁博梁nop1"/>
</bean>
  1. 测试类
@Test
public void test(){
	ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
	UserWithNoParameters userNoP = (UserWithNoParameters) context.getBean("userNoP");
	System.out.println(userNoP);
}
  1. 结果
    Spring_学习02

2. 通过有参构造方法来创建

  1. UserWithParameters
@Data
public class UserWithParameters {
    private String name;

    public UserWithParameters(String name){
        this.name=name;
    }
}
  1. beans.xml
<!--    有参构造 类的bean 的配置beans.xml
    方法一 直接命名法
    <bean id="userP" class="com.ctgu.pojo.UserWithParameters">
        <constructor-arg name="name" value="翁博梁"/>
    </bean>
    方法二 参数类型法
    <bean id="userP" class="com.ctgu.pojo.UserWithParameters">
        <constructor-arg type="java.lang.String" value="翁博梁"/>
    </bean>
-->
<!--    方法三 索引法-->
    <bean id="userP" class="com.ctgu.pojo.UserWithParameters">
        <constructor-arg index="0" value="翁博梁p1"/>
    </bean>
  1. 测试类
@Test
public void test(){
	ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
	UserWithParameters userP = (UserWithParameters) context.getBean("userP");
	System.out.println(userP);
}
  1. 结果
    Spring_学习02

在配置文件加载的时候。其中管理的对象都已经初始化了

上一篇:Spring快速入门


下一篇:centos 7 下载安装& 配置 zookeeper