spring(一) IOC 控制反转 、DI 依赖注入

IOC 控制反转:创建对象的方式  变成了由Spring来主导

IOC底层原理:对象工厂

1、导入jar包:4个核心jar和1个依赖jar

spring-beans-4.3.9.RELEASE.jar

spring-context-4.3.9.RELEASE.jar

spring-core-4.3.9.RELEASE.jar

spring-expression-4.3.9.RELEASE.jar

com.springsource.org.apache.commons.logging-1.1.1.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">
<bean id="foo" class="x.y.Foo">
<meta key="cacheName" value="foo"/>
<property name="name" value="Rick"/>
</bean>
</beans>

3、在xml中beans标签里注册类

Class属性 一定为全路径 (因为ioc的底层实现是通过反射技术)

id:通过id来 获取本类对象,一般为类名(首字母小写)

  <bean id="studentId" class="com.spring.Student">
</bean>

4、获取对象方式

     //初始化spring 容器
ApplicationContext con = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过 getBean() 获取对象
Student stu = (Student)con.getBean("studentId");
stu.study();

scope

scope= "prototype"多实例  。 scope= " singleton "单例

默认为单例模式 singleton

lazy-init

lazy-init = "true";  懒加载:使用时再创建对象

lazy-init = "false"; 默认为false。容器初始化时创建对象

DI  依赖注入

概念 :启动Spring容器 加载bean配置的时候,完成对变量的赋值行为

一:使用xml表配置注入:

1、有参构造注入

     public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
     <bean id="studentId" class="com.spring.Student" scope="singleton" lazy-init="true">

         <constructor-arg name="name" value="刘杨哥哥"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg> </bean>

2、Set方法注入

     public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
    <bean id="studentId" class="com.spring.Student" scope="singleton" lazy-init="true">

         <property name="name" value="liuyang"></property>
<property name="age" value="20"></property> </bean>

3、属性为对象的注入方式

 package com.spring_ioc1;

 public class Clazz {

     private Student stu;

     public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
} public void clzzbegin(){ // clazz依赖学生 System.out.println("=== 开始上课 ===");
stu.study();
} }
     <!-- 依赖注入: clazz依赖学生: 所以需注入学生 -->
<bean id="clazzId" class="com.spring.Clazz" scope="singleton" lazy-init="true">
<property name="stu" ref="studentId"></property>
</bean>

4、特殊类型的对象的注入

  数组、List集合、Map集合

 package com.spring_ioc1;

 import java.util.ArrayList;
import java.util.Map; public class Property { private String arr[];
private ArrayList<String> list;
private Map map; public String[] getArr() {
return arr;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public ArrayList<String> getList() {
return list;
}
public void setList(ArrayList<String> list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Property() {
super();
} }
     <bean id="propertyId" class="com.spring.Property">
<property name="arr">
<list>
<value>arr刘杨</value>
<value>arr刘超</value>
<value>arr刘聪</value>
</list>
</property>
<property name="list">
<list>
<value>list刘杨</value>
<value>list刘超</value>
<value>list刘聪</value>
</list>
</property>
<property name="map">
<map>
<entry key="1" value="map刘杨"></entry>
<entry key="2" value="map刘超"></entry>
<entry key="3" value="map刘聪"></entry>
</map>
</property>
</bean>

二:使用注解注入:

 <?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: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="com.spring_ioc2"></context:component-scan> </beans>
 package com.spring_ioc2;

 import javax.annotation.Resource;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; @Component(value="clazzId") //相当于 <bean id="clazzId" class="com.spring.clazz"></bean>
//@Scope(value="prototype")
@Lazy
public class Clazz { @Value("gs02班") // <property name="" value="">
private String name; //@Autowired //根据student 的类型寻找 // <property name="" ref="">
@Resource(name="student") //指定别名扫描寻找
private Student stu; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
} public Clazz(){
System.out.println("=== clazz初始化完成 ===");
} public void clzzBegin(){ System.out.println("=== 开始上课喽 ===");
//stu.study();
} }
 package com.spring_ioc2;

 import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("student")
public class Student { @Value("刘杨")
private String name;
private int age; public Student() {
System.out.println("=== student初始化完成 ===");
} public Student(String name) {
this.name = name;
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} public void study(){
System.out.println("Student study 正在努力学习!!!");
}
}

三: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"
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 --> <!-- 自动扫描 配置 扫描有限注解 :xml表与注解混合使用注入-->
<context:annotation-config></context:annotation-config> <!--注册类 -->
<bean id="clazzId" class="com.spring_ioc3.Clazz"></bean>
<bean id="student" class="com.spring_ioc3.Student"></bean> </beans>
 package com.spring_ioc3;

 import javax.annotation.Resource;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; public class Clazz { @Value("gs02班") // <property name="" value="">
private String name; //@Autowired //根据student 的类型寻找 // <property name="" ref="">
@Resource(name="student") //指定别名扫描寻找
private Student stu; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
} public Clazz(){
System.out.println("=== clazz初始化完成 ===");
} public void clzzBegin(){ System.out.println("=== 开始上课喽 ===");
//stu.study();
} }
 package com.spring_ioc3;

 import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; public class Student { @Value("刘杨")
private String name;
private int age; public Student() {
System.out.println("=== student初始化完成 ===");
} public Student(String name) {
this.name = name;
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} public void study(){
System.out.println("Student study 正在努力学习!!!");
}
}
上一篇:将 vue 挂在 window 对象上,实现能调用 elementUI 的组件


下一篇:201521123010 《Java程序设计》第5周学习总结