四夕桐人

博客园 首页 新随笔 联系 订阅 管理

1.概要:

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

对象创建方式

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

 

 

 

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

 

 

 

3.导入依赖

日志依赖:log4j

测试依赖:junit

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

 

 

 

4.创建resources

log4j.properties配置如下:

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

 

 5.创建bean层

public class Person {
    private Integer id;
    private String name;

//无参,有参,getset,tostring

    public Person() {
        System.out.println("调用了无参构造");//方便观察如何调用的
    }

    public Person(Integer id, String name) {
    System.out.println("调用了有参构造。。") this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { System.out.println("调用了setId"); this.id = id; } public String getName() { return name; } public void setName(String name) { System.out.println("调用了setName"); this.name = name; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + '}'; }

//静态工厂模式创建对象要单独在同一个bean中,新建一个类
  package com.dream.bean;

  public class PersonFactory {
   public static Person createPerson(){
   return new Person(3,"使用静态工厂创建p3.....");
   }
  }

//非静态工厂模式创建对象要单独新建一个类
  package com.dream.bean;

  public class PFactory {
   public Person createPerson(){
   return new Person(4,"使用非静态工厂创建p4.....");
   }
  }

 

 

 6.创建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的反射机制 创建对象-->
    <!--1:默认调用无参数的构造方法-->
    <!--2.调用有参数的构方法创建对象-->
    <!--3.使用静态工厂创建对象-->
    <!--4.使用非静态工厂创建对象-->


   <!--1:默认调用无参数的构造方法-->
<bean id="p1" class="com.dream.bean.Person">
<property name="id" value="1"/>
<property name="name" value="这是spring创建的第一个对象"/>
</bean>

  <!--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>
  
  <!--3.使用静态工厂创建对象-->
<bean id="p3" class="com.dream.bean.PersonFactory" factory-method="createPerson"/>

  <!--4.使用非静态工厂创建对象-->
<bean id="factory" class="com.dream.bean.PFactory"/>
<bean id="p4" factory-bean="factory" factory-method="createPerson"/>

</beans>

 

 7.创建测试类

 

package com.dream.test;
import com.dream.bean.Person;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testSpring {
@Test public void testSpring01(){ //启动加载配置文件,并启动Spring框架 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 从Spring容器中获取对象(装的是对象!Spring就是对象的容器!) Person p = context.getBean("p1", Person.class); System.out.println(p); }
打印结果:
调用了无参构造
调用了setId
调用了setName
调用了无参构造
调用了setId
调用了setName
Person{id=1, name='这是spring创建的第一个对象'}

    
@Test
public void testSpring02(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person p = context.getBean("p2", Person.class);
System.out.println(p);
}

打印结果:

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




@Test
public void testSpring03(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person p = context.getBean("p3", Person.class);
System.out.println(p);
}

打印结果:Person{id=3, name='使用静态工厂创建p3.....'}


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

}

  

 

 8.另附xml中属性解释

 

 

posted on 2019-05-17 00:34  四夕桐人  阅读(479)  评论(0编辑  收藏  举报