欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring之创建bean(xml配置)

程序员文章站 2022-05-24 16:12:08
...

1.通过无参构造创建

Bean1.java

package zh.spring.entity;

public class Bean1 {

	// 需要无参构造方法
	public Bean1(){
		
	}
	
	public void fun() {
		System.out.println("bean1...");
	}

}

在src下创建bean1.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">
	
	<!-- scope取值:singleton(默认)、prototype、request、session、globalSession -->
	<bean id="bean1" class="zh.spring.entity.Bean1" scope="singleton"></bean>

</beans>

Bean1Test.java

package zh.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import zh.spring.entity.Bean1;

public class Bean1Test {

	public static void main(String[] args) {

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
		
		Bean1 bean11 = (Bean1) applicationContext.getBean("bean1");
		Bean1 bean12 = (Bean1) applicationContext.getBean("bean1");
		
		// scope="singleton",所以创建的对象是单例的
		System.out.println(bean11);// [email protected]
		System.out.println(bean12);// [email protected]

		bean11.fun();// bean1...
		bean12.fun();// bean1...
		
	}

}

2.通过静态工厂创建

Bean1.java

package zh.spring.entity;

public class Bean1 {

	public Bean1(){
		
	}
	
	public void fun() {
		System.out.println("bean1...");
	}

}

MyFactory1.java

package zh.spring.entity;

/**
 * 静态工厂
 * @author ZH
 */
public class MyFactory1 {
	
	public static Bean1 getBean1(){
		return new Bean1();
	}
	
}

在src下创建bean1.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">
	
	<!-- scope取值:singleton(默认)、prototype、request、session、globalSession -->
	<bean id="bean1" class="zh.spring.entity.MyFactory1" factory-method="getBean1" scope="prototype"></bean>

</beans>

Bean1Test.java

package zh.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import zh.spring.entity.Bean1;

public class Bean1Test {

	public static void main(String[] args) {

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
		
		Bean1 bean11 = (Bean1) applicationContext.getBean("bean1");
		Bean1 bean12 = (Bean1) applicationContext.getBean("bean1");
		
		// scope="prototype",所以创建的对象是多例的
		System.out.println(bean11);// [email protected]
		System.out.println(bean12);// [email protected]

		bean11.fun();// bean1...
		bean12.fun();// bean1...
		
	}

}

3.通过实例工厂创建

Bean1.xml

package zh.spring.entity;

public class Bean1 {

	public Bean1(){
		
	}
	
	public void fun() {
		System.out.println("bean1...");
	}

}

MyFactory2.java

package zh.spring.entity;

/**
 * 实例工厂
 * @author ZH
 */
public class MyFactory2 {
	
	public Bean1 getBean1(){
		return new Bean1();
	}
	
}

在src下创建bean1.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">
	
	<!-- scope取值:singleton(默认)、prototype、request、session、globalSession -->
	
	<!-- 首先创建实例工厂 -->
	<bean id="myFactory2" class="zh.spring.entity.MyFactory2"></bean>
	
	<!-- factory-bean的值写上面实例工厂的id值 -->
	<bean id="bean1" factory-bean="myFactory2" factory-method="getBean1" scope="prototype"></bean>

</beans>

Bean1Test.java

package zh.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import zh.spring.entity.Bean1;

public class Bean1Test {

	public static void main(String[] args) {

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
		
		Bean1 bean11 = (Bean1) applicationContext.getBean("bean1");
		Bean1 bean12 = (Bean1) applicationContext.getBean("bean1");
		
		// scope="prototype",所以创建的对象是多例的
		System.out.println(bean11);// [email protected]
		System.out.println(bean12);// [email protected]

		bean11.fun();// bean1...
		bean12.fun();// bean1...
		
	}

}