Java EE 实验 第4课 Spring基础

结构图

Java EE 实验 第4课 Spring基础

源码

User

package cn.edu;
public class User{
    private Long id;
    private String name;

    public Long getId(){
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    	   http://www.springframework.org/schema/beans/spring-beans.xsd
						   http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">


	<bean id="user" name="u,u1" class="cn.edu.User" scope = "singleton">
 	<!-- Propeties Injection Dependency -->
	<property name  = "id" value = "2019216584" />
	<property name  = "name" value = "丁帅帅" />
	</bean>

                <bean id="user2" name="u2" class="cn.edu.User" scope = "singleton">
 	<!-- Propeties Injection Dependency -->
	<property name  = "id" value = "2019216584" />
	<property name  = "name" value = "丁帅帅" />
	</bean>
	
</beans>  

DemoSpringApp

import org.springframework.context.ApplicationContext;
import cn.edu.*;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.SpringVersion;
public class DemoSpringApp 
{
    public static void main( String[] args )
    {
		// Using XML File to initialize container.
	//		AbstractApplicationContext context = new 
		//	ClassPathXmlApplicationContext("/applicationContext.xml",DemoSpringApp.class);
		AbstractApplicationContext context = new 
			ClassPathXmlApplicationContext("/applicationContext.xml");
		// Get SpringVersion
			System.out.println("Spring Version: "+SpringVersion.getVersion());
		// Get a Bean	
		User user1= (User) context.getBean("u1");
		User user2= (User) context.getBean("u2");// 运行一次后,把u2改为u1查看运行结果有什么不一样

		// Execute a method of the Bean
		// // hashCode()取得的是对象的哈希码,哈希码一样说明是同一个对象
        // 这个实验要看看从一个Bean配置是否是单例模式
		System.out.println("user1 hashCode = "+user1.hashCode());
		System.out.println("user2 hashCode = "+user2.hashCode());

		System.out.println("user1's id = "+user1.getId()
		                    +" | user1's name ="+user1.getName());
		System.out.println("user2's id = "+user2.getId()
		                    +" | user2's name ="+user2.getName());
		
		context.registerShutdownHook();
		context.close();
    }
}

Spring5版

1.编译内容(jc.bat)

setlocal
set "SPRING=.\spring\"
javac -cp ".;%SPRING%javax.inject-1.jar;%SPRING%javax.servlet-api-4.0.1.jar;%SPRING%junit-3.8.1.jar;%SPRING%spring-aop-5.3.8.jar;%SPRING%spring-beans-5.3.8.jar;%SPRING%spring-context-5.3.8.jar;%SPRING%spring-core-5.3.8.jar;%SPRING%spring-expression-5.3.8.jar;%SPRING%spring-jcl-5.3.8.jar" -encoding "UTF-8" DemoSpringApp.java

2.编译截图

Java EE 实验 第4课 Spring基础

3.运行内容(jrun.bat)

setlocal
set "SPRING=.\spring\"
java -cp ".;%SPRING%javax.inject-1.jar;%SPRING%javax.servlet-api-4.0.1.jar;%SPRING%junit-3.8.1.jar;%SPRING%spring-aop-5.3.8.jar;%SPRING%spring-beans-5.3.8.jar;%SPRING%spring-context-5.3.8.jar;%SPRING%spring-core-5.3.8.jar;%SPRING%spring-expression-5.3.8.jar;%SPRING%spring-jcl-5.3.8.jar" DemoSpringApp

4.运行截图

Java EE 实验 第4课 Spring基础

Spring4版

1.编译

setlocal
set "SPRING=.\spring4\"
javac -cp ".;%SPRING%spring-beans-4.1.6.RELEASE.jar;%SPRING%spring-core-4.1.6.RELEASE.jar;%SPRING%spring-context-4.1.6.RELEASE.jar;" -encoding "UTF-8" -Xlint:deprecation %1

Java EE 实验 第4课 Spring基础

2.运行

setlocal
set "SPRING=.\spring4\"
java -cp ".;%SPRING%spring-beans-4.1.6.RELEASE.jar;%SPRING%spring-core-4.1.6.RELEASE.jar;%SPRING%spring-context-4.1.6.RELEASE.jar;%SPRING%spring-expression-4.1.6.RELEASE.jar;%SPRING%commons-logging-1.1.1.jar;"  %1

Java EE 实验 第4课 Spring基础

修改后的截图

Java EE 实验 第4课 Spring基础

上一篇:eclipse IDE中如何实现Java-web


下一篇:2022/1/31