Spring核心(Core模块):IOC
1. IOC容器初理解
解决对象的创建以及对象之间的依赖关系。
主要学习如何得到IOC容器,IOC容器如何创建对象,如何解决对象之间的依赖关系,以及IOC的具体细节
Spring的核心思想之一就是IOC(控制反转)
控制反转的意思:对象的创建交给外部容器完成
- Spring使用控制反转来实现对象不用再程序中写死
- 控制反转将对象由我们创建交给容器创建
依赖注入:解决对象之间的依赖关系
- Spring使用依赖注入来实现对象之间的依赖关系
2. 得到IOC容器
IOC容器可以分为两种接口
- Bean工厂,BeanFactory———功能简单
- 应用上下文,ApplicationContext———功能强大
2.1 通过Resoure获取Bean工厂
- 加载Spring的资源文件
- 获取IOC容器
ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");//在执行getbean时,user对象已经通过无参构造创建成功了。User user = (User) context.getBean("user");
2.2 直接通过ClassPathXmlApplicationContext对象来获取
// 加载配置文件ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");// IOC容器创建对象XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(classPathResource);
3. IOC容器创建对象及属性注入
在Spring中总体可以通过三种方式来创建对象

3.1 XML配置方式
- 首先我们需要一个类User.class
public class User {private String name;private int age;public User() {System.out.println("我是User,被无参构造器创建了。。。");}public User(String name, int age) {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 show() {System.out.println("name" + name);}}
- 以前我们都是通过new User() 的方式来创建对象
现在通过IOC容器来创建对象,我们只需要在applicationContext.xml核心配置文件中配置对应的信息即可
User user = new User();
- 通过IOC容器来获取创建的对象
<!--使用bean节点来创建对象id属性标识对象class属性代表要创建的对象的全类名--><bean id="hello" class="com.xiaojian.pojo.User"></bean>
- 从控制台打印的结果我们可以看出来,IOC是通过无参构造函数的方式进行创建对象,IOC一般有三种创建对象的方式

1. 有参构造器创建对象
- 首先Javabean需要提供带参数的构造函数
public User(String name, int age) {this.name = name;this.age = age;}
- 关键点,配置applicationContext.xml文件,三种方式进行属性的注入,并且可以混合使用
有参构造器我们通过<constructor-arg>标签来完成配置。
<!-- 构造器注入通过有参的构造方法 通过参数index的下标进行设置 --><bean id="userT" class="com.xiaojian.pojo.UserT"><constructor-arg index="0" value="张三"/></bean><!--通过有参的构造方法 通过参数的名字 --><bean id="userT" class="com.xiaojian.pojo.UserT"><constructor-arg name="name" value="张三"/></bean><!--通过有参的构造方法 通过参数的类型 --><bean id="userT" class="com.xiaojian.pojo.UserT"><constructor-arg type="java.lang.String" value="张三"/></bean>
- 获取结果
@Testpublic void test2() {ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");//在执行getbean时,user对象已经通过无参构造创建成功了。UserT user = (UserT) context.getBean("userT");System.out.println(user);// 容器创建对象和我们创建对象的方式一样UserT userT = new UserT();System.out.println(userT);}

2. 工厂创建对象
1. 静态工厂方法
- 首先使用一个工厂的静态方法返回一个对象
public class BeanFactory {public static User getBean(){return new User();}}
- 配置文件中使用工厂的静态方法返回对象
<bean id="user3" class="com.xiaojian.pojo.BeanFactory" factory-method="getBean"></bean>
- 测试结果
@Testpublic void test3() {ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");User user = (User) context.getBean("user3");System.out.println(user);}// 我是User,被无参构造器创建了。。。// User{name='null', age=0}
2.非静态工厂方法
- 和静态工厂相同使用工厂的非静态方法返回一个对象
public class BeanFactory {public User getBean(){return new User();}}
- 配置文件中使用工厂的非静态方法返回对象
<!--首先创建工厂对象--><bean id="factory" class="com.xiaojian.pojo.BeanFactory"/><!--指定工厂对象和工厂方法--><bean id="user" class="com.xiaojian.pojo.User" factory-bean="factory" factory-method="getBean">
- 测试结果相同
3. xml方式依赖注入

构造器注入在前面已经说过,下面主要介绍属性注入
1. 常量注入
<!--2.常量注入--><bean id="student" class="com.xiaojian.pojo.Student"><property name="name" value="李四"/></bean>
测试类
@Testpublic void test4(){ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");Student student = (Student) context.getBean("student");System.out.println(student.getName());}
2. bean注入
<!--3.bean注入--><bean id="addr" class="com.xiaojian.pojo.Address"><property name="address" value="上海"/></bean><bean id="student1" class="com.xiaojian.pojo.Student"><property name="name" value="王五"/><property name="address" ref="addr"/></bean>
测试类
@Testpublic void test4(){ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");Student student = (Student) context.getBean("student1");System.out.println(student.getAddress().getAddress());System.out.println(student.getName());}
3. 数组注入
<!--4.数组注入--><bean id="student2" class="com.xiaojian.pojo.Student"><property name="books"><array ><value>红楼梦</value><value>三国演绎</value><value>西游记</value><value>水浒传</value></array></property></bean>
测试类
@Testpublic void test4(){ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");Student student = (Student) context.getBean("student2");System.out.println(student.getAddress().getAddress());System.out.println(student.getName());student.show();}
4. List注入
<property name="hobbys"><list><value>听歌</value><value>打球</value></list></property>
5. map注入
<property name="card"><map><entry key="邮政" value="34234234234312"/><entry key="招商" value="4323465474523"/></map></property>
6. set注入
<property name="games"><set><value>LOL</value><value>BOB</value><value>COC</value></set></property>
7. Null注入
<property name="wife"><null/></property>
8.properties注入
<property name="info"><props><prop key="学号">20190604</prop><prop key="性别">男</prop><prop key="姓名">小明</prop></props></property>
所有属性的测试类
@Testpublic void test4(){ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");Student student = (Student) context.getBean("student4");// System.out.println(student.getAddress().getAddress());//System.out.println(student.getName());student.show();}
测试结果
name=王五,address上海,books=<<红楼梦>><<三国演绎>><<西游记>><<水浒传>>爱好:[听歌, 打球]card:{邮政=34234234234312, 招商=4323465474523}games:[LOL, BOB, COC]wife:nullinfo:{学号=20190604, 性别=男, 姓名=小明}
9. P命名空间和C命名空间的注入
- p命名空间注入需在头文件中导入约束
//导入约束 xmlns:p="http://www.springframework.org/schema/p"<!--p的命名空间 --><bean id="user1" class="com.xiaojian.pojo.User"p:age="24" p:name="小贱"/>
- c命名空间也要在头文件添加约束
//导入约束 xmlns:c="http://www.springframework.org/schema/c"<!--c的命名空间 --><bean id="user2" class="com.xiaojian.pojo.User"c:age="24" c:name="小贱"/>
若此时User对象没有有参构造会爆红。
也就是说c命名空间就是构造器注入。
3.2 @component等衍生注解配置方法
1. bean的实例化
- 在使用注解进行bean的实例化的时候,首先需要在配置文件中进行开启注解扫描的配置
<!--开启注解的扫描 扫描pojo包下面的所有类--><context:component-scan base-package="com.xiaojian"/><!--只会扫描属性上的注解--><context:annotation-config/>
- 使用注解创建对象
@Component(value = "userServiceImpl") //相当于在配置文件中<bean id="computer" class="……"></bean>@Scope(value = "prototype") // 配置创建对象是否以单例模式进行创建public class UserServiceImpl implements UserService {public User queryUser() {User user = new User();user.setAge(11);return user;}}
在需要实例化的类的类名上面加上@Component 注解来进行标识,value的值就相当于在配置文件中进行配置时bean标签的id属性的值,用于对象的创建
2. 创建对象的四个注解
@Component@Controller //web层中使用@Service //业务层@Repository //持久层
这四个注解目前的功能都是一样的,注解名的不同为了能够让标记类本身的用途更加清晰,Spring在后续的版本中会对其加强
3. 使用注解注入对象属性
在开发中,我们经常会遇到在一个类中调用另一个类的情况(控制层调用业务逻辑层),这就属于注入了对象属性,现在我们学习使用注解的方式注入对象属性。
@Component(value = "userDaoImpl")public class UserDaoImpl implements UserDao {@Autowired // 自动装配,根据类名称去找相应的类来创建对象private UserService userService;public User queryUser() {User user = userService.queryUser();user.setName("xiaoxin ");return user;}}
3.1 两种对象属性注入的注解
- 使用 @Autowired 注解进行自动装配,不需要指定要注入的对象的value值,自动的根据类名去寻找对应的类来创建对象并进行对象属性的注入。
- 使用 @Resource(name=”userDao”),需要指定需要创建的对象的名字,这里的name对应@Component注解中的value的值,使用这个注解能够根据我们所指定的对象名准确创建出我们所需要的对象。
@Resource(name = "userServiceImpl") // name属性值写使用注解创建对象时的value中的值private UserService userService;
3.3 配置文件和注解混合使用
- 创建对象使用配置文件的方式
public class UserDaoImpl implements UserDao {public void add() {System.out.println("注解,配置文件混合版");}}
<bean id="userDao" class="com.xiaojian.Dao.UserDaoImpl"/>
- 注入属性使用注解的方式在UserServiceImpl中引用UserDaouserDaoImpl类
@Component(value = "userServiceImpl")public class UserServiceImpl implements UserService {@Resource(name = "userDao")private UserDao userDao;public void add() {userDao.add();}}
- 测试类
@Testpublic void test3() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = (UserService) context.getBean("userServiceImpl");userService.add();}
- 测试结果
注解,配置文件混合版
3.4 Java的配置方式
后续。。。。
