2.安裝cnpm用cnpm替代npm
地址:http://npm.taobao.org/
PostConstruct注釋用于在完成依賴項(xiàng)注入以執(zhí)行任何初始化之后需要執(zhí)行的方法。必須在類投入使用之前調(diào)用此方法。 所有支持依賴注入的類都必須支持此注釋。即使類沒有請求注入任何資源,也必須調(diào)用使用PostConstruct注釋的方法。 只有一個方法可以使用此批注進(jìn)行批注。 應(yīng)用PostConstruct注釋的方法必須滿足以下所有條件:除了攔截器之外,方法絕不能有任何參數(shù),在這種情況下它采用Interceptor規(guī)范定義的InvocationContext對象。 在攔截器類上定義的方法必須具有以下簽名之一: void <METHOD>(InvocationContext)Object <METHOD>(InvocationContext)拋出異常注意: PostConstruct攔截器方法不能拋出應(yīng)用程序異常,但可以聲明它拋出檢查異常,包括java.lang.Exception, 如果相同的攔截器方法除了生命周期事件之外插入業(yè)務(wù)或超時方法。 如果PostConstruct攔截器方法返回一個值,容器將忽略它。 在非攔截器類上定義的方法必須具有以下簽名:void <METHOD>()應(yīng)用PostConstruct的方法可以是public,protected,package private或private。 除應(yīng)用程序客戶端外,該方法絕不能是靜態(tài)的。 該方法可能是最終的。如果該方法拋出一個未經(jīng)檢查的異常,那么該類絕不能投入使用,除非EJB可以處理異常甚至從它們恢復(fù)的EJB
然后就會思考問題,這個注釋是修飾初始化之后需要執(zhí)行的方法,那么它和@Autowired、構(gòu)造函數(shù)的執(zhí)行順序是什么呢?(當(dāng)然注釋中已經(jīng)說明了PostConstruct注釋用于在完成依賴項(xiàng)注入之后)
public class BeanA {
@Autowired
private BeanB beanB;
public BeanA() {
System.out.println("這是Bean A 的構(gòu)造方法");
}
@PostConstruct
private void init() {
System.out.println("這是BeanA的 init 方法");
beanB.testB();
}
}
public class BeanB {
@PostConstruct
private void init() {
System.out.println("這是BeanB 的init 方法");
}
public BeanB() {
System.out.println("這是Bean B的 構(gòu)造方法");
}
void testB() {
System.out.println("這是Bean B 的 testB 方法");
}
}
啟動后輸出:
這是Bean A 的構(gòu)造方法
這是Bean B的 構(gòu)造方法
這是BeanB 的init 方法
這是BeanA的 init 方法
這是Bean B 的 testB 方法
所以得到結(jié)論: 構(gòu)造方法 > @Autowired > @PostConstruct
2、ApplicationEvent
是個抽象類,里面只有一個構(gòu)造函數(shù)和一個長整型的timestamp。其源碼如下
public abstract class ApplicationEvent extends EventObject {
/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 7099057708183571937L;
/** System time when the event happened */
private final long timestamp;
/**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
/**
* Return the system time in milliseconds when the event happened.
*/
public final long getTimestamp() {
return this.timestamp;
}
}
3、ApplicationListener
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
自定義事件NotifyEvent:
public class NotifyEvent extends ApplicationEvent {
private String email;
private String content;
public NotifyEvent(Object source){
super(source);
}
public NotifyEvent(Object source,String email,String content){
super(source);
this.email = email;
this.content = content;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
定義監(jiān)聽器NotifyListener:
import org.springframework.context.annotation.Configuration;
@Configuration
public class NotifyListener implements ApplicationListener<NotifyEvent>{
@Override
public void onApplicationEvent(NotifyEvent event) {
System.out.println("郵件地址:" + event.getEmail());
System.out.println("郵件內(nèi)容:" + event.getContent());
}
}
單元測試類ListenerTest:
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServerLauncher.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ListenerTest {
@Autowired
private WebApplicationContext webApplicationContext;
@Test
public void testListener(){
NotifyEvent event = new NotifyEvent("object","abc@qq.com","This is the content");
webApplicationContext.publishEvent(event);
}
}
之前用戶使用的是3個注解注解他們的main類。分別是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于這些注解一般都是一起使用,spring boot提供了一個統(tǒng)一的注解@SpringBootApplication。
@SpringBootApplication = (默認(rèn)屬性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
分開解釋@Configuration,@EnableAutoConfiguration,@ComponentScan。
1、@Configuration:提到@Configuration就要提到他的搭檔@Bean。使用這兩個注解就可以創(chuàng)建一個簡單的spring配置類,可以用來替代相應(yīng)的xml配置文件。
<bean id = "car" class="com.test.Car">
<property name="wheel" ref = "wheel"></property>
</bean>
<bean id = "wheel" class="com.test.Wheel"></bean>
</beans>
相當(dāng)于:
public class Conf {
@Bean
public Car car() {
Car car = new Car();
car.setWheel(wheel());
return car;
}
@Bean
public Wheel wheel() {
return new Wheel();
}
}
@Configuration的注解類標(biāo)識這個類可以使用Spring IoC容器作為bean定義的來源。@Bean注解告訴Spring,一個帶有@Bean的注解方法將返回一個對象,該對象應(yīng)該被注冊為在Spring應(yīng)用程序上下文中的bean。
2、@EnableAutoConfiguration:能夠自動配置spring的上下文,試圖猜測和配置你想要的bean類,通常會自動根據(jù)你的類路徑和你的bean定義自動配置。
3、@ComponentScan:會自動掃描指定包下的全部標(biāo)有@Component的類,并注冊成bean,當(dāng)然包括@Component下的子注解@Service,@Repository,@Controller。
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
29 | 30 | 31 | 1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 | |||
12 | 13 | 14 | 15 | 16 | 17 | 18 | |||
19 | 20 | 21 | 22 | 23 | 24 | 25 | |||
26 | 27 | 28 | 29 | 30 | 1 | 2 | |||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
常用鏈接
留言簿(2)
隨筆分類
- Android(49)
- Androidpn(2)
- hibernate(1)
- Https(1)
- JavaCard(3)
- jQuery(6)
- netty
- NFC(1)
- react框架(1)
- spring(2)
- SpringBoot(1)
- Tomcat+Eclipse(18)
- WebService(2)
- 一些心得(1)
隨筆檔案
- 2020年4月 (4)
- 2015年7月 (5)
- 2015年6月 (6)
- 2015年5月 (4)
- 2015年4月 (3)
- 2015年3月 (1)
- 2015年2月 (1)
- 2015年1月 (4)
- 2014年12月 (1)
- 2014年11月 (2)
- 2014年10月 (2)
- 2014年9月 (2)
- 2014年5月 (5)
- 2014年3月 (3)
- 2014年2月 (2)
- 2014年1月 (8)
- 2013年12月 (2)
- 2013年7月 (2)
- 2013年6月 (4)
- 2013年5月 (16)
- 2012年7月 (1)
- 2012年3月 (2)
- 2011年7月 (6)
文章分類
文章檔案
相冊
收藏夾
Java
搜索
最新隨筆
最新評論

- 1.?re: Android JSON的簡單例子
- 評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
- --JSON.COM
- 2.?re: androidpn(本文服務(wù)器為tomcat)
- 評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
- --Deepak Singh