- 1、@Scope("***"):描述spring是如何新建bean的
- Singleton:單例共享
- Prototype:每次調用都新建實例
- Request/Session:web項目中,每個session新建
- GlobalSession:portal應用中,每個globalsession新建
2、EL和資源調用
在屬性上面添加注解@Value
@Component("customerBean") public class Customer { @Value("#{itemBean}") private Item item; @Value("#{itemBean.name}") private String itemName; //getter and setter... }
3、Bean的初始化和銷毀
- initMethod
- destroyMethod
- PostConstruct
- PreDestroy
- 4、Profile為不同環境使用不同的配置
@Configuration public class ProfileConfig { @Bean @Profile("dev") public DemoBean devDemoBean() { return new DemoBean("from development profile"); } }
- 5、事件
自定義事件:繼承ApplicationEvent
定義事件監聽器:實現ApplicationListener
使用onApplicationEvent方法對消息進行接收處理注入ApplicationContext用以發布事件
使用ApplicationContext的publishEvent方法發布事件