IOC的使用

1.概要:

在spring中,都是使用反射机制创建对象,将创建对象的权利交给spring容器,就是控制反转(ioc)

对象创建方式

  • 有参构造
  • 无参构造
  • 工厂模式(静态,非静态)

2.创建IDEA控制台测试项目

3.导入依赖

日志依赖:log4j

测试依赖:junit

spring依赖:spring-context-support、core、context、expression、beans、test

4.创建resources

log4j.properties配置如下:

1# log4J日志框架的配置文件 文件名字不能改 2# DEBUG 表示日志的级别 调试 3# Console 日志打印在控制台 4log4j.rootLogger=DEBUG, Console 5log4j.appender.Console=org.apache.log4j.ConsoleAppender 6log4j.appender.Console.layout=org.apache.log4j.PatternLayout 7log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n 8# 哪些日志需要打印 9log4j.logger.java.sql.ResultSet=INFO 10log4j.logger.org.apache=INFO 11log4j.logger.java.sql.Connection=DEBUG 12log4j.logger.java.sql.Statement=DEBUG 13log4j.logger.java.sql.PreparedStatement=DEBUG

 5.创建bean层

1public class Person { 2 private Integer id; 3 private String name; 4 5//无参,有参,getset,tostring 6 7 public Person() { 8 System.out.println("调用了无参构造");//方便观察如何调用的 9 } 10 11 public Person(Integer id, String name) {    System.out.println("调用了有参构造。。") 12 this.id = id; 13 this.name = name; 14 } 15 16 public Integer getId() { 17 return id; 18 } 19 20 public void setId(Integer id) { 21 System.out.println("调用了setId"); 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 System.out.println("调用了setName"); 31 this.name = name; 32 } 33 34 @Override 35 public String toString() { 36 return "Person{" + 37 "id=" + id + 38 ", name='" + name + '\'' + 39 '}'; 40 }//静态工厂模式创建对象要单独在同一个bean中,新建一个类 41 42  package com.dream.bean;  public class PersonFactory {    public static Person createPerson(){    return new Person(3,"使用静态工厂创建p3.....");    }  } 43 44//非静态工厂模式创建对象要单独新建一个类 45 46  package com.dream.bean;  public class PFactory {    public Person createPerson(){    return new Person(4,"使用非静态工厂创建p4.....");    }  }

 6.创建spring容器创建对象的配置文件(建议使用applicationContext.xml命名)

1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 <!--使用Spring的反射机制 创建对象--> 7 <!--1:默认调用无参数的构造方法--> 8 <!--2.调用有参数的构方法创建对象--> 9 <!--3.使用静态工厂创建对象--> 10 <!--4.使用非静态工厂创建对象--> 11 12<!--1:默认调用无参数的构造方法--> 13 14<bean id="p1" class="com.dream.bean.Person"> <property name="id" value="1"/> <property name="name" value="这是spring创建的第一个对象"/></bean> 15 16  <!--2.调用有参数的构造方法创建对象--><bean id="p2" class="com.dream.bean.Person"> <constructor-arg name="name" value="用index调整传入参数的顺序" index="1" /> <constructor-arg name="id" value="2" index="0" /></bean>   17 18  <!--3.使用静态工厂创建对象--><bean id="p3" class="com.dream.bean.PersonFactory" factory-method="createPerson"/> 19 20  <!--4.使用非静态工厂创建对象--><bean id="factory" class="com.dream.bean.PFactory"/><bean id="p4" factory-bean="factory" factory-method="createPerson"/> 21 22</beans>

 7.创建测试类

1package com.dream.test; 2import com.dream.bean.Person; 3import org.junit.Test; 4import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6public class testSpring { 7@Test 8public void testSpring01(){ 9//启动加载配置文件,并启动Spring框架 10 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 11// 从Spring容器中获取对象(装的是对象!Spring就是对象的容器!) 12 Person p = context.getBean("p1", Person.class); 13 System.out.println(p); 14 }打印结果:调用了无参构造调用了setId调用了setName调用了无参构造调用了setId调用了setNamePerson{id=1, name='这是spring创建的第一个对象'}     15 16@Testpublic void testSpring02(){ 17 18 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person p = context.getBean("p2", Person.class); System.out.println(p);}打印结果:

调用了有参构造。。
调用了有参构造。。
调用了有参构造。。
调用了无参构造
调用了setId
调用了setName
Person{id=2, name='用index调整传入参数的顺序'}

1@Testpublic void testSpring03(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person p = context.getBean("p3", Person.class); System.out.println(p);}打印结果:Person{id=3, name='使用静态工厂创建p3.....'} 2 3@Testpublic void testSpring04(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person p = context.getBean("p4", Person.class); System.out.println(p);} 4 5打印结果: Person{id=4, name='使用非静态工厂创建p4.....'}}

 8.另附xml中属性解释

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )