javaee之Spring的練習

Spring:
Spring是一個javaee企業級應用的一站式框架,基於IOC和AOP思想的框架,可以整合Struts2、hibernate、Servlet、JDBC等技術和框架。

Spring大致的體系分為6各模塊:

IOC:
ioc,控制反轉,把創建對象的行為交給容器去完成,是解決對象的創建問題;

applicationContext.xml:





	


	
	 
	
	 
	
		
		
		
		
	
	
	
	
	
	
	
	
	
	
	
	

Student:

public class Student {


	//使用無參的構造方法,默認的控制反轉為無參
	public Student(){
		System.out.println("調用瞭無參");
	}
	
	//使用有參的構造方法來反轉
	public Student(String name,int age){
		System.out.println("調用瞭有參:name = "+name+ ";age = "+age);
	}
}

StudentFactory:

public class StudentFactory {


	//調用成員方法來ioc
	public Student getStudent(){
		System.out.println("成員方法");
		return new Student();
	}
	
	//調用靜態方法來ioc
	public static Student getStudentStatic(){
		System.out.println("靜態方法");
		return new Student();
	}
}

測試類:

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


public class Demo {


	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("demo3_ioc/applicationContext.xml");  //讀取的文件是在類路徑下的文件
		Student s  = (Student)ac.getBean("s2");
		/*for(int i=1;i<=5;i++){
			Student s  = (Student)ac.getBean("s2");
			System.out.println(s);
		}*/
	}
}

在ioc的過程中,需要註意的是:
1)、不用考慮線程安全的情況盡量使用單例,用以節省內存,如在dao層、service層
2)、如果有線程安全的情況下必須使用多例,如action對象的創建

DI,依賴註入,把對象之間的關系交給容器完成,是解決對象與對象之間的依賴關系

applicationContext.xml:

		



	
		
		
		
		
		
			
				廣州高鐵
				廣西高鐵
			
		
		
		
		
			
				珠海常量
				廣州常量
			
		
		
		
		
				
				
		
		
		
		
			
				廣東
				廣西
			
		
		
		
		
		
		
		
		
			
				
				
			
		
		
	
	
	
	
		
	
	
		
	
	

User:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;


public class User {


	//1.基本數據類型:String
		private String name;
		//用於外部註入數據
		public void setName(String name) {
			this.name = name;
		}
		private int age;
		public void setAge(int age) {
			this.age = age;
		}
		
		//2.數組類型
		private String[] addresses;
		public void setAddresses(String[] addresses) {
			this.addresses = addresses;
		}
		
		//3.List集合
		private List addressList;
		public void setAddressList(List addressList) {
			this.addressList = addressList;
		}
		
		
		//4.Map集合
		private Map addressMap;
		public void setAddressMap(Map addressMap) {
			this.addressMap = addressMap;
		}
		
		
		//5.Properties對象
		private Properties addressProp;
		public void setAddressProp(Properties addressProp) {
			this.addressProp = addressProp;
		}
		
		//6.javabean對象
		private Address addr;
		public void setAddr(Address addr) {
			this.addr = addr;
		}
		
		
		//7: List集合+javabean
		private List addressList2;
		public void setAddressList2(List addressList2) {
			this.addressList2 = addressList2;
		}
		@Override
		public String toString() {
			return "User [name=" + name + ", age=" + age + ", addresses="
					+ Arrays.toString(addresses) + ", addressList=" + addressList
					+ ", addressMap=" + addressMap + ", addressProp=" + addressProp
					+ ", addr=" + addr + ", addressList2=" + addressList2 + "]";
		}
}

Address:

	
public class Address {


	private String name;


	public String getName() {
		return name;
	}


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


	@Override
	public String toString() {
		return "Address [name=" + name + "]";
	}
	
	
}

測試類:


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


public class Demo {


	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("demo4_di/applicationContext.xml");
		User u = (User)ac.getBean("ul");
	
		System.out.println(u);
	}
}

Spring的註解:
IOC註解:
@Component: 用在類的頂部,代表聲明一個對象,
@Respositoty:等價於@component,用於持久層(dao層)
@Service:等價於@component,用於業務層(service層)
@Controller:等價於@component,用於視圖層(web層)

DI註解:
@Resource:用於註入數據對象,
@Autowire:用於註入數據,自定義匹配名稱進行註入(自動的註入)

applicationContext.xml:



      
      
      
	

User:

import javax.annotation.Resource;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;


//@Component("userID")
@Repository("userID")  //repository和component的效果一樣,都是在類上進行反轉,如
public class User {


	@Resource(name="addID")
	//如果屬性名稱和需要註入的javabean的ID名稱一致,可以使用@Autowise(可以省略setter方法)
	@Autowired //自動裝載
	private Address add;
	
	public void setAdd(Address add) {
		this.add = add;
	}


	public User(){
		System.out.println("構造");
	}


	@Override
	public String toString() {
		return "User [add=" + add + "]";
	}
	
	
}

Address:

import org.springframework.stereotype.Component;


@Component("addID")
public class Address {


	public Address(){
		System.out.println("調用Address構造方法");
	}
}

測試類:

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


public class Demo {


	public static void main(String[] args) {
			ApplicationContext ac = new ClassPathXmlApplicationContext("demo5_annotation/applicationContext.xml");
			
			User u = (User)ac.getBean("userID");
			System.out.println(u);
		}
	}

在Spring中,主要要瞭解的是IOC和AOP,而IOC是基礎底層,解決對象的問題,大大增加開發效率,不過也是會減低運行效率。

發佈留言