iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 8
1

ApplicationContext主要實現類
(1)ClassPathXmlApplicationContext 依照類別路徑
(2)FileSystemXmlApplicationContext 依照系統檔案指定路徑

DI依賴注入三種方法 (1)屬性注入(2)構造器注入(3)工廠方法注入

(1)setter注入最常用方法
		XML
		 `<bean name="helloWorld,helloWorld2" class="com.spring.beans.HelloWorld">
			<property name="name" value="Spring"></property>
		</bean>`
		Java
		`public void setname(String name){
			System.out.println("setName:"+name);
			this.name = name;
		}`
(2)構造器注入(屬性值可以在xml指定參數位置及類型以區分overloading重載的構造器
		XML	
		`<!-- 構造器注入 -->
			<bean id="car" class="com.spring.beans.Car">
				<constructor-arg value="Audi" index="0"></constructor-arg>
				<constructor-arg value="ShanHai" index="1"></constructor-arg>
				<constructor-arg value="3000000" type="double"></constructor-arg>
			</bean>
			<bean id="car2" class="com.spring.beans.Car">
				<constructor-arg value="BMW" ></constructor-arg>
				<constructor-arg value="Taipei"></constructor-arg>
				<constructor-arg value="240" type="int"></constructor-arg>
			</bean>`
		Car.Java
			`public class Car {
				private String brand;
				private String corp;
				private double price;
				private int maxSpeed;

			public Car(String brand, String corp, double price) {
				super();
				this.brand = brand;
				this.corp = corp;
				this.price = price;
			}

			public Car(String brand, String corp, int maxSpeed) {
				super();
				this.brand = brand;
				this.corp = corp;
				this.maxSpeed = maxSpeed;
			}
		Main.Java
			public class Main {
				Car car = (Car) ctx.getBean("car");
				System.out.println(car);
	
				Car car2 = (Car) ctx.getBean("car2");
				System.out.println(car2);
			}`
		輸出結果
			
				`Car [brand=Audi, corp=ShanHai, price=3000000.0, maxSpeed=0]
				Car [brand=BMW, corp=Taipei, price=0.0, maxSpeed=240]`

(3)工廠方法也分兩類 靜態和實例

	3.1 靜態方式就是指Factory類本身不需要實例化, Factory類別中提供了1個静態方法来生成bean
		
		Car.java
			`public class Car {
					private int id;
					private String name;
					private int price;
				public int getId() {
    					return id;
					}

				 public void setId(int id) {
				        this.id = id;
					}

			        public String getName() {
    					return name;
					}

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

			        public int getPrice() {
    				  	return price;
					}

			        public void setPrice(int price) {
    					this.price = price;
					}

		    @Override
				public String toString() {
    					return "Car [id=" + id + ", name=" + name + ", price=" + price + "]";
					}

			  public Car(){

				  }

			  public Car(int id, String name, int price) {
    					super();
    					this.id = id;
    					this.name = name;
    					this.price = price;
					  }
				}`

		 CarStaticFactory.java
			`public class CarStaticFactory {
					private static Map<Integer, Car> map = new HashMap<Integer,Car>();

				static{
   					 map.put(1, new Car(1,"Honda",300000));
   					 map.put(2, new Car(2,"Audi",440000));
  				         map.put(3, new Car(3,"BMW",540000));
			      }

				public static Car getCar(int id){
  					  return map.get(id);
				      }
			}`
	XML
		`<bean id="bmwCar" class="com.home.factoryMethod.CarStaticFactory" factory-method="getCar">
    			<constructor-arg value="3"></constructor-arg>           
			</bean>

		 <bean id="audiCar" class="com.home.factoryMethod.CarStaticFactory" factory-method="getCar">
			        <constructor-arg value="2"></constructor-arg>           
		 </bean>`
	Main.java
		`public static void Main(){
			public static void main(String[] args){
			ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-factoryMethod.xml");
			Car car1 = (Car) ctx.getBean("bmwCar");
		        System.out.println(car1);

		        car1 = (Car) ctx.getBean("audiCar");
		        System.out.println(car1);
			}
		}`

3.2實例工廠方式就是getBean方法不是静態的,代表要先實例1個工廠對象, 才能依靠這個工廠對象去getBean對 象。
CarInstanceFactory.java
`public class CarInstanceFactory {
private Map<Integer, Car> map = new HashMap<Integer,Car>();

				  public void setMap(Map<Integer, Car> map) {
    				this.map = map;
				  }

				  public CarInstanceFactory(){
		          }

		          public Car getCar(int id){
    				return map.get(id);
		          }
		  }`
		XML
		`<bean id="carFactory" class="com.home.factoryMethod.CarInstanceFactory">
    				<property name="map">
        			<map>
         			   	<entry key="4">
                		        	<bean class="com.home.factoryMethod.Car">
                        			<property name="id" value="4"></property>   
                       	        	<property name="name" value="Honda"></property> 
                                    	<property name="price" value="300000"></property>   
                    	</bean>
            			</entry>    

            			<entry key="6">
                  		        	<bean class="com.home.factoryMethod.Car">
                      	        	<property name="id" value="6"></property>   
                        	        	<property name="name" value="ford"></property>  
                      	       	        <property name="price" value="500000"></property>   
                   		</bean>
            			</entry>
       			</map>  
    				</property>
			</bean>
	
 			<bean id="car4" factory-bean="carFactory" factory-method="getCar">
    			<constructor-arg value="4"></constructor-arg>           
 			</bean>

	        <bean id="car6" factory-bean="carFactory" factory-method="getCar">
    		<constructor-arg value="6"></constructor-arg>           
 			</bean>`
		Main.java
			`public static void h2(){
    				ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-factoryMethod.xml");
				Car car1 = (Car) ctx.getBean("car4");
					System.out.println(car1);
 					car1 = (Car) ctx.getBean("car6");
  					System.out.println(car1);
				}`

上一篇
Day 07 Spring提供的@Autowired標籤介紹
下一篇
Day09 Spring集合屬性
系列文
30天學習J2EE Spring+Hibernate學習心得記錄12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言