Java EE 实验 第4课 Spring基础

2021/10/15 12:44:14

本文主要是介绍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.编译截图

image-20211015110546002

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.运行截图

image-20211015120051927

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

image-20211015111023685

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

image-20211015111119838

修改后的截图



这篇关于Java EE 实验 第4课 Spring基础的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程