FactoryBean+Bean
十余年的天元網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整天元建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“天元網(wǎng)站設(shè)計(jì)”,“天元網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
package com.example.demo10.entity;
//實(shí)體
public class Stu {
String name;
public Stu(String name) {
this.name = name;
}
public Stu() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.example.demo10.factory;
import com.example.demo10.entity.Stu;
import org.springframework.beans.factory.FactoryBean;
//創(chuàng)建一個(gè)Stu場(chǎng)子
public class StuFactory implements FactoryBean
@Override
public Class> getObjectType() {
return Stu.class;
}
@Override
public boolean isSingleton() {
return false;
}
@Override
public Stu getObject() throws Exception {
return new Stu();
}
}
package com.example.demo10.configuration;
import com.example.demo10.factory.StuFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class StuConfiguration {
//創(chuàng)建場(chǎng)子Bean
//這里要注意返回的是場(chǎng)子對(duì)象,但是正真獲取的是Stu對(duì)象
@Bean
public StuFactory stuFactory(){
return new StuFactory();
}
}
package com.example.demo10;
import com.example.demo10.entity.Stu;
import com.example.demo10.factory.StuFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Demo10Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Demo10Application.class, args);
//獲取場(chǎng)子里面的產(chǎn)品
Stu stu1 = context.getBean("stuFactory", Stu.class);
Stu stu2 = context.getBean("stuFactory", Stu.class);
System.out.println(stu1.getClass().getName());
System.out.println(stu1.hashCode());
System.out.println(stu2.getClass().getName());
System.out.println(stu2.hashCode());
//獲取工廠自己
StuFactory stuFactory = context.getBean("&stuFactory", StuFactory.class);
System.out.println(stuFactory.getClass().getName());
context.close();
}
}