Spring 环境搭建

Spring 环境搭建

Spring 环境搭建步骤

  • 导 jar 包,有几个主要的包。如下:

spring_jar

  • 创建配置文件 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">
    <!-- id表示获取到对象标识
    	 class 创建哪个类的对象
     -->
    <bean id="peo" class="com.spring.People"/>
</beans>

Spring 创建对象的三种方式

  • 先创建一个People类。
public class People {
	private int id;
	private String name;
	public People() {
		super();
		System.out.println("people 构造方法");
		// TODO Auto-generated constructor stub
	}
	public People(int id, String name) {
		super();
		this.id = id;
		this.name = name;
		System.out.println("people 有参构造方法");
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", name=" + name + "]";
	}
}
  1. 通过构造方法创建

    1.1 无参构造创建:默认情况。

    配置文件中写以下代码:

<bean id="peo" class="com.spring.People"/>

​ 调用对象写以下代码:

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
People people = ac.getBean("peo",People.class);
		
System.out.println(people);

​ 1.2 有参构造创建:需要明确配置。

​ 配置文件中写以下代码:

<bean id="peo" class="com.spring.People">
  <constructor-arg name="id" value="123"></constructor-arg>
  <constructor-arg name="name" value="张三"></constructor-arg>
</bean>

​ 调用对象同上。

  1. 实例工厂
  • 帮助创建类对象,一个工厂可以生产多个对象。
  • 实例工厂就是需要先创建工厂,才能生产对象。

必须有一个实例工厂

public class PeopleFactory {
	public People newInstance () {
		return  new People(1,"测试");
	}
}

然后在applicationContext.xml中配置工厂和需要创建的对象。

<bean id="factory" class="com.spring.PeopleFactory"></bean>
<bean id="peo1" factory-bean="factory" factory-method="newInstance"></bean>

测试代码

public class Test {
	public static void main(String [] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//使用xml创建对象
		People people = ac.getBean("peo1",People.class);
		
		System.out.println(people);
		
		
		//普通对象创建对象
		/*PeopleFactory factory = new PeopleFactory();
		People people2 = factory.newInstance();
		System.out.println(people2);*/
	}
	
}
  1. 静态工厂
  • 静态工厂就是不需要创建工厂,可以直接生产对象。
  • 只需要将工厂类中方法改为静态的。
public class PeopleFactory {
	public static People newInstance () {
		return  new People(1,"测试");
	}
}

然后在applicationContext.xml中配置工厂和需要创建的对象。

<bean id="peo2" class="com.spring.PeopleFactory" factory-method="newInstance"></bean>

  • 此处可以看到无需调用factory类,可以直接生产对象。

测试代码

public class Test {
	
	public static void main(String [] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		People people = ac.getBean("peo2",People.class);
		
		System.out.println(people);
		
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

i白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值