1. 程式人生 > 其它 >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.編譯截圖

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.執行截圖

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

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

修改後的截圖

世界不會因為你的疲憊,而停下它的腳步