Metode pabrik instance Spring dan metode pabrik statis dapat digunakan untuk instantiate kacang. Dalam artikel ini, kita akan melihat contoh terkait.
Metode Pabrik Statis: Menelepon langsung metode statis dapat mengembalikan contoh kacang
paket com.zhu.string.factory; impor java.util.hashmap; impor java.util.map; Metode Public Class StaticCarFactory { / *** Statis Factory: Langsung panggil metode statis untuk mengembalikan instance dari kacang** / peta statis pribadi <string, car> cars = hashmap baru <string, car> (); static {cars.put ("audi", mobil baru (3000, "aodi")); cars.put ("fodo", mobil baru (3000, "aodi")); } // Metode Pabrik Statis Public Static Car GetCar (String Name) {return cars.get (name); }}Contoh metode pabrik. Yaitu, panggil pabrik itu sendiri, lalu panggil metode instance pabrik untuk mengembalikan instance kacang
paket com.zhu.string.factory; impor java.util.hashmap; impor java.util.map; InstanceCarFactory kelas publik { /*** Metode pabrik instan. Artinya, panggil pabrik itu sendiri, dan kemudian panggil metode instance pabrik untuk mengembalikan instance bean*/ peta pribadi <string, car> cars = null; Public InstanceCarFactory () {// TODO Konstruktor Stubs Constructor Auto-Auto-Auto = New HashMap <String, Car> (); cars.put ("audi", mobil baru (1000, "audi")); cars.put ("dffdas", mobil baru (2000, "audi")); } Public Car GetCar (String Brand) {return cars.get (Brand); }}Beans-factory.xml
<span style = "font-size: 14px;"> <? xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http:-plmaM. xmlns: p = "http://www.springframework.org/schema/p" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org.org/beans/beans/bwww.spramework.orger/schema/beans kacang melalui metode statis. Note that it is not configuring static factory method instances, but configuring bean instances --> <!-- class attribute: pointing to the full class name of the static method factory-method: pointing to the name of the static method constructor-arg: If the factory method needs to pass in parameters, use constructor-arg to configure the parameters --> <bean id="car1" factory-method="getCar" > <constructor-arg value="audi"></constructor-arg> </bean> <!-- Configure factory instance --> <bean id="carFactory"> </bean> <bean id="car2" factory-bean="carFactory" factory-method="getCar"> <constructor-arg value="audi"></constructor-arg> </bean> </boy> </span>
Kelas entitas car.java
Paket com.zhu.string.factory; mobil kelas publik {harga ganda pribadi; merek string pribadi; getPrice double publik () {harga kembali;} public void setPrice (harga ganda) {this.price = harga;} public string getBrand () {return brand;} public void setBrand (string brand) {this.brander = {{over@wround@@@@@@{{@{{@{@{@{{public@{{@{@{@{{@{{@{{@{thand thand {thand@{{{@{thand thand {thand@{this. Brand + ", Price =" + Price + "]";} Public Car () {System.out.println ("Cars..Constructor");} Mobil Publik (Harga Ganda, Merek String) {super (); this.price = harga; this.brand = brand;}}Main.java
package com.zhu.string.factory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub ApplicationContext cx = classpathxmlapplicationContext baru ("beans- factory.xml"); car car1 = (car) cx.getbean ("car1"); System.out.println (car1); car car2 = (car) cx.getbean ("car2"); System.out.println (car2); Hasil Menjalankan:
Mobil [merek = aodi, harga = 3000.0]
Mobil [Brand = Audi, Harga = 1000.0]
Meringkaskan
Di atas adalah seluruh konten artikel ini tentang metode pabrik Contoh Spring dan kode contoh metode pabrik statis. Saya harap ini akan membantu semua orang. Teman yang tertarik dapat terus merujuk ke topik terkait lainnya di situs ini. Jika ada kekurangan, silakan tinggalkan pesan untuk menunjukkannya. Terima kasih teman atas dukungan Anda untuk situs ini!