spring framework入门(3):bean的装配

示例代码:https://download.csdn.net/download/u010476739/11419455

sping入门参照:https://blog.csdn.net/u010476739/article/details/76696393

试验条件:

maven

spring framwork 4.2.4.RELEASE

试验目的:

试验bean的构建方法:空参构建、带参构造函数构建、单例模式

1. 首先pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jackletter</groupId>
  <artifactId>springxmldemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>this is name</name>
  <description>this is desc</description>
  
  <properties>
  	<spring.version>4.2.4.RELEASE</spring.version>
  </properties>
  
  <dependencies>
 
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
         
    </dependencies>
</project>

2. 配置文件beans.xml (springxmldemo\src\main\resources\applicationContext.xml)

 

<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">
	<!-- 根据类的空参构造函数构建简单的bean实例,默认是单例的(在这个容器中) -->
	<bean name="dogSimple" class="bean.Dog"></bean>

	<!-- 根据类的带参构造函数构建简单的bean实例,参数为简单类型 -->
	<bean name="dogConstructor" class="bean.Dog">
		<constructor-arg value="lucy" />
	</bean>

	<!-- 根据类的带参构造函数构建简单的bean实例,参数为集合类型 -->
	<bean name="dogConstructorList" class="bean.Dog">
		<constructor-arg>
			<list>
				<value>lucy</value>
				<value>tom</value>
			</list>
		</constructor-arg>
	</bean>

	<!-- 根据根据类的带参构造函数构建简单的bean实例,参数为引用类型 -->
	<bean id="actorConstructorRef" class="bean.Actor">
		<constructor-arg ref="dogConstructorList"></constructor-arg>
	</bean>

	<!-- 根据类的静态方法构建bean实例 -->
	<bean id="stage" class="bean.Stage" factory-method="getInstance">
	</bean>

	<!-- 根据类的空参构造函数构建简单的bean实例,每次获取bean的时候都会重新构建一个新的实例 -->
	<bean id="personPrototype" class="bean.Person" scope="prototype">
	</bean>

	<!-- 这个bean是为下面的String像Date转换做准备的 -->
	<bean id="dateFormat" class="java.text.SimpleDateFormat">
		<constructor-arg value="yyyy-MM-dd" />
	</bean>
	
	<!-- 根据类的空参构造函数构建简单的bean实例,并且初始化属性设置 -->
	<bean id="personProperty" class="bean.Person">
		<property name="name" value="晓明" />
		<property name="age" value="20" />
		<property name="birthday">
			<bean factory-bean="dateFormat" factory-method="parse">
				<constructor-arg value="1995-02-03" />
			</bean>
		</property>
		<property name="scores">
			<list>
				<value>98</value>
				<value>65</value>
				<value>83</value>
			</list>
		</property>
	</bean>
</beans>

3. 代码结构

4.代码

Dog.java

package bean;

import java.util.List;

public class Dog {
	public Dog(){
		
	}
	public String show(){
		if(name!=null&&name!=""){
			return "i'm a dog named "+name;
		}
		if(names!=null){
			String res="i'm a dog named ";
			for(int i=0;i<names.size();i++){
				res+=names.get(i)+",";
			}
			return res;
		}
		return "i'm a dog";
	}
	
	private String name;
	public Dog(String name){
		this.name=name;
	}
	private List<String> names;
	public Dog(List<String> names){
		this.names=names;
	}
}

Person.java

package bean;

import java.util.Date;
import java.util.List;

public class Person {
	private String name;
	private int age;
	private Date birthday;

	private List<Double> scores;

	public List<Double> getScores() {
		return scores;
	}

	public void setScores(List<Double> scores) {
		this.scores = scores;
	}

	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 Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Override
	public String toString() {
		String base = "{name:" + this.name + ",age:" + this.age + ",birthday:" + this.birthday + "}";
		base += "\r\n";
		for (Double score : scores) {
			base += score + "\r\n";
		}
		return base;
	}
}

Actor.java

package bean;

public class Actor {
	public Dog dog;

	public Actor(Dog dog) {
		this.dog = dog;
	}
}

 

 

Stage.java

package bean;

public class Stage {
	private Stage() {
		
	}
	public String show(){
		return "i'm stage";
	}
	private static class F{
		static Stage sta= new Stage();
	}
	
	public static Stage getInstance(){
		return F.sta;
	}
}

App.java

package springxmldemo;

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

import bean.Actor;
import bean.Dog;
import bean.Person;
import bean.Stage;

public class App {
	public static void main(String[] args) {
		//启动spring容器
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//1. 根据类的空参构造函数构建简单的bean实例,默认是单例的(在这个容器中)
		Dog dogSimple=(Dog) context.getBean("dogSimple");
		//i'm a dog
		System.out.println(dogSimple.show());
		Dog dogSimple2=(Dog) context.getBean("dogSimple");
		//true
		System.out.println(dogSimple2==dogSimple);
		
		//2.根据类的带参构造函数构建简单的bean实例,参数为简单类型
		Dog dogConstructor=(Dog) context.getBean("dogConstructor");
		//i'm a dog named lucy
		System.out.println(dogConstructor.show());
		
		//3.根据类的带参构造函数构建简单的bean实例,参数为集合类型
		Dog dogConstructorList=(Dog) context.getBean("dogConstructorList");
		//i'm a dog named lucy,tom,
		System.out.println(dogConstructorList.show());
		
		//4. 根据根据类的带参构造函数构建简单的bean实例,参数为引用类型
		Actor actorConstructorRef=(Actor) context.getBean("actorConstructorRef");
		//i'm a dog named lucy,tom,
		System.out.println(actorConstructorRef.dog.show());
		
		//5.根据类的静态方法构建bean实例,这里用工厂方法模拟了程序内单例的实现
		Stage stage=(Stage) context.getBean("stage");
		//i'm stage
		System.out.println(stage.show());
		Stage stage2=(Stage) context.getBean("stage");
		//true
		System.out.println(stage==stage2);
		
		//6.根据类的空参构造函数构建简单的bean实例,在这个spring容器内是非单例的,每次获取bean的时候都会重新构建一个新的实例
		Person personPrototype = (Person) context.getBean("personPrototype");
		Person personPrototype2 = (Person) context.getBean("personPrototype");
		//false
		System.out.println(personPrototype==personPrototype2);
		
		//7.根据类的空参构造函数构建简单的bean实例,并且初始化属性设置
		Person personProperty = (Person) context.getBean("personProperty");
		//false
		System.out.println(personProperty);

	}
}

5. 运行

 

6.结果

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

jackletter

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

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

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

打赏作者

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

抵扣说明:

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

余额充值