Spring学习笔记01(IOC)

IOC和DI

IOC(控制反转 Inversion of control)很抽象的概念,是一种设计思想,在spring中可以先简单的理解为将对象的创建,赋值等交给spring工厂进行.
DI(依赖注入 dependency injection)控制反转有一种方式叫做 依赖注入,A如果依赖B,A此时并不需要创建B,也不用关心B如何创建的,spring会在适合的时候将B创建出来注入到A中。

简单了解一下工厂模式

下面写了一个小demo
在这里插入图片描述

a1.properties 是一个文本文件,里面通过键值的方式放入我们需要的数据
key值自己定义,value值是要创建的对象的类
本例子里面放的就是:   userDao = com.
其中factory包下的BeanFactory就是要写的一个工厂类,直接先看代码然后解释

public class BeanFactory{
	//先创建Properties对象
	private static Properties pro = new Properties();
	//在静态代码块里面读取a1.properties里面的内容
	static {
		//静态代码块中的代码要进行try catch处理
		try {
			//将properties文件里面的内容读取进入一个流中
			InputStream in = BeanFactory.class.getResourceAsStream("/a1.properties");
			//用properties对象从输入流中读取
			pro.load(in);
		} catch (Exception e){
			e.printStackTrace();
		}
		
		//创建对象的方法
		public static Object getBean(String key){
			//通过反射机制创建对象
			Class clz = Class.forName(pro.getProperty(key));
			return clz.newInstance();
		}
	}
}
//创建一个测试类
public class TestBeanFactory{
	public static void main(String[] args) {
		//调用工厂类创建对象
		UserDao dao=(UserDao)BeanFactory.getBean("userDao");
		//调用dao中的方法
		dao.insert();
	}
}

接下来就看一下 Spring怎么创建对象的吧

1.添加jar包 为了省事,我将spring的初始学习的jar全部导入了
在这里插入图片描述
2.需要一个xml配置文件,本例名字是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">
        <!--在这里写上要创建的对象
        1d:这个bean标签的唯一标识,自己起的名字不要重复就好
        class:要创建对象的类全路径
        -->
    <bean id="userService" class="com.service.impl.UserServiceImpl"></bean>
</beans>

3.测试一下

public class TestSpring{
	public static void main(String[] args) {
		//创建spring的工厂的对象
		ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		//参数就是配置文件里面 对应bean的id值
		UserService us=(UserService)ac.getBean("userService");
	}
}

属性注入

这个直接上代码了
Person类

	private int id;
    private String name;
    private List<String> list;
    private Properties pro;
	//set get 方法

配置文件:

<bean id="person" class="com.entity.Person">
        <!--给基本类型赋值  int-->
        <property name="id" value="101"></property>
        <!--给String类型赋值-->
        <property name="name" value="张三"></property>
        <!--给list集合类型赋值-->
        <property name="list">
            <list>
                <value>哈哈</value>
                <value>呵呵</value>
                <value>嘿嘿</value>
            </list>
        </property>
        <!--给properties类型赋值-->
        <property name="pro">
            <props>
                <prop key="a1">哈哈</prop>
                <prop key="a2">呵呵</prop>
                <prop key="a3">嘿嘿</prop>
            </props>
        </property>
    </bean>


构造注入

User类里面要加一个有参构造方法

private String username;
public User(String name){
	this.name=name;
}

配置文件:

<bean id="user" class="com.entity.User">
		<!--name属性是User类里面的属性名称
		也可以是index 代表有参构造中方法的顺序
		还有type等....
		-->
        <constructor-arg name="username" value="张三"></constructor-arg>
 </bean>

本次教程到此结束啦

猜你喜欢

转载自blog.csdn.net/cheng6202/article/details/84324722